Hello,

I am trying to access custom product fields from the $order object during the checkout process. Is there a commerce or drupal function that I can use to access this information?

Thanks,
Paul

Comments

rszrama’s picture

What are you trying to do with them? The answer is going to involve Views, Rules, or a module, but your purpose in accessing the data will determine which one.

pnigro’s picture

I am trying to calculate a shipping cost using commerce shipping and a custom module. Inside the custom module I am using the following function to return the cost:

public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL)

I added a single float field to each of my products to represent a flat rate shipping cost. During checkout, I would like to loop through each sku in the cart, multiply the quantity of each sku by the respective float field (flat rate shipping cost), sum up the shipping cost of each unique sku, and return the shipping cost.

The thing I can't figure out is how to access product fields and quantity of a product in a shopping cart from within the above function. I tried the $order object, but all I could obtain was the line_item_id of each product. How can I correlate that to the actual product so that I can access the flat rate shipping cost field?

Thank you for your time and an excellent module,
Paul

j0rd’s picture

Could you not copy the code out of a standard shipping module available for Drupal Commerce? I assume these would loop over the project and calculate shipping totals.

You can view that code here:
http://drupal.org/project/commerce_shipping

Not sure if the code actually works, as I've not used it, but I assume you can find your answers by digging in there.

pnigro’s picture

I copied the example module from commerce shipping, but it only covers a very basic flat rate shipping scenario (a constant cost regardless of the number of items purchased). I looked inside of commerce_shipping.module and noticed entity_metadata_wrapper().
Could I use this function to access the product fields?

Thanks

j0rd’s picture

I'm not 100% sure about the entity stuff, as I'm still wrapping my head around the proper way to interface with that API.

With that said, you can try using this method to load the data:
http://api.drupal.org/api/drupal/includes--common.inc/function/entity_lo...

it would look something like entity_load('node', $entity->id); // where 'node' is what ever the type of your entity is.

rszrama’s picture

Status: Active » Closed (fixed)

Not sure how far you got with this, but nowadays with Shipping 2.x, you shouldn't even need the custom code. However, if you just need an entity metadata wrapper pointer, the code to get to product data would look something like this, assuming you have the $order object:

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
  $product_wrapper = $line_item_wrapper->commerce_product;
  // Then access any property or field on the product like so...
  if ($product_wrapper->commerce_price->amount->value() == 0) {
    // ..
  }
  // Or show the contents of the product variable via...
  drupal_set_message('<pre>'. print_r($product_wrapper->value(), TRUE) .'</pre>');
}

I know this is very late, so I hope you figured this out months ago. Figured I'd post the pseudo-code for posterity's sake, though.

cviccaro’s picture

Well rszrama, good thing you posted this.. Im juggling multiple projects and have the need for essentially the same module as the OP wrote about, so your post was a huge help. Thanks.

rszrama’s picture

Great, glad to hear it helped.