Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to add a class to body if the current user's cart is not empty.

I'd like to take the preprocess approach and not using rules.

so far using commerce_cart_order_load function I came up with this in template.php:

function THEME_preprocess_html(&$vars) {
    global $user;
    $order = commerce_cart_order_load($user->uid);

    if ($order != 0) {
        $vars['classes_array'][] = 'cart-loaded';
    }
}

but unfortunately it throws an unknown error.

share|improve this question
up vote 5 down vote accepted

Using this resource and thanks to @rémy I come up with this solution:

function THEME_preprocess_html(&$vars) {
    global $user;
    $cart = commerce_cart_order_load($user->uid);
    foreach (entity_metadata_wrapper('commerce_order', $cart)->commerce_line_items as $delta => $line_item_wrapper) {
      if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
        $vars['classes_array'][] = 'cart-loaded';
      }
    }
}

So after a while of testing I ended up with an error with anonymous users, so I digged a little bit deeper and come up with something new and no errors this time:

function THEMENAME_preprocess_html(&$vars) {
    global $user;
    $quantity = 0;
    $order = commerce_cart_order_load($user->uid);
    if ($order) {
        $wrapper = entity_metadata_wrapper('commerce_order', $order);
        $line_items = $wrapper->commerce_line_items;
        $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
        if ($quantity) {
            $vars['classes_array'][] = 'cart-loaded';
        }
    }
}
share|improve this answer

Using rules you would probably need to install context and context_rules module .., see here Is it possible to add css class to the page's body tag with rules when certain conditions are met?

So i would go with this approach: Function to determine whether my Cart is empty?

But doing it in preprocess html like here: http://stackoverflow.com/questions/7700838/adding-a-class-to-body/7700886

If you're using ajax cart (fe with dc_ajax_add_cart) you'll need to write some javascript too ..

share|improve this answer
    
I am actually using dc_ajax_add_cart module, Also I rather take the preprocess approach, based on what you referred so far I came up with this so far: function THEME_preprocess_html(&$vars) { global $user; $order = commerce_cart_order_load($user->uid); if ($order != 0) { $vars['classes_array'][] = 'cart-loaded'; } } but it throws an unknown error, can you help me out – Sohail Mar 30 at 12:19
    
@Sohail Any reason you've left all of that vitally important information out of your question? – Clive Mar 30 at 12:22
    
Hey @Clive , You are right, I guess it's been a while for me and forgot how to behave on stackexchange, I'll update the question right now, thanks – Sohail Mar 30 at 12:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.