I'm creating a module for a custom action for commerce calculation rule. The code seems to just stop after the foreach, and I'm not sure why.

What the code below is supposed to do is total the quantity of the products in the order with a sku that matches the $product_skus.

// e.g. $order has 3 products in it
//$product_skus = '123456, 654321';
function custom_module_count_products_by_sku($order, $product_skus)
{
  $total = 0;
  
  echo 'before if total: ' . $total. "\r\n";
  if (!empty($order)) {
    $skus = explode(',', trim($product_skus));
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    echo 'before foreach total: ' . $total. "\r\n";
    foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
      if( in_array( $line_item_wrapper->commerce_product->sku->value(), $skus ) ) {
        $total += $line_item_wrapper->quantity->value();
        //echo 'total: ' . $total . "\r\n";
      }
      echo 'foreach total: ' . $total. "\r\n";
    }
    echo 'after foreach total: ' . $total. "\r\n";
  }
  echo 'after if total: ' . $total. "\r\n";
  
  return array(
    'qty_of_sku' => $total
  );
}

Returns

before if total: 0
before foreach total: 0
foreach total: 10
foreach total: 10
foreach total: 10

Comments

dotmundo’s picture

Use drupal_set_message or dpm to show your values as well as the Devel module. Never use echo or print statements for debugging as the results can be very unreliable.

SomeDev’s picture

The result is the same. Nothing after the foreach is ran.

dpm results

before if total: 0
before foreach total: 0
foreach total: 10
(x3)
dotmundo’s picture

You should dpm the value of $order_wrapper->commerce_line_items to see how many items it has since that is the actual conditional from what I can see in the code.

SomeDev’s picture

If I do this before the foreach
dpm($order_wrapper->commerce_line_items)
I get
Property commerce_line_items

If I do this inside of the foreach
dpm($line_item_wrapper);
I get

Property 0
Property 1
Property 2
Property 3

I've checked, and
dpm($line_item_wrapper->commerce_product->sku->value());
Does return each line items sku.

And
dpm($line_item_wrapper->quantity->value())
Does return the correct line item quantity

dotmundo’s picture

Interesting.... Well I don't know why you are having this problem since according to your last post $line_item_wrapper does have values. I would check the php, apache and drupal logs to see what it says. You might have some kind of internal PHP problem such as out of memory. Outside of that, this appears to be a PHP problem rather than Drupal so you may want to pursue that on another forum.

dotmundo’s picture

One more thing.... always clear the cache before you run your code

SomeDev’s picture

I checked the error logs before I made this post. That's part of why this is such a head scratcher.

No PHP errors logged. Even with
error_reporting(-1);

Drupal DB log shows an error
Unable to get variable qty_of_sku, it is not defined.
Which makes sense, because the function isn't finishing, and that variable is never set. If I take out all the logic and just do the below, it works, but obviously isn't what I need.

function custom_module_count_products_by_sku($order, $product_skus)
{
  return array(
    'qty_of_sku' => 10
  );
}
SomeDev’s picture

By adding a Catch I was able to get this error
Exception: Unknown data property commerce_product.

RoloDMonkey’s picture

I noticed that the function isn't showing 'after foreach total: ' or 'after if total: '. If that is true, then PHP must be throwing an error before it can finish the function. You need to look at your logs to see what is happening.

--

Read more at iRolo.net

bribread22’s picture

To answer your question about the foreach loop, the code is probably stopping because shortly after that, you have a return statement returning an array. This would end this function.

Also, I am noticing that you are using an echo statement to do debugging. In Drupal, this is very unreliable/should be avoided because many times, you will not see where your echo statement is being printed out. A much more reliable way to do this would be in a development environment to enable both the devel and devel_debug_log module and instead of using echo, use the ddl function. This will save the result in a Debug log. You can even view arrays in a nice drop-down view. Here's an example implementation.

$variable = array(
  'fruit' => 'apple',
  'vegetable' => 'lettuce',
);

ddl($variable);

This function will make debugging much easier.