4

I'm trying to make a block with an image that would change when there is something in his or hger basket (empty basket image when basket is empty, full basket when it's full) My understanding of PHP is weak, but I found this topic :

http://www.drupalcommerce.org/discussions/787/simple-shopping-cart-summary

and I wrote that:

<?php
global $user;
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_total = $wrapper->commerce_order_total->value();
if ($order_total['amount']>0) echo ('<a href="/twotrees/cart"> <img src="/sites/default/files/basketfull.png" > </img> </a>');
else echo ('<a href="/twotrees/cart"> <img src="/sites/default/files/basketempty.png" > </img> </a>');
?>

Now on my home server everything worked as expected. On the reomte one i got several show-stopping errors.

If i put the whole thing, I get

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. w EntityDrupalWrapper->set() (line 744 of /profiles/commerce_kickstart/modules/entity/includes/entity.wrapper.inc).

By trial and error, I detected that leaving the first two lines

global $user;
$order = commerce_cart_order_load($user->uid);

are 'innocent'. It's the third one

$wrapper = entity_metadata_wrapper('commerce_order', $order);

that generates the error. So actually it's the code from that commerce thread that seems to generate the error. To me it seems pretty innocent, though.

As I said, I know very little about PHP, not to mention the internals of Drupal Commerce. I'd be grateful for any suggestions.

1 Answer 1

7

Try this

global $user;
$image = theme('image', array('path' => url('sites/default/files/basketempty.png', array('absolute' => TRUE))));
$order = commerce_cart_order_load($user->uid);

if ($order) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $line_items = $wrapper->commerce_line_items;
  $total = commerce_line_items_total($line_items);
  if($total['amount']){ 
    $image = theme('image', array('path' => url('sites/default/files/basketfull.png', array('absolute' => TRUE))));
  }
}

echo l($image, 'twotrees/cart', array('html' => TRUE));
1
  • Worked great!Thank you. Trying to look at the documentation of the theme, commerce_cart_order_load, and l functions to see how it works.
    – Dalmaz
    Commented Apr 30, 2013 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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