Flat Shipping price per product using Drupal Commerce I've been beating my head against a wall trying to configure shipping in Drupal Commerce for a week now and so here I am! I have a Select or Other field in the product entity with a couple of choices:


Free Shipping
Call for Details
Other (and the admin enters a flat price)


Again, this is entered on a per product basis, on the Commerce Product entity (not the node). My question is how do I update the shipping price based on this value? It should work as follows:


Item 1 (shipping: $1.00)
Item 2 (shipping: $4.00)
Item 3 (shipping: Free)
Item 4 (shipping: Call for Details)
Item 5 (shipping: $1.00)
Total Shipping: $6.00


Perhaps I'm doing this wrong. Any direction would be awesome. Thanks!



This is the final working code:

class CustomShipping extends CommerceShippingQuote {
  public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL) {
    $order = $order ? $order : $this-&gt;order;
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $price = 0;
    // Loop through the products, and add costs.
    foreach ($order_wrapper-&gt;commerce_line_items as $line_item_wrapper) {
      if ($line_item_wrapper-&gt;commerce_product-&gt;field_shipping_price_other-&gt;value() &gt; 1) {
          $price += ($line_item_wrapper-&gt;commerce_product-&gt;field_shipping_price_other-&gt;value()*100)*$line_item_wrapper-&gt;quantity-&gt;value();
      }
    }
    return array(array(
      'amount' =&gt; $price,
      'currency_code' =&gt; $currency_code,
      'label' =&gt; t('Custom Shipping label'),
      'quantity' =&gt; 1,
    ));
  }
}
If you are using commerce shipping 1.x, you should do something like this:

Disclaimer, this is written by memory.

This is based on two fields on the product,

field_shipping_type containing the shipping type
field_flat_rate containing the actual rate for the product.


Create a custom module and add a folder for the shipping plugin, fx plugins/quotes/custom_shipping
Create the custom_shipping.inc with the plugin definition.

&lt;?php

$plugin = array(
  'title' =&gt; t('Custom shipping'),
  'description' =&gt; t('Descriptive text.'),
  'handler' =&gt; array(
    'class' =&gt; 'CustomShipping',
    'parent' =&gt; 'quote_base'
  ),
  'price_component' =&gt; array(
   'title' =&gt; t('Shipping'),
   'display_title' =&gt; t('Shipping'),
   'weight' =&gt; -40,
  ),
);

Create the actual shipping logic in the CustomShipping.class.php file in the same dir. All you need is something like this:

class CustomShipping extends CommerceShippingQuote {
  public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL) {
    $order = $order ? $order : $this-&gt;order;
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $price = 0;
    // Loop through the products, and add costs.
    foreach ($order_wrapper-&gt;commerce_line_items as $line_item_wrapper) {
      switch ($line_item_wrapper-&gt;commerce_product-&gt;field_shipping_type-&gt;value()) {
        case 'free':
          break;
        case 'flat_rate':
          $price += $line_item_wrapper-&gt;commerce_product-&gt;field_flat_rate-&gt;amount-&gt;value();
          break;
        case 'call_for_details':
          // Call for details
          break;
      }
    }
    return array(array(
      'amount' =&gt; $price,
      'currency_code' =&gt; $currency_code,
      'label' =&gt; t('Custom shipping method line item label'),
      'quantity' =&gt; 1,
    ));
  }
}