3

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.

6

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));
  • 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 Apr 30 '13 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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