Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal Commerce: Exlude shipping when product has some property

Parent Feed: 

Few days ago my client came and said.. I need to exlude a shipping when there's special type of product.
At first I was sure this can be done with rules but it didn't work out for me.
So after spending some time with this I wrote some php.
At first I would like you to know. Its not good idea to put some php throught the database, becouse it will make your site at least a bit slower. But still my client needed this really fast so I did it.
At first from the shipping options I choosed configure component and added new condition wich was a PHP.

# Get all the line item ids from the order.
$line_item_ids = $commerce_order->commerce_line_items[LANGUAGE_NONE];
$line_items = commerce_line_item_load_multiple($line_item_ids);
# If the line item is the type of product then add this to array.   
foreach($line_items as $line_item) {
  # Put all the product types the array.
  if(isset($line_item->commerce_product[LANGUAGE_NONE][0]['product_id'])) {
      $product_ids[] = $line_item->commerce_product[LANGUAGE_NONE][0]['product_id'];
  }
}
$products = commerce_product_load_multiple( $product_ids);
foreach($products as $product) {
  $types[] = $product->type;
}
# If the product type (tops) is in array then return true.
if (in_array('tops', $types)) {
   return TRUE;
}

After I just added if this is is true then negate. This could be easily used other ways. Lets say you want to do something if theres this type of procuct in the shppping cart.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web