2

I'm trying to add a fixed weight to all cart order for shipping. I know I need to implement hook_commerce_physical_order_weight_alter() but I'm unsure of how to implement the function in a custom module. I have been testing this code in the devel php exectute window

global $user;
$order = commerce_cart_order_load($user->uid);
$weight = commerce_physical_order_weight($order, $unit = 'oz');
function commerce_physical_order_weight_alter(&$weight, $order, $unit){
if ($weight['weight'] >0)
{$weight = $weight + 6;}
}
dpm($weight);

Where I am confused, is passing the variable by reference. From what I understand, this should be enough to increment the weight but it returns the unadjusted weight

I can grab all of the stuff I need outside the function, but I'm unclear about passing the variable by reference and how to use that syntax for my purpose. I know that this means that the function returns a modified variable. It doesn't look like the function is called since I've tried to put the dpm in a condition inside the _alter function and it doesn't run.

Thanks the help so far, and I appreciate the earlier answers.

  • You know how to use hooks, it seems. So it is in module named add_shipping_weight, module is enabled and? Is it fired at all? Does dpm show what you expect it to? What's the problem? Because it looks like you are doing just fine. – Mołot Dec 24 '15 at 12:28
  • 1
    It doesn't fire, and there's no dpm message. I tried running dpm in the devel execute php and I get two empty bullets – nizz0k Dec 24 '15 at 12:30
  • Edit this into your question, please. Because in 6 and 7 proper name should be enough to "set this up", only here it wasn't. Oh, and expect more clarification requests from more knowledgeable people, because reason probably is in sth you don't know to post. What you posted looks good. – Mołot Dec 24 '15 at 12:34
  • This is why I posted. I got this far, and I don't know what else to say. The module files and function names all match. The only thing I can think of is to try to call the order weight function within the module, or something else to instantiate, like checking if a cart is present, or that there's an order – nizz0k Dec 24 '15 at 12:51
  • So, it's updated. I think I have it mostly right, but it's not behaving the way I expect – nizz0k Dec 26 '15 at 12:06
2

If your hook_commerce_physical_order_weight_alter is not fired (remove if to double check that it's not), check if commerce_physical_order_weight() is fired as alter hook is right at the end (so add dpm() or die() at start of the function):

    function commerce_physical_order_weight($order, $unit = 'lb') {
      // ...
      // Allow other modules to alter the weight if necessary.
      drupal_alter('commerce_physical_order_weight', $weight, $order, $unit);
  • if it is not fired, that means the problem is somewhere else, so check if commerce_physical module is enabled and configured properly and you've the right dependency to it,
  • if it is fired, then check that:

    • your module is exactly called add_shipping_weight,
    • the module file included (add hook_init() or die() to check)
    • clear your caches (Drupal caches, memcached/redis if used, etc.), so list of hooks can be updated (it's a common mistake)
| improve this answer | |
  • I think we're on the right track here. I think I'll have to calculate the weight for the current cart with a second function to pull the values into the function, then use the if function exists, do math. – nizz0k Dec 24 '15 at 13:11
  • this didn't quite get me where I needed to go, but it helped. – nizz0k Dec 27 '15 at 7:47
0

The reason why this isn't working is because $weight isn't an integer, it's an array (much like price field values are).

Looking around the code, it looks like $weight will have 2 array keys:

  // Update the weight amount using the multiplier.
  $weight['weight'] *= $multiplier;

  // Update the unit type to the target type.
  $weight['unit'] = $unit;

So you need to check if $weight['weight'] > 0 instead of just $weight > 0.

| improve this answer | |
0

Thanks to everyone for the above comments, it got me really close to what I needed - but like OP had difficulty getting it to behave as expected. In the end, I found that it was actually very simple. By including the following in my custom helper module, I was able to add a weight to the total order:

/**
* Implements hook_commerce_physical_order_weight_alter()
*/
function YOUR_MODULE_commerce_physical_order_weight_alter(&$weight, $order, $unit = 'lb') {
    $weight['weight'] = $weight['weight'] + $YOUR_VALUE;
}    
| improve this answer | |

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.