4

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->order;
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $price = 0;
    // Loop through the products, and add costs.
    foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
      if ($line_item_wrapper->commerce_product->field_shipping_price_other->value() > 1) {
          $price += ($line_item_wrapper->commerce_product->field_shipping_price_other->value()*100)*$line_item_wrapper->quantity->value();
      }
    }
    return array(array(
      'amount' => $price,
      'currency_code' => $currency_code,
      'label' => t('Custom Shipping label'),
      'quantity' => 1,
    ));
  }
}
6

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.

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

  2. Create the custom_shipping.inc with the plugin definition.

    <?php
    
    $plugin = array(
      'title' => t('Custom shipping'),
      'description' => t('Descriptive text.'),
      'handler' => array(
        'class' => 'CustomShipping',
        'parent' => 'quote_base'
      ),
      'price_component' => array(
       'title' => t('Shipping'),
       'display_title' => t('Shipping'),
       'weight' => -40,
      ),
    );
    
  3. 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->order;
        $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
        $price = 0;
        // Loop through the products, and add costs.
        foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
          switch ($line_item_wrapper->commerce_product->field_shipping_type->value()) {
            case 'free':
              break;
            case 'flat_rate':
              $price += $line_item_wrapper->commerce_product->field_flat_rate->amount->value();
              break;
            case 'call_for_details':
              // Call for details
              break;
          }
        }
        return array(array(
          'amount' => $price,
          'currency_code' => $currency_code,
          'label' => t('Custom shipping method line item label'),
          'quantity' => 1,
        ));
      }
    }
    
  • Thanks, I'm actually using shipping 2.x, but I don't see a reason I can't go with 1.x. It isn't as if it works right now! I'll let you know. – Aaron Ortega Mar 2 '12 at 22:47
  • 1
    @Clive I did write the module after all ;) – googletorp Mar 3 '12 at 9:50
  • I didn't realise that...still an impressive brain dump in my opinion! – Clive Mar 3 '12 at 10:09
  • I'm almost there. I made a couple of changes to the code itself, the issue I am having now involved quantity. If I have more than one of the item I need to multiply the shipping amount. Any pointers on what the var is for quantity? – Aaron Ortega Mar 7 '12 at 1:36
  • $line_item_wrapper->quantity->value() I believe. – googletorp Mar 7 '12 at 10:39

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.