Advertisement
Guest User

commerce_order.rules.inc modification for sku wildcard

a guest
Jul 11th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.57 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Rules integration for orders.
  6.  *
  7.  * @addtogroup rules
  8.  * @{
  9.  */
  10.  
  11. /**
  12.  * Implements hook_rules_condition_info().
  13.  */
  14. function commerce_order_rules_condition_info() {
  15.   $conditions = array();
  16.  
  17.   $conditions['commerce_order_compare_address'] = array(
  18.     'label' => t('Order address component comparison'),
  19.     'parameter' => array(
  20.       'commerce_order' => array(
  21.         'type' => 'commerce_order',
  22.         'label' => t('Order'),
  23.         'description' => t('The order containing the profile reference with the address in question.'),
  24.       ),
  25.       'address_field' => array(
  26.         'type' => 'text',
  27.         'label' => t('Address'),
  28.         'options list' => 'commerce_order_address_field_options_list',
  29.         'description' => t('The address associated with this order whose component you want to compare.'),
  30.         'restriction' => 'input',
  31.       ),
  32.       'address_component' => array(
  33.         'type' => 'text',
  34.         'label' => t('Address component'),
  35.         'options list' => 'commerce_order_address_component_options_list',
  36.         'description' => t('The actual address component you want to compare. Common names of address components are given in parentheses.'),
  37.         'restriction' => 'input',
  38.       ),
  39.       'operator' => array(
  40.         'type' => 'text',
  41.         'label' => t('Operator'),
  42.         'description' => t('The comparison operator.'),
  43.         'optional' => TRUE,
  44.         'default value' => 'equals',
  45.         'options list' => 'commerce_order_address_comparison_operator_options_list',
  46.         'restriction' => 'input',
  47.       ),
  48.       'value' => array(
  49.         'type' => 'text',
  50.         'label' => t('Value'),
  51.         'description' => t('The value to compare against the address component. Bear in mind that addresses using select lists for various components may use a value different from the option you select. For example, countries are selected by name, but the value is the two letter abbreviation.'),
  52.       ),
  53.     ),
  54.     'group' => t('Commerce Order'),
  55.     'callbacks' => array(
  56.       'execute' => 'commerce_order_rules_compare_address',
  57.     ),
  58.   );
  59.  
  60.   $conditions['commerce_order_contains_product'] = array(
  61.     'label' => t('Order contains a particular product'),
  62.     'parameter' => array(
  63.       'commerce_order' => array(
  64.         'type' => 'commerce_order',
  65.         'label' => t('Order'),
  66.         'description' => t('The order whose line items should be checked for the specified product. If the specified order does not exist, the comparison will act as if it is against a quantity of 0.'),
  67.       ),
  68.       'product_id' => array(
  69.         'type' => 'text',
  70.         'label' => t('Product SKU'),
  71.         'description' => t('The SKU of the product to look for on the order.'),
  72.       ),
  73.       'operator' => array(
  74.         'type' => 'text',
  75.         'label' => t('Operator'),
  76.         'description' => t('The operator used with the quantity value below to compare the quantity of the specified product on the order.'),
  77.         'default value' => '>=',
  78.         'options list' => 'commerce_numeric_comparison_operator_options_list',
  79.         'restriction' => 'input',
  80.       ),
  81.       'value' => array(
  82.         'type' => 'text',
  83.         'label' => t('Quantity'),
  84.         'default value' => '1',
  85.         'description' => t('The value to compare against the quantity of the specified product on the order.'),
  86.       ),
  87.     ),
  88.     'group' => t('Commerce Order'),
  89.     'callbacks' => array(
  90.       'execute' => 'commerce_order_rules_contains_product',
  91.     ),
  92.   );
  93.  
  94. //New condition added by Matt Habermehl on July 11 2012 - sorry for the hackiness.
  95.   $conditions['commerce_order_partial_sku'] = array(
  96.     'label' => t('Order has one or more products where the sku contains...'),
  97.     'parameter' => array(
  98.       'commerce_order' => array(
  99.         'type' => 'commerce_order',
  100.         'label' => t('Order'),
  101.         'description' => t('The order whose line items should be checked for the specified product. If the specified order does not exist, the comparison will act as if it is against a quantity of 0.'),
  102.       ),
  103.       'product_id' => array(
  104.         'type' => 'text',
  105.         'label' => t('Product SKU'),
  106.         'description' => t('The SKU of the product must contain...'),
  107.       ),
  108.       'operator' => array(
  109.         'type' => 'text',
  110.         'label' => t('Operator'),
  111.         'description' => t('The operator used with the quantity value below to compare the quantity of the specified product on the order.'),
  112.         'default value' => '>=',
  113.         'options list' => 'commerce_numeric_comparison_operator_options_list',
  114.         'restriction' => 'input',
  115.       ),
  116.       'value' => array(
  117.         'type' => 'text',
  118.         'label' => t('Quantity'),
  119.         'default value' => '1',
  120.         'description' => t('The value to compare against the quantity of the specified product on the order.'),
  121.       ),
  122.     ),
  123.     'group' => t('Commerce Order'),
  124.     'callbacks' => array(
  125.       'execute' => 'commerce_order_rules_partial_sku',
  126.     ),
  127.   );
  128. //End new condition added by Matt Habermehl on July 11 2012
  129.  
  130.   $conditions['commerce_order_compare_total_product_quantity'] = array(
  131.     'label' => t('Total product quantity comparison'),
  132.     'parameter' => array(
  133.       'commerce_order' => array(
  134.         'type' => 'commerce_order',
  135.         'label' => t('Order'),
  136.         'description' => t('The order whose product line item quantities should be totalled. If the specified order does not exist, the comparison will act as if it is against a quantity of 0.'),
  137.       ),
  138.       'operator' => array(
  139.         'type' => 'text',
  140.         'label' => t('Operator'),
  141.         'description' => t('The comparison operator to use against the total number of products on the order.'),
  142.         'default value' => '>=',
  143.         'options list' => 'commerce_numeric_comparison_operator_options_list',
  144.         'restriction' => 'input',
  145.       ),
  146.       'value' => array(
  147.         'type' => 'text',
  148.         'label' => t('Quantity'),
  149.         'default value' => 1,
  150.         'description' => t('The value to compare against the total quantity of products on the order.'),
  151.       ),
  152.     ),
  153.     'group' => t('Commerce Order'),
  154.     'callbacks' => array(
  155.       'execute' => 'commerce_order_rules_compare_total_quantity',
  156.     ),
  157.   );
  158.  
  159.   return $conditions;
  160. }
  161.  
  162. /**
  163.  * Options list callback: address fields for the address comparison condition.
  164.  */
  165. function commerce_order_address_field_options_list() {
  166.   $options = array();
  167.  
  168.   // Retrieve a list of all address fields on customer profile bundles.
  169.   $address_fields = commerce_info_fields('addressfield', 'commerce_customer_profile');
  170.  
  171.   // Loop over every customer profile reference field on orders.
  172.   foreach (commerce_info_fields('commerce_customer_profile_reference', 'commerce_order') as $field_name => $field) {
  173.     // Retrieve the type of customer profile referenced by this field.
  174.     $type = $field['settings']['profile_type'];
  175.  
  176.     // Loop over every address field looking for any attached to this bundle.
  177.     foreach ($address_fields as $address_field_name => $address_field) {
  178.       if (in_array($type, $address_field['bundles']['commerce_customer_profile'])) {
  179.         // Add it to the options list.
  180.         $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', $type);
  181.  
  182.         $options[commerce_customer_profile_type_get_name($type)][$field_name . '|' . $address_field_name] = check_plain($instance['label']);
  183.       }
  184.     }
  185.   }
  186.  
  187.   if (empty($options)) {
  188.     drupal_set_message(t('No order addresses could be found for comparison.'), 'error');
  189.   }
  190.  
  191.   return $options;
  192. }
  193.  
  194. /**
  195.  * Options list callback: components for the address comparison condition.
  196.  */
  197. function commerce_order_address_component_options_list() {
  198.   return array(
  199.     'country' => t('Country'),
  200.     'name_line' => t('Full name'),
  201.     'first_name' => t('First name'),
  202.     'last_name' => t('Last name'),
  203.     'organisation_name' => t('Company name'),
  204.     'thoroughfare' => t('Thoroughfare (Street address)'),
  205.     'premise' => t('Premise (Building)'),
  206.     'sub_premise' => t('Sub-premise (Suite)'),
  207.     'locality' => t('Locality (City)'),
  208.     'dependent_locality' => t('Dependent locality (Town)'),
  209.     'administrative_area' => t('Administrative area (State / Province)'),
  210.     'sub_administrative_area' => t('Sub-administrative area (District)'),
  211.     'postal_code' => t('Postal code'),
  212.   );
  213. }
  214.  
  215. /**
  216.  * Options list callback: operators for the address comparison condition.
  217.  */
  218. function commerce_order_address_comparison_operator_options_list() {
  219.   return array(
  220.     'equals' => t('equals'),
  221.     'begins with' => t('begins with'),
  222.     'contains' => t('contains'),
  223.   );
  224. }
  225.  
  226. /**
  227.  * Condition callback: compares an address component against the given value.
  228.  */
  229. function commerce_order_rules_compare_address($order, $address_field, $component, $operator, $value) {
  230.   list($field_name, $address_field_name) = explode('|', $address_field);
  231.  
  232.   // If we actually received a valid order...
  233.   if (!empty($order)) {
  234.     $wrapper = entity_metadata_wrapper('commerce_order', $order);
  235.  
  236.     // And if we can actually find the requested address data...
  237.     if (!empty($wrapper->{$field_name}) && !empty($wrapper->{$field_name}->{$address_field_name})) {
  238.       $address = $wrapper->{$field_name}->{$address_field_name}->value();
  239.  
  240.       // Make a comparison based on the operator.
  241.       switch ($operator) {
  242.         case 'equals':
  243.           return $address[$component] == $value;
  244.         case 'begins with':
  245.           return strpos($address[$component], $value) === 0;
  246.         case 'contains':
  247.           return strpos($address[$component], $value) !== FALSE;
  248.       }
  249.     }
  250.   }
  251.  
  252.   return FALSE;
  253. }
  254.  
  255. /**
  256.  * Condition callback: checks to see if a particular product exists on an order
  257.  * in the specified quantity.
  258.  */
  259. function commerce_order_rules_contains_product($order, $sku, $operator, $value) {
  260.   $products = array($sku => 0);
  261.  
  262.   // If we actually received a valid order...
  263.   if (!empty($order)) {
  264.     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  265.  
  266.     // Populate the array of the quantities of the products on the order.
  267.     foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
  268.       if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
  269.         // Extract a product ID and quantity from the line item.
  270.         $line_item_sku = $line_item_wrapper->commerce_product->sku->value();
  271.         $quantity = $line_item_wrapper->quantity->value();
  272.  
  273.         // Update the product's quantity value.
  274.         if (empty($products[$line_item_sku])) {
  275.           $products[$line_item_sku] = $quantity;
  276.         }
  277.         else {
  278.           $products[$line_item_sku] += $quantity;
  279.         }
  280.       }
  281.     }
  282.   }
  283.  
  284.   // Make a quantity comparison based on the operator.
  285.   switch ($operator) {
  286.     case '<':
  287.       return $products[$sku] < $value;
  288.     case '<=':
  289.       return $products[$sku] <= $value;
  290.     case '=':
  291.       return $products[$sku] == $value;
  292.     case '>=':
  293.       return $products[$sku] >= $value;
  294.     case '>':
  295.       return $products[$sku] > $value;
  296.   }
  297.  
  298.   return FALSE;
  299. }
  300.  
  301. //Condition callback added by Matt Habermehl on July 11, 2012 - sorry for the hackiness
  302.  
  303. /**
  304.  * Condition callback: checks to see if a particular product exists on an order
  305.  * in the specified quantity, based on a sku fragment.
  306.  */
  307. function commerce_order_rules_partial_sku($order, $sku, $operator, $value) {
  308.   $total_quantity = 0;
  309.  
  310.   // If we actually received a valid order...
  311.   if (!empty($order)) {
  312.     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  313.  
  314.     // Populate the array of the quantities of the products on the order.
  315.     foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
  316.       if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
  317.         // Extract a product ID and quantity from the line item.
  318.         $line_item_sku = $line_item_wrapper->commerce_product->sku->value();
  319.         $quantity = $line_item_wrapper->quantity->value();
  320.    
  321.     // If the partial sku is contained in the line item sku...
  322.     if (strpos($line_item_sku,$sku) !== false) {
  323.         // ...update the total found quantity value.
  324.         $total_quantity += $quantity;
  325.     }
  326.    
  327.       }
  328.     }
  329.   }
  330.  
  331.   // Make a quantity comparison based on the operator.
  332.   switch ($operator) {
  333.     case '<':
  334.       return $total_quantity < $value;
  335.     case '<=':
  336.       return $total_quantity <= $value;
  337.     case '=':
  338.       return $total_quantity == $value;
  339.     case '>=':
  340.       return $total_quantity >= $value;
  341.     case '>':
  342.       return $total_quantity > $value;
  343.   }
  344.  
  345.   return FALSE;
  346. }
  347.  
  348.  
  349. //end condition callback added by Matt Habermehl on July 11, 2012
  350.  
  351. /**
  352.  * Condition callback: compares the total quantity of products on an order
  353.  * against the specified quantity.
  354.  */
  355. function commerce_order_rules_compare_total_quantity($order, $operator, $value) {
  356.   $quantity = 0;
  357.  
  358.   // If we received an order, get the total quantity of products on it.
  359.   if (!empty($order)) {
  360.     $wrapper = entity_metadata_wrapper('commerce_order', $order);
  361.  
  362.     if (!empty($wrapper->commerce_line_items)) {
  363.       $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());
  364.     }
  365.   }
  366.  
  367.   // Make a quantity comparison based on the operator.
  368.   switch ($operator) {
  369.     case '<':
  370.       return $quantity < $value;
  371.     case '<=':
  372.       return $quantity <= $value;
  373.     case '=':
  374.       return $quantity == $value;
  375.     case '>=':
  376.       return $quantity >= $value;
  377.     case '>':
  378.       return $quantity > $value;
  379.   }
  380.  
  381.   return FALSE;
  382. }
  383.  
  384. /**
  385.  * Implements hook_rules_action_info().
  386.  */
  387. function commerce_order_rules_action_info() {
  388.   $actions = array();
  389.  
  390.   $actions['commerce_order_update_state'] = array(
  391.     'label' => t('Update the order state'),
  392.     'parameter' => array(
  393.       'commerce_order' => array(
  394.         'type' => 'commerce_order',
  395.         'label' => t('Order to update'),
  396.       ),
  397.       'order_state' => array(
  398.         'type' => 'text',
  399.         'label' => t('Order state'),
  400.         'description' => t('Select the order state whose default status the order will be updated to.'),
  401.         'options list' => 'commerce_order_state_options_list',
  402.       ),
  403.     ),
  404.     'group' => t('Commerce Order'),
  405.     'callbacks' => array(
  406.       'execute' => 'commerce_order_rules_update_state',
  407.     ),
  408.   );
  409.  
  410.   $actions['commerce_order_update_status'] = array(
  411.     'label' => t('Update the order status'),
  412.     'parameter' => array(
  413.       'commerce_order' => array(
  414.         'type' => 'commerce_order',
  415.         'label' => t('Order to update'),
  416.       ),
  417.       'order_status' => array(
  418.         'type' => 'text',
  419.         'label' => t('Order status'),
  420.         'options list' => 'commerce_order_status_options_list',
  421.       ),
  422.     ),
  423.     'group' => t('Commerce Order'),
  424.     'callbacks' => array(
  425.       'execute' => 'commerce_order_rules_update_status',
  426.     ),
  427.   );
  428.  
  429.   return $actions;
  430. }
  431.  
  432. /**
  433.  * Rules action: updates an order's status to the default status of the given
  434.  *   order state.
  435.  */
  436. function commerce_order_rules_update_state($order, $name) {
  437.   $order_state = commerce_order_state_load($name);
  438.   commerce_order_status_update($order, $order_state['default_status'], FALSE, NULL, t('Order state updated via Rules.'));
  439. }
  440.  
  441. /**
  442.  * Rules action: updates an order's status using the Order API.
  443.  */
  444. function commerce_order_rules_update_status($order, $name) {
  445.   commerce_order_status_update($order, $name, FALSE, NULL, t('Order status updated via Rules.'));
  446. }
  447.  
  448. /**
  449.  * @}
  450.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement