1
Answers
Vote up!
2
Vote down!

Commerce Conditional Module for checking SKU's in rules

I'm in the process of finishing up a module for checking for the existence of a particular sku/skus in a product referenced by a line item. I would like to publish this so the whole community can use it. Essentially it's a module built by Prince Manfred called Commerce Conditional. Due to the fact that i'm using the conditional on the action 'calculating a shipping rate' it doesn't have the entities i need in scope. How do I bring it in scope? I have the commerce_line_item but the scope is only the shipping line item. Enclosed is the check that the rules condition does. Please help me increase the scope to the product line items and the skus of the products. Thanks :)

<?php
function commerce_sku_condition_has_sku($line_item, $sku_id) {

  if (!empty(
$line_item)) {
   
$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
   
$sku_ids = $wrapper->commerce_product->sku->raw();
    if (!empty(
$sku_ids)) {
      if (
is_array($sku_ids)) {
        foreach (
$sku_ids as $sku_id_line_ids) {
          if (
$sku_id_line_ids == $sku_id) {
            return
TRUE;
        }
      }
    }
  }
}
  return
FALSE;

}
?>
Asked by: dannymacom
on June 4, 2012

1 Answer

Vote up!
2
Vote down!

I decided on loading all skus from my commerce products and comparing it against that list. This works but I still need to create an action to apply specific unit price costs to specific line items based on their skus, wish me luck as I hope to release the module for the community once complete. :)

function commerce_sku_condition_has_sku($line_item, $sku_id) {
   
$skus = entity_load('commerce_product', $product_ids = FALSE, $conditions = array(), $reset = FALSE);
    foreach ($skus as $key => $value) {
$allproduct_skus[$key] = $value->sku;
    }

$new_sku = array();

//TODO: CHECK IF ARRAY
//if (!empty($line_item)){
//  if (is_array($line_item)){
foreach ($allproduct_skus as $all_product_sku_id => $all_product_sku_term){
     foreach ($line_item as $line_item_object){
         if ($all_product_sku_term == $line_item_object->line_item_label){
             $new_sku[$all_product_sku_id] = array($all_product_sku_term => $line_item_object->quantity);
              if ($sku_id == $all_product_sku_id){
                  return TRUE;
              }
//        }
//       }
     }
  } 
}


//ddl_once($new_sku);
//ddl_once($allproduct_skus, $title='all product skus where key = sku id and value = sku value');


return FALSE;

}
Answer by: dannymacom
Posted: Jun 5, 2012