diff --git a/inline_conditions.inline_conditions.inc b/inline_conditions.inline_conditions.inc
new file mode 100644
index 0000000..1df1468
--- /dev/null
+++ b/inline_conditions.inline_conditions.inc
@@ -0,0 +1,441 @@
+ t('Amount'),
+ 'entity type' => 'commerce_order',
+ 'callbacks' => array(
+ 'configure' => 'commerce_order_compare_order_amount_configure',
+ 'build' => 'commerce_order_compare_order_amount_build',
+ ),
+ );
+
+ $conditions['commerce_order_has_owner'] = array(
+ 'label' => t('User'),
+ 'entity type' => 'commerce_order',
+ 'callbacks' => array(
+ 'configure' => 'commerce_order_has_owner_configure',
+ 'build' => 'commerce_order_has_owner_build',
+ ),
+ );
+
+ $conditions['commerce_order_contains_products'] = array(
+ 'label' => t('Product(s)'),
+ 'entity type' => 'commerce_order',
+ 'callbacks' => array(
+ 'configure' => 'commerce_order_contains_products_configure',
+ 'build' => 'commerce_order_contains_products_build',
+ ),
+ );
+
+ $conditions['commerce_order_has_specific_quantity_products'] = array(
+ 'label' => t('Product(s) and quantity'),
+ 'entity type' => 'commerce_order',
+ 'callbacks' => array(
+ 'configure' => 'commerce_order_has_specific_quantity_products_configure',
+ 'build' => 'commerce_order_has_specific_quantity_products_build',
+ ),
+ );
+ }
+
+ if (module_exists('commerce_shipping')) {
+ $conditions['commerce_shipping_compare_shipping_service'] = array(
+ 'label' => t('Shipping service'),
+ 'entity type' => 'commerce_order',
+ 'callbacks' => array(
+ 'configure' => 'commerce_shipping_compare_shipping_service_configure',
+ 'build' => 'commerce_shipping_rules_line_item_exists',
+ ),
+ );
+ }
+
+ if (module_exists('commerce_product')) {
+ $conditions['commerce_product_contains_products'] = array(
+ 'label' => t('Product(s)'),
+ 'entity type' => 'commerce_line_item',
+ 'callbacks' => array(
+ 'configure' => 'commerce_product_contains_products_configure',
+ 'build' => 'commerce_product_contains_products_build',
+ ),
+ );
+ }
+
+ if (module_exists('taxonomy')) {
+ $conditions['commerce_product_has_specified_terms'] = array(
+ 'label' => t('Product attributes'),
+ 'entity type' => 'commerce_line_item',
+ 'callbacks' => array(
+ 'configure' => 'commerce_product_has_specified_terms_configure',
+ 'build' => 'commerce_product_has_specified_terms_build',
+ ),
+ );
+ }
+
+ return $conditions;
+}
+
+/**
+ * Implements hook_inline_conditions_build_alter().
+ */
+function inline_conditions_inline_conditions_build_alter(&$value) {
+ switch ($value['condition_name']) {
+ case 'commerce_order_contains_products':
+ case 'commerce_order_has_specific_quantity_products':
+ $entity_ids = array();
+ foreach ($value['condition_settings']['products'] as $delta) {
+ $entity_ids[] = reset($delta);
+ }
+ $products = commerce_product_load_multiple($entity_ids);
+
+ $value['condition_settings']['products'] = '';
+ foreach ($products as $product) {
+ $value['condition_settings']['products'] .= $product->sku;
+ if ($product !== end($products)) {
+ $value['condition_settings']['products'] .= ', ';
+ }
+ }
+ break;
+
+ case 'commerce_product_has_specified_terms':
+ $terms = $value['condition_settings']['terms'];
+
+ $value['condition_settings']['terms'] = '';
+ foreach ($terms as $term) {
+ $value['condition_settings']['terms'] .= reset($term);
+ if ($term !== end($terms)) {
+ $value['condition_settings']['terms'] .= ', ';
+ }
+ }
+ break;
+
+ case 'commerce_product_contains_products':
+ $entity_ids = array();
+ foreach ($value['condition_settings']['sku'] as $delta) {
+ $entity_ids[] = reset($delta);
+ }
+ $products = commerce_product_load_multiple($entity_ids);
+
+ $value['condition_settings']['sku'] = '';
+ foreach ($products as $product) {
+ $value['condition_settings']['sku'] .= $product->sku;
+ if ($product !== end($products)) {
+ $value['condition_settings']['sku'] .= ', ';
+ }
+ }
+ break;
+
+ }
+}
+
+/**
+ * Configuration callback for commerce_order_compare_order_amount.
+ *
+ * @param array $settings
+ * An array of rules condition settings.
+ *
+ * @return array;
+ * A form element array.
+ */
+function commerce_order_compare_order_amount_configure($settings) {
+ $form = array();
+
+ // Get the default website currency.
+ $default_currency = commerce_currency_load(NULL);
+
+ $form['operator'] = array(
+ '#type' => 'select',
+ '#title' => t('Operator'),
+ '#title_display' => 'invisible',
+ '#options' => array(
+ '<' => t('lower than'),
+ '==' => t('equals'),
+ '>' => t('greater than'),
+ ),
+ '#default_value' => !empty($settings['operator']) ? $settings['operator'] : '>=',
+ );
+
+ $form['total'] = array(
+ '#type' => 'container',
+ '#tree' => TRUE,
+ '#element_validate' => array('commerce_price_field_widget_validate'),
+ '#suffix' => '
' . t('Enter the minimum order amount to activate the discount.') . '
',
+ );
+
+ $form['total']['amount'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Order total'),
+ '#title_display' => 'invisible',
+ '#default_value' => commerce_currency_amount_to_decimal($settings['total']['amount'], $default_currency['code']),
+ '#size' => 10,
+ '#field_suffix' => $default_currency['code'],
+ '#require' => TRUE,
+ );
+
+ $form['total']['currency_code'] = array(
+ '#type' => 'value',
+ '#default_value' => $default_currency['code'],
+ );
+
+ return $form;
+}
+
+/**
+ * Configuration callback for commerce_order_has_owner.
+ *
+ * @param array $settings
+ * An array of rules condition settings.
+ *
+ * @return array;
+ * A form element array.
+ */
+function commerce_order_has_owner_configure($settings) {
+ $form = array();
+
+ $form['account'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Users'),
+ '#title_display' => 'invisible',
+ '#default_value' => ($account = user_load($settings['account'])) ? $account->name : '',
+ '#required' => TRUE,
+ '#autocomplete_path' => 'user/autocomplete',
+ '#element_validate' => array('_commerce_order_has_owner_validate'),
+ '#suffix' => '' . t('Discount is active if the selected user is the order owner.') . '
',
+ );
+
+ return $form;
+}
+
+/**
+ * Validation callback for a commerce_order_has_owner autocomplete element.
+ */
+function _commerce_order_has_owner_validate($element, &$form_state, $form) {
+ // Try to fetch user.
+ $user = user_load_by_name($element['#value']);
+
+ if (!empty($user)) {
+ $value = $user->uid;
+ form_set_value($element, $value, $form_state);
+ }
+ else {
+ form_set_error(implode('][', $element['#array_parents']), t('Enter a correct username.'));
+ }
+}
+
+/**
+ * Configuration callback for commerce_order_contains_products.
+ *
+ * @param array $settings
+ * Values for the form element.
+ *
+ * @return array
+ * Return a form element.
+ */
+function commerce_order_contains_products_configure($settings) {
+ // Get values from $settings.
+ $default_value = '';
+ foreach ($settings['products'] as $delta => $product_id) {
+ $product = commerce_product_load(reset($product_id));
+ $default_value .= $product->sku;
+ if ($product_id !== end($settings['products'])) {
+ $default_value .= ', ';
+ }
+ }
+
+ $form = array();
+
+ $form['products'] = array(
+ '#type' => 'textfield',
+ '#title' => t('SKUs'),
+ '#title_display' => 'invisible',
+ '#default_value' => $default_value,
+ '#required' => TRUE,
+ '#maxlength' => 4096,
+ '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
+ '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
+ '#suffix' => '' . t('Select products when ordered make discount active.') . '
',
+ '#attributes' => array('placeholder' => array(t('enter product name'))),
+ );
+
+ return $form;
+}
+
+/**
+ * Configure callback for commerce_order_has_specific_quantity_products.
+ *
+ * @param array $settings
+ * Values for the form element.
+ *
+ * @return array
+ * Return the form element to display.
+ */
+function commerce_order_has_specific_quantity_products_configure($settings) {
+ // Get product IDs from $settings.
+ $default_value = '';
+ foreach ($settings['products'] as $delta => $product_id) {
+ $product = commerce_product_load(reset($product_id));
+ $default_value .= $product->sku;
+ if ($product_id !== end($settings['products'])) {
+ $default_value .= ', ';
+ }
+ }
+
+ $form = array();
+
+ $form['products'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Products'),
+ '#title_display' => 'invisible',
+ '#default_value' => $default_value,
+ '#required' => TRUE,
+ '#maxlength' => 4096,
+ '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
+ '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
+ '#attributes' => array('placeholder' => array(t('enter product name'))),
+ );
+
+ $form['operator'] = array(
+ '#type' => 'select',
+ '#title' => t('Operator'),
+ '#title_display' => 'invisible',
+ '#options' => array(
+ '<' => t('quantity lower than'),
+ '==' => t('quantity equals'),
+ '>' => t('quantity greater than'),
+ ),
+ '#default_value' => !empty($settings['operator']) ? $settings['operator'] : '==',
+ );
+
+ $form['quantity'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Quantity'),
+ '#title_display' => 'invisible',
+ '#default_value' => !empty($settings['quantity']) ? $settings['quantity'] : '',
+ '#size' => 5,
+ '#required' => TRUE,
+ '#element_validate' => array('element_validate_integer'),
+ '#suffix' => '' . t('Enter product(s) and the quantity to be checked.') . '
',
+ );
+
+ return $form;
+}
+
+/**
+ * Configuration callback for commerce_shipping_compare_shipping_service.
+ *
+ * @param array $settings
+ * An array of rules condition settings.
+ *
+ * @return array;
+ * A form element array.
+ */
+function commerce_shipping_compare_shipping_service_configure($settings) {
+ $form = array();
+
+ $form['service'] = array(
+ '#type' => 'select',
+ '#multiple' => FALSE,
+ '#options' => commerce_shipping_service_options_list(),
+ '#title' => t('Shipping service'),
+ '#title_display' => 'invisible',
+ '#default_value' => !empty($settings['service']) ? $settings['service'] : FALSE,
+ '#require' => TRUE,
+ '#element_validate' => array('_commerce_shipping_compare_shipping_service_validate'),
+ '#suffix' => '' . t('The discount will be enable only if the shipping service is selected by customer.') . '
',
+ );
+
+ return $form;
+}
+
+/**
+ * Element validation callback.
+ *
+ * Ensures passed element is a valid commerce shipping service.
+ */
+function _commerce_shipping_compare_shipping_service_validate($element, &$form_state, $form) {
+ $shipping_services = commerce_shipping_services();
+
+ if (!array_key_exists($element['#value'], $shipping_services)) {
+ form_set_error(implode('][', $element['#array_parents']), t('A correct shipping service must be selected'));
+ }
+}
+
+/**
+ * Configuration callback for commerce_product_contains_products.
+ *
+ * @param array $settings
+ * Values for the form element.
+ *
+ * @return array
+ * Return a form element.
+ */
+function commerce_product_contains_products_configure($settings) {
+ $form = array();
+ $default_value = '';
+
+ if (!empty($settings)) {
+ foreach ($settings['sku'] as $delta => $product_id) {
+ $product = commerce_product_load(reset($product_id));
+ $default_value .= $product->sku;
+ if ($product_id !== end($settings['sku'])) {
+ $default_value .= ', ';
+ }
+ }
+ }
+
+ $form['sku'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Product title'),
+ '#title_display' => 'invisible',
+ '#default_value' => $default_value,
+ '#required' => TRUE,
+ '#maxlength' => 4096,
+ '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
+ '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
+ '#suffix' => '' . t('The discount is active if the line item is a product with the selected SKU.') . '
',
+ );
+
+ return $form;
+}
+
+/**
+ * Configuration callback for commerce_product_has_specified_terms on product.
+ *
+ * @param array $settings
+ * Values for the form element.
+ *
+ * @return array
+ * Return a form element.
+ */
+function commerce_product_has_specified_terms_configure($settings) {
+ $form = array();
+
+ $default_value = '';
+ if (!empty($settings['terms'])) {
+ foreach ($settings['terms'] as $delta => $term) {
+ $default_value .= taxonomy_term_load($term['target_id'])->name . ' (' . $term['target_id'] . ')';
+ if ($term != end($settings['terms'])) {
+ $default_value .= ', ';
+ }
+ }
+ }
+
+ $form['terms'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Terms'),
+ '#title_display' => 'invisible',
+ '#required' => TRUE,
+ '#maxlength' => 4096,
+ '#default_value' => $default_value,
+ '#autocomplete_path' => 'inline_conditions/autocomplete/taxonomy_term/1/0',
+ '#element_validate' => array('_inline_conditions_autocomplete_validate'),
+ '#suffix' => '' . t('The discount is active if the product has the selected term(s).') . '
',
+ );
+
+ return $form;
+}
diff --git a/inline_conditions.module b/inline_conditions.module
index d44555d..93d6ce0 100644
--- a/inline_conditions.module
+++ b/inline_conditions.module
@@ -4,19 +4,6 @@
* Manage frequently used conditions directly on the relevant form.
*/
-// Load local conditions files located into /modules folder.
-foreach (array('commerce_order', 'commerce_product') as $module) {
- if (module_exists($module)) {
- module_load_include('inc', 'inline_conditions', "modules/$module.inline_conditions");
- }
-}
-// Load conditions files implemented by other modules.
-foreach (inline_conditions_get_info_by_module() as $module => $condition) {
- if (module_exists($module)) {
- module_load_include('inc', $module, "$module.inline_conditions");
- }
-}
-
/**
* Implements hook_menu().
*/
@@ -59,6 +46,9 @@ function inline_conditions_hook_info() {
$hooks['inline_conditions_info_alter'] = array(
'group' => 'inline_conditions',
);
+ $hooks['inline_conditions_build_alter'] = array(
+ 'group' => 'inline_conditions',
+ );
return $hooks;
}
@@ -389,6 +379,12 @@ function inline_conditions_field_prepare_items(&$items) {
* Implements hook_field_widget_form().
*/
function inline_conditions_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
+ // Ensure this .inline_conditions files are loaded when the form is rebuilt
+ // from the cache.
+ foreach (inline_conditions_get_info_by_module() as $module => $condition) {
+ $form_state['build_info']['files']["form_ic_$module"] = drupal_get_path('module', $module) . "/$module.inline_conditions.inc";
+ }
+
// Get entity_type setting from the current instance.
$entity_type_setting = empty($instance['settings']['entity_type']) ? 'commerce_order' : $instance['settings']['entity_type'];
diff --git a/inline_conditions.rules.inc b/inline_conditions.rules.inc
new file mode 100644
index 0000000..e2ea5ef
--- /dev/null
+++ b/inline_conditions.rules.inc
@@ -0,0 +1,386 @@
+ t('Order amount comparison'),
+ 'parameter' => array(
+ 'commerce_order' => array(
+ 'type' => 'commerce_order',
+ 'label' => t('Order'),
+ 'description' => t('The order.'),
+ 'wrapped' => TRUE,
+ ),
+ 'operator' => array(
+ 'type' => 'text',
+ 'label' => t('Operator'),
+ 'description' => t('The operator used with the order amount value below.'),
+ 'default value' => '>=',
+ 'options list' => '_inline_conditions_order_operator_options',
+ ),
+ 'total' => array(
+ 'type' => 'commerce_price',
+ 'label' => t('Order amount'),
+ 'default value' => '',
+ 'description' => t('The value to compare against the passed order amount.'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Order'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_order_compare_order_amount']['callbacks']['build'],
+ ),
+ );
+
+ $conditions['commerce_order_has_owner'] = array(
+ 'label' => t('Order owner'),
+ 'parameter' => array(
+ 'commerce_order' => array(
+ 'type' => 'commerce_order',
+ 'label' => t('Order'),
+ 'description' => t('The order.'),
+ 'wrapped' => TRUE,
+ ),
+ 'account' => array(
+ 'type' => 'user',
+ 'label' => t('User'),
+ 'description' => t('User account.'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Order'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_order_has_owner']['callbacks']['build'],
+ ),
+ );
+
+ $conditions['commerce_order_contains_products'] = array(
+ 'label' => t('Order contains products'),
+ 'parameter' => array(
+ 'commerce_order' => array(
+ 'type' => 'commerce_order',
+ 'label' => t('Order'),
+ 'description' => t('The order.'),
+ 'wrapped' => TRUE,
+ ),
+ 'products' => array(
+ 'type' => 'text',
+ 'label' => t('Product SKU(s)'),
+ 'description' => t('Products SKU to look for on the order. Enter a comma-separated list of product SKU(s).'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Order'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_order_contains_products']['callbacks']['build'],
+ ),
+ );
+
+ $conditions['commerce_order_has_specific_quantity_products'] = array(
+ 'label' => t('Order has a specific quantity of products'),
+ 'parameter' => array(
+ 'commerce_order' => array(
+ 'type' => 'commerce_order',
+ 'label' => t('Order'),
+ 'description' => t('The order.'),
+ 'wrapped' => TRUE,
+ ),
+ 'products' => array(
+ 'type' => 'text',
+ 'label' => t('Product SKU(s)'),
+ 'description' => t('Products SKU to look for on the order. Enter a comma-separated list of product SKU(s).'),
+ ),
+ 'operator' => array(
+ 'type' => 'text',
+ 'label' => t('Operator'),
+ 'description' => t('The operator used with the product quantity value below.'),
+ 'default value' => '>=',
+ 'options list' => '_inline_conditions_order_operator_options',
+ ),
+ 'quantity' => array(
+ 'type' => 'integer',
+ 'label' => t('Quantity'),
+ 'description' => t('Quantity value to be compared against each selected product(s).'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Order'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_order_has_specific_quantity_products']['callbacks']['build'],
+ ),
+ );
+ }
+
+ if (module_exists('commerce_product')) {
+ $conditions['commerce_product_contains_products'] = array(
+ 'label' => t('Line item contains a specific product'),
+ 'parameter' => array(
+ 'commerce_line_item' => array(
+ 'type' => 'commerce_line_item',
+ 'label' => t('Line item'),
+ 'description' => t('The line item.'),
+ 'wrapped' => TRUE,
+ ),
+ 'sku' => array(
+ 'type' => 'text',
+ 'label' => t('SKU'),
+ 'description' => t('Enter a comma-separated list of product SKU(s) to compare against the passed product line item.'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Line Item'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_product_contains_products']['callbacks']['build'],
+ ),
+ );
+ }
+
+ if (module_exists('taxonomy') && module_exists('commerce_product')) {
+ $conditions['commerce_product_has_specified_terms'] = array(
+ 'label' => t('Line item product contains specific terms ID'),
+ 'parameter' => array(
+ 'commerce_line_item' => array(
+ 'type' => 'commerce_line_item',
+ 'label' => t('Line item'),
+ 'description' => t('The line item.'),
+ 'wrapped' => TRUE,
+ ),
+ 'terms' => array(
+ 'type' => 'text',
+ 'label' => t('Terms ID'),
+ 'description' => t('Enter a comma-separated list of term ID to compare against the passed product line item.'),
+ ),
+ ),
+ 'module' => 'inline_conditions',
+ 'group' => t('Commerce Line Item'),
+ 'callbacks' => array(
+ 'execute' => $inline_conditions['commerce_product_has_specified_terms']['callbacks']['build'],
+ ),
+ );
+ }
+
+ return $conditions;
+}
+
+/**
+ * Build callback for commerce_order_compare_order_amount.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * The wrapped entity given by the rule.
+ * @param string $operator
+ * The comparison operator.
+ * @param array $total
+ * A commerce_price type array.
+ *
+ * @return bool
+ * return true if condition is valid. false otherwise.
+ */
+function commerce_order_compare_order_amount_build(EntityDrupalWrapper $wrapper, $operator, $total) {
+ // Get given total order amount.
+ $total_order = $wrapper->commerce_order_total->value();
+
+ // Ensures currency codes match.
+ if ($total['currency_code'] != $total_order['currency_code']) {
+ return FALSE;
+ }
+
+ switch ($operator) {
+ case '<':
+ return $total_order['amount'] < $total['amount'];
+
+ case '==':
+ return $total_order['amount'] == $total['amount'];
+
+ case '>':
+ return $total_order['amount'] > $total['amount'];
+
+ default:
+ return FALSE;
+ }
+}
+
+/**
+ * Build callback for commerce_order_has_owner.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * The wrapped entity given by the rule.
+ * @param array $account
+ * A fully loaded drupal user.
+ *
+ * @return bool
+ * Returns true if condition is valid. false otherwise.
+ */
+function commerce_order_has_owner_build(EntityDrupalWrapper $wrapper, $account) {
+ if (isset($account->uid)) {
+ // If current logged user matches the discount related users.
+ return $account->uid == $wrapper->uid->value();
+ }
+
+ return FALSE;
+}
+
+/**
+ * Build callback for commerce_order_contains_products.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * The wrapped entity given by the rule
+ * @param string $products
+ * A list of products SKU.
+ *
+ * @return bool
+ * Returns True if condition is valid. False otherwise.
+ */
+function commerce_order_contains_products_build(EntityDrupalWrapper $wrapper, $products) {
+
+ $products_sku = explode(', ', (string) $products);
+
+ foreach ($wrapper->commerce_line_items as $wrapper_line_item) {
+ // Ensures the passed line item is a product.
+ if (in_array('commerce_product', array_keys($wrapper_line_item->getPropertyInfo()))) {
+ if (($key = array_search($wrapper_line_item->commerce_product->sku->value(), $products_sku)) !== FALSE) {
+ unset($products_sku[$key]);
+ }
+ }
+ }
+
+ return empty($products_sku);
+}
+
+/**
+ * Build callback for inline_conditions_product_quantity.
+ *
+ * Checks if every order line item match the quantity comparison defined in the
+ * rule settings.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * Wrapped entity given by the rule.
+ * @param string $products
+ * A list of products SKU.
+ * @param string $operator
+ * String operator used to compare product quantity.
+ * @param int $quantity
+ * Product quantity.
+ *
+ * @return bool
+ * True if the condition is valid. False otherwise.
+ */
+function commerce_order_has_specific_quantity_products_build(EntityDrupalWrapper $wrapper, $products, $operator, $quantity) {
+ $products_sku = explode(', ', (string) $products);
+ // Loop on order line items to check if each product has the quantity
+ // specified in the rule settings.
+ foreach ($wrapper->commerce_line_items as $wrapper_line_item) {
+ if (in_array('commerce_product', array_keys($wrapper_line_item->getPropertyInfo()))) {
+ if (($key = array_search($wrapper_line_item->commerce_product->sku->value(), $products_sku)) !== FALSE) {
+ // At this point, we are sure that the current product is in the order.
+ // If this product line item doesn't meet the quantity comparison, the
+ // condition will return false.
+ switch ($operator) {
+ case '<':
+ if ($wrapper_line_item->quantity->value() < $quantity) {
+ unset($products_sku[$key]);
+ }
+ else {
+ return FALSE;
+ }
+ break;
+
+ case '==':
+ if ($wrapper_line_item->quantity->value() == $quantity) {
+ unset($products_sku[$key]);
+ }
+ else {
+ return FALSE;
+ }
+ break;
+
+ case '>':
+ if ($wrapper_line_item->quantity->value() > $quantity) {
+ unset($products_sku[$key]);
+ }
+ else {
+ return FALSE;
+ }
+ break;
+
+ }
+ }
+ }
+ }
+
+ return empty($products_sku);
+}
+
+/**
+ * Build callback for commerce_product_contains_products.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * Wrapped entity type given by the rule.
+ * @param string $sku
+ * Product sku(s) returned by rule condition.
+ *
+ * @return bool
+ * True if condition is valid. false otherwise.
+ */
+function commerce_product_contains_products_build(EntityDrupalWrapper $wrapper, $sku) {
+ // Only for Line items with Product reference field.
+ if (in_array('commerce_product', array_keys($wrapper->getPropertyInfo()))) {
+ return in_array($wrapper->commerce_product->sku->value(), array_map('trim', explode(',', $sku)));
+ }
+ return FALSE;
+}
+
+/**
+ * Build callback for commerce_product_has_specified_terms on product.
+ *
+ * @param EntityDrupalWrapper $wrapper
+ * Wrapped entity type given by the rule.
+ * @param array $terms
+ * Values for the condition settings.
+ *
+ * @return bool
+ * True is condition is valid. false otherwise.
+ */
+function commerce_product_has_specified_terms_build(EntityDrupalWrapper $wrapper, $terms) {
+ $terms_name = explode(', ', $terms);
+
+ if (in_array('commerce_product', array_keys($wrapper->getPropertyInfo()))) {
+ // Fetch all the fields name of taxonomy_term type for the passed entity.
+ foreach ($wrapper->commerce_product->getPropertyInfo() as $field_name => $property) {
+ if (preg_match('/taxonomy_term/', $property['type'])) {
+ // If the wrapped field is an instance of EntityListWrapper class, that
+ // means that field contains multiple values.
+ if ($wrapper->commerce_product->$field_name instanceof EntityListWrapper) {
+ foreach ($wrapper->commerce_product->$field_name as $wrapper_term) {
+ if (($key = array_search($wrapper_term->getIdentifier(), $terms_name)) !== FALSE) {
+ unset($terms_name[$key]);
+ }
+ }
+ }
+ elseif ($term = $wrapper->commerce_product->$field_name->value()) {
+ if (($key = array_search($term->tid, $terms_name)) !== FALSE) {
+ unset($terms_name[$key]);
+ }
+ }
+ }
+ }
+ }
+
+ return empty($terms_name);
+}
+
+/**
+ * Options list callback for condition commerce_order_compare_order_amount.
+ */
+function _inline_conditions_order_operator_options() {
+ return array(
+ '<' => t('lower than'),
+ '==' => t('equals'),
+ '>' => t('greater than'),
+ );
+}
diff --git a/modules/commerce_order.inline_conditions.inc b/modules/commerce_order.inline_conditions.inc
deleted file mode 100644
index 6aefe5a..0000000
--- a/modules/commerce_order.inline_conditions.inc
+++ /dev/null
@@ -1,599 +0,0 @@
- t('Amount'),
- 'entity type' => 'commerce_order',
- 'callbacks' => array(
- 'configure' => 'commerce_order_compare_order_amount_configure',
- 'build' => 'commerce_order_compare_order_amount_build',
- ),
- );
-
- $conditions['commerce_order_has_owner'] = array(
- 'label' => t('User'),
- 'entity type' => 'commerce_order',
- 'callbacks' => array(
- 'configure' => 'commerce_order_has_owner_configure',
- 'build' => 'commerce_order_has_owner_build',
- ),
- );
-
- $conditions['commerce_order_contains_products'] = array(
- 'label' => t('Product(s)'),
- 'entity type' => 'commerce_order',
- 'callbacks' => array(
- 'configure' => 'commerce_order_contains_products_configure',
- 'build' => 'commerce_order_contains_products_build',
- ),
- );
-
- $conditions['commerce_order_has_specific_quantity_products'] = array(
- 'label' => t('Product(s) and quantity'),
- 'entity type' => 'commerce_order',
- 'callbacks' => array(
- 'configure' => 'commerce_order_has_specific_quantity_products_configure',
- 'build' => 'commerce_order_has_specific_quantity_products_build',
- ),
- );
-
- if (module_exists('commerce_shipping')) {
- $conditions['commerce_shipping_compare_shipping_service'] = array(
- 'label' => t('Shipping service'),
- 'entity type' => 'commerce_order',
- 'callbacks' => array(
- 'configure' => 'commerce_shipping_compare_shipping_service_configure',
- 'build' => 'commerce_shipping_rules_line_item_exists',
- ),
- );
- }
-
- return $conditions;
-}
-
-
-/**
- * Implements hook_inline_conditions_build_alter().
- */
-function commerce_order_inline_conditions_build_alter(&$value) {
- switch ($value['condition_name']) {
- case 'commerce_order_contains_products':
- case 'commerce_order_has_specific_quantity_products':
- $entity_ids = array();
- foreach ($value['condition_settings']['products'] as $delta) {
- $entity_ids[] = reset($delta);
- }
- $products = commerce_product_load_multiple($entity_ids);
-
- $value['condition_settings']['products'] = '';
- foreach ($products as $product) {
- $value['condition_settings']['products'] .= $product->sku;
- if ($product !== end($products)) {
- $value['condition_settings']['products'] .= ', ';
- }
- }
- break;
- }
-}
-
-/**
- * Configuration callback for commerce_order_compare_order_amount.
- *
- * @param array $settings
- * An array of rules condition settings.
- *
- * @return array;
- * A form element array.
- */
-function commerce_order_compare_order_amount_configure($settings) {
- $form = array();
-
- // Get the default website currency.
- $default_currency = commerce_currency_load(NULL);
-
- $form['operator'] = array(
- '#type' => 'select',
- '#title' => t('Operator'),
- '#title_display' => 'invisible',
- '#options' => array(
- '<' => t('lower than'),
- '==' => t('equals'),
- '>' => t('greater than'),
- ),
- '#default_value' => !empty($settings['operator']) ? $settings['operator'] : '>=',
- );
-
- $form['total'] = array(
- '#type' => 'container',
- '#tree' => TRUE,
- '#element_validate' => array('commerce_price_field_widget_validate'),
- '#suffix' => '' . t('Enter the minimum order amount to activate the discount.') . '
',
- );
-
- $form['total']['amount'] = array(
- '#type' => 'textfield',
- '#title' => t('Order total'),
- '#title_display' => 'invisible',
- '#default_value' => commerce_currency_amount_to_decimal($settings['total']['amount'], $default_currency['code']),
- '#size' => 10,
- '#field_suffix' => $default_currency['code'],
- '#require' => TRUE,
- );
-
- $form['total']['currency_code'] = array(
- '#type' => 'value',
- '#default_value' => $default_currency['code'],
- );
-
- return $form;
-}
-
-
-/**
- * Build callback for commerce_order_compare_order_amount.
- *
- * @param EntityDrupalWrapper $wrapper
- * The wrapped entity given by the rule.
- * @param string $operator
- * The comparison operator.
- * @param array $total
- * A commerce_price type array.
- *
- * @return bool
- * return true if condition is valid. false otherwise.
- */
-function commerce_order_compare_order_amount_build(EntityDrupalWrapper $wrapper, $operator, $total) {
- // Get given total order amount.
- $total_order = $wrapper->commerce_order_total->value();
-
- // Ensures currency codes match.
- if ($total['currency_code'] != $total_order['currency_code']) {
- return FALSE;
- }
-
- switch ($operator) {
- case '<':
- return $total_order['amount'] < $total['amount'];
-
- case '==':
- return $total_order['amount'] == $total['amount'];
-
- case '>':
- return $total_order['amount'] > $total['amount'];
-
- default:
- return FALSE;
- }
-}
-
-/**
- * Configuration callback for commerce_order_has_owner.
- *
- * @param array $settings
- * An array of rules condition settings.
- *
- * @return array;
- * A form element array.
- */
-function commerce_order_has_owner_configure($settings) {
- $form = array();
-
- $form['account'] = array(
- '#type' => 'textfield',
- '#title' => t('Users'),
- '#title_display' => 'invisible',
- '#default_value' => ($account = user_load($settings['account'])) ? $account->name : '',
- '#required' => TRUE,
- '#autocomplete_path' => 'user/autocomplete',
- '#element_validate' => array('_commerce_order_has_owner_validate'),
- '#suffix' => '' . t('Discount is active if the selected user is the order owner.') . '
',
- );
-
- return $form;
-}
-
-
-/**
- * Validation callback for a commerce_order_has_owner autocomplete element.
- */
-function _commerce_order_has_owner_validate($element, &$form_state, $form) {
- // Try to fetch user.
- $user = user_load_by_name($element['#value']);
-
- if (!empty($user)) {
- $value = $user->uid;
- form_set_value($element, $value, $form_state);
- }
- else {
- form_set_error(implode('][', $element['#array_parents']), t('Enter a correct username.'));
- }
-}
-
-/**
- * Build callback for commerce_order_has_owner.
- *
- * @param EntityDrupalWrapper $wrapper
- * The wrapped entity given by the rule.
- * @param array $account
- * A fully loaded drupal user.
- *
- * @return bool
- * Returns true if condition is valid. false otherwise.
- */
-function commerce_order_has_owner_build(EntityDrupalWrapper $wrapper, $account) {
- if (isset($account->uid)) {
- // If current logged user matches the discount related users.
- return $account->uid == $wrapper->uid->value();
- }
-
- return FALSE;
-}
-
-/**
- * Configuration callback for commerce_order_contains_products.
- *
- * @param array $settings
- * Values for the form element.
- *
- * @return array
- * Return a form element.
- */
-function commerce_order_contains_products_configure($settings) {
- // Get values from $settings.
- $default_value = '';
- foreach ($settings['products'] as $delta => $product_id) {
- $product = commerce_product_load(reset($product_id));
- $default_value .= $product->sku;
- if ($product_id !== end($settings['products'])) {
- $default_value .= ', ';
- }
- }
-
- $form = array();
-
- $form['products'] = array(
- '#type' => 'textfield',
- '#title' => t('SKUs'),
- '#title_display' => 'invisible',
- '#default_value' => $default_value,
- '#required' => TRUE,
- '#maxlength' => 4096,
- '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
- '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
- '#suffix' => '' . t('Select products when ordered make discount active.') . '
',
- '#attributes' => array('placeholder' => array(t('enter product name'))),
- );
-
- return $form;
-}
-
-/**
- * Build callback for commerce_order_contains_products.
- *
- * @param EntityDrupalWrapper $wrapper
- * The wrapped entity given by the rule
- * @param string $products
- * A list of products SKU.
- *
- * @return bool
- * Returns True if condition is valid. False otherwise.
- */
-function commerce_order_contains_products_build(EntityDrupalWrapper $wrapper, $products) {
-
- $products_sku = explode(', ', (string) $products);
-
- foreach ($wrapper->commerce_line_items as $wrapper_line_item) {
- // Ensures the passed line item is a product.
- if (in_array('commerce_product', array_keys($wrapper_line_item->getPropertyInfo()))) {
- if (($key = array_search($wrapper_line_item->commerce_product->sku->value(), $products_sku)) !== FALSE) {
- unset($products_sku[$key]);
- }
- }
- }
-
- return empty($products_sku);
-}
-
-/**
- * Configure callback for commerce_order_has_specific_quantity_products.
- *
- * @param array $settings
- * Values for the form element.
- *
- * @return array
- * Return the form element to display.
- */
-function commerce_order_has_specific_quantity_products_configure($settings) {
- // Get product IDs from $settings.
- $default_value = '';
- foreach ($settings['products'] as $delta => $product_id) {
- $product = commerce_product_load(reset($product_id));
- $default_value .= $product->sku;
- if ($product_id !== end($settings['products'])) {
- $default_value .= ', ';
- }
- }
-
- $form = array();
-
- $form['products'] = array(
- '#type' => 'textfield',
- '#title' => t('Products'),
- '#title_display' => 'invisible',
- '#default_value' => $default_value,
- '#required' => TRUE,
- '#maxlength' => 4096,
- '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
- '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
- '#attributes' => array('placeholder' => array(t('enter product name'))),
- );
-
- $form['operator'] = array(
- '#type' => 'select',
- '#title' => t('Operator'),
- '#title_display' => 'invisible',
- '#options' => array(
- '<' => t('quantity lower than'),
- '==' => t('quantity equals'),
- '>' => t('quantity greater than'),
- ),
- '#default_value' => !empty($settings['operator']) ? $settings['operator'] : '==',
- );
-
- $form['quantity'] = array(
- '#type' => 'textfield',
- '#title' => t('Quantity'),
- '#title_display' => 'invisible',
- '#default_value' => !empty($settings['quantity']) ? $settings['quantity'] : '',
- '#size' => 5,
- '#required' => TRUE,
- '#element_validate' => array('element_validate_integer'),
- '#suffix' => '' . t('Enter product(s) and the quantity to be checked.') . '
',
- );
-
- return $form;
-}
-
-/**
- * Build callback for inline_conditions_product_quantity.
- *
- * Checks if every order line item match the quantity comparison defined in the
- * rule settings.
- *
- * @param EntityDrupalWrapper $wrapper
- * Wrapped entity given by the rule.
- * @param string $products
- * A list of products SKU.
- * @param string $operator
- * String operator used to compare product quantity.
- * @param int $quantity
- * Product quantity.
- *
- * @return bool
- * True if the condition is valid. False otherwise.
- */
-function commerce_order_has_specific_quantity_products_build(EntityDrupalWrapper $wrapper, $products, $operator, $quantity) {
- $products_sku = explode(', ', (string) $products);
- // Loop on order line items to check if each product has the quantity
- // specified in the rule settings.
- foreach ($wrapper->commerce_line_items as $wrapper_line_item) {
- if (in_array('commerce_product', array_keys($wrapper_line_item->getPropertyInfo()))) {
- if (($key = array_search($wrapper_line_item->commerce_product->sku->value(), $products_sku)) !== FALSE) {
- // At this point, we are sure that the current product is in the order.
- // If this product line item doesn't meet the quantity comparison, the
- // condition will return false.
- switch ($operator) {
- case '<':
- if ($wrapper_line_item->quantity->value() < $quantity) {
- unset($products_sku[$key]);
- }
- else {
- return FALSE;
- }
- break;
-
- case '==':
- if ($wrapper_line_item->quantity->value() == $quantity) {
- unset($products_sku[$key]);
- }
- else {
- return FALSE;
- }
- break;
-
- case '>':
- if ($wrapper_line_item->quantity->value() > $quantity) {
- unset($products_sku[$key]);
- }
- else {
- return FALSE;
- }
- break;
-
- }
- }
- }
- }
-
- return empty($products_sku);
-}
-
-/**
- * Configuration callback for commerce_shipping_compare_shipping_service.
- *
- * @param array $settings
- * An array of rules condition settings.
- *
- * @return array;
- * A form element array.
- */
-function commerce_shipping_compare_shipping_service_configure($settings) {
- $form = array();
-
- $form['service'] = array(
- '#type' => 'select',
- '#multiple' => FALSE,
- '#options' => commerce_shipping_service_options_list(),
- '#title' => t('Shipping service'),
- '#title_display' => 'invisible',
- '#default_value' => !empty($settings['service']) ? $settings['service'] : FALSE,
- '#require' => TRUE,
- '#element_validate' => array('_commerce_shipping_compare_shipping_service_validate'),
- '#suffix' => '' . t('The discount will be enable only if the shipping service is selected by customer.') . '
',
- );
-
- return $form;
-}
-
-/**
- * Element validation callback.
- *
- * Ensures passed element is a valid commerce shipping service.
- */
-function _commerce_shipping_compare_shipping_service_validate($element, &$form_state, $form) {
- $shipping_services = commerce_shipping_services();
-
- if (!array_key_exists($element['#value'], $shipping_services)) {
- form_set_error(implode('][', $element['#array_parents']), t('A correct shipping service must be selected'));
- }
-}
-
-/**
- * Implements hook_rules_condition_info_alter().
- *
- * Adds new rule conditions to commerce_order entity type.
- */
-function commerce_order_rules_condition_info_alter(&$conditions) {
- $inline_conditions = inline_conditions_get_info();
-
- $conditions['commerce_order_compare_order_amount'] = array(
- 'label' => t('Order amount comparison'),
- 'parameter' => array(
- 'commerce_order' => array(
- 'type' => 'commerce_order',
- 'label' => t('Order'),
- 'description' => t('The order.'),
- 'wrapped' => TRUE,
- ),
- 'operator' => array(
- 'type' => 'text',
- 'label' => t('Operator'),
- 'description' => t('The operator used with the order amount value below.'),
- 'default value' => '>=',
- 'options list' => '_inline_conditions_order_operator_options',
- ),
- 'total' => array(
- 'type' => 'commerce_price',
- 'label' => t('Order amount'),
- 'default value' => '',
- 'description' => t('The value to compare against the passed order amount.'),
- ),
- ),
- 'module' => 'commerce_order',
- 'group' => t('Commerce Order'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_order_compare_order_amount']['callbacks']['build'],
- ),
- );
-
- $conditions['commerce_order_has_owner'] = array(
- 'label' => t('Order owner'),
- 'parameter' => array(
- 'commerce_order' => array(
- 'type' => 'commerce_order',
- 'label' => t('Order'),
- 'description' => t('The order.'),
- 'wrapped' => TRUE,
- ),
- 'account' => array(
- 'type' => 'user',
- 'label' => t('User'),
- 'description' => t('User account.'),
- ),
- ),
- 'module' => 'commerce_order',
- 'group' => t('Commerce Order'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_order_has_owner']['callbacks']['build'],
- ),
- );
-
- $conditions['commerce_order_contains_products'] = array(
- 'label' => t('Order contains products'),
- 'parameter' => array(
- 'commerce_order' => array(
- 'type' => 'commerce_order',
- 'label' => t('Order'),
- 'description' => t('The order.'),
- 'wrapped' => TRUE,
- ),
- 'products' => array(
- 'type' => 'text',
- 'label' => t('Product SKU(s)'),
- 'description' => t('Products SKU to look for on the order. Enter a comma-separated list of product SKU(s).'),
- ),
- ),
- 'module' => 'commerce_order',
- 'group' => t('Commerce Order'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_order_contains_products']['callbacks']['build'],
- ),
- );
-
- $conditions['commerce_order_has_specific_quantity_products'] = array(
- 'label' => t('Order has a specific quantity of products'),
- 'parameter' => array(
- 'commerce_order' => array(
- 'type' => 'commerce_order',
- 'label' => t('Order'),
- 'description' => t('The order.'),
- 'wrapped' => TRUE,
- ),
- 'products' => array(
- 'type' => 'text',
- 'label' => t('Product SKU(s)'),
- 'description' => t('Products SKU to look for on the order. Enter a comma-separated list of product SKU(s).'),
- ),
- 'operator' => array(
- 'type' => 'text',
- 'label' => t('Operator'),
- 'description' => t('The operator used with the product quantity value below.'),
- 'default value' => '>=',
- 'options list' => '_inline_conditions_order_operator_options',
- ),
- 'quantity' => array(
- 'type' => 'integer',
- 'label' => t('Quantity'),
- 'description' => t('Quantity value to be compared against each selected product(s).'),
- ),
- ),
- 'module' => 'commerce_order',
- 'group' => t('Commerce Order'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_order_has_specific_quantity_products']['callbacks']['build'],
- ),
- );
-}
-
-/**
- * Options list callback for condition commerce_order_compare_order_amount.
- */
-function _inline_conditions_order_operator_options() {
- return array(
- '<' => t('lower than'),
- '==' => t('equals'),
- '>' => t('greater than'),
- );
-}
diff --git a/modules/commerce_product.inline_conditions.inc b/modules/commerce_product.inline_conditions.inc
deleted file mode 100644
index dff34b4..0000000
--- a/modules/commerce_product.inline_conditions.inc
+++ /dev/null
@@ -1,258 +0,0 @@
- t('Product(s)'),
- 'entity type' => 'commerce_line_item',
- 'callbacks' => array(
- 'configure' => 'commerce_product_contains_products_configure',
- 'build' => 'commerce_product_contains_products_build',
- ),
- );
-
- if (module_exists('taxonomy')) {
- $conditions['commerce_product_has_specified_terms'] = array(
- 'label' => t('Product attributes'),
- 'entity type' => 'commerce_line_item',
- 'callbacks' => array(
- 'configure' => 'commerce_product_has_specified_terms_configure',
- 'build' => 'commerce_product_has_specified_terms_build',
- ),
- );
- }
-
- return $conditions;
-}
-
-/**
- * Implements hook_inline_conditions_build_alter().
- */
-function commerce_product_inline_conditions_build_alter(&$value) {
- switch ($value['condition_name']) {
- case 'commerce_product_has_specified_terms':
- $terms = $value['condition_settings']['terms'];
-
- $value['condition_settings']['terms'] = '';
- foreach ($terms as $term) {
- $value['condition_settings']['terms'] .= reset($term);
- if ($term !== end($terms)) {
- $value['condition_settings']['terms'] .= ', ';
- }
- }
- break;
-
- case 'commerce_product_contains_products':
- $entity_ids = array();
- foreach ($value['condition_settings']['sku'] as $delta) {
- $entity_ids[] = reset($delta);
- }
- $products = commerce_product_load_multiple($entity_ids);
-
- $value['condition_settings']['sku'] = '';
- foreach ($products as $product) {
- $value['condition_settings']['sku'] .= $product->sku;
- if ($product !== end($products)) {
- $value['condition_settings']['sku'] .= ', ';
- }
- }
- break;
-
- }
-}
-
-/**
- * Configuration callback for commerce_product_contains_products.
- *
- * @param array $settings
- * Values for the form element.
- *
- * @return array
- * Return a form element.
- */
-function commerce_product_contains_products_configure($settings) {
- $form = array();
- $default_value = '';
-
- if (!empty($settings)) {
- foreach ($settings['sku'] as $delta => $product_id) {
- $product = commerce_product_load(reset($product_id));
- $default_value .= $product->sku;
- if ($product_id !== end($settings['sku'])) {
- $default_value .= ', ';
- }
- }
- }
-
- $form['sku'] = array(
- '#type' => 'textfield',
- '#title' => t('Product title'),
- '#title_display' => 'invisible',
- '#default_value' => $default_value,
- '#required' => TRUE,
- '#maxlength' => 4096,
- '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/0/0',
- '#element_validate' => array('commerce_product_reference_autocomplete_validate'),
- '#suffix' => '' . t('The discount is active if the line item is a product with the selected SKU.') . '
',
- );
-
- return $form;
-}
-
-/**
- * Build callback for commerce_product_contains_products.
- *
- * @param EntityDrupalWrapper $wrapper
- * Wrapped entity type given by the rule.
- * @param string $sku
- * Product sku(s) returned by rule condition.
- *
- * @return bool
- * True if condition is valid. false otherwise.
- */
-function commerce_product_contains_products_build(EntityDrupalWrapper $wrapper, $sku) {
- // Only for Line items with Product reference field.
- if (in_array('commerce_product', array_keys($wrapper->getPropertyInfo()))) {
- return in_array($wrapper->commerce_product->sku->value(), array_map('trim', explode(',', $sku)));
- }
- return FALSE;
-}
-
-/**
- * Configuration callback for commerce_product_has_specified_terms on product.
- *
- * @param array $settings
- * Values for the form element.
- *
- * @return array
- * Return a form element.
- */
-function commerce_product_has_specified_terms_configure($settings) {
- $form = array();
-
- $default_value = '';
- if (!empty($settings['terms'])) {
- foreach ($settings['terms'] as $delta => $term) {
- $default_value .= taxonomy_term_load($term['target_id'])->name . ' (' . $term['target_id'] . ')';
- if ($term != end($settings['terms'])) {
- $default_value .= ', ';
- }
- }
- }
-
- $form['terms'] = array(
- '#type' => 'textfield',
- '#title' => t('Terms'),
- '#title_display' => 'invisible',
- '#required' => TRUE,
- '#maxlength' => 4096,
- '#default_value' => $default_value,
- '#autocomplete_path' => 'inline_conditions/autocomplete/taxonomy_term/1/0',
- '#element_validate' => array('_inline_conditions_autocomplete_validate'),
- '#suffix' => '' . t('The discount is active if the product has the selected term(s).') . '
',
- );
-
- return $form;
-}
-
-/**
- * Build callback for commerce_product_has_specified_terms on product.
- *
- * @param EntityDrupalWrapper $wrapper
- * Wrapped entity type given by the rule.
- * @param array $terms
- * Values for the condition settings.
- *
- * @return bool
- * True is condition is valid. false otherwise.
- */
-function commerce_product_has_specified_terms_build(EntityDrupalWrapper $wrapper, $terms) {
- $terms_name = explode(', ', $terms);
-
- if (in_array('commerce_product', array_keys($wrapper->getPropertyInfo()))) {
- // Fetch all the fields name of taxonomy_term type for the passed entity.
- foreach ($wrapper->commerce_product->getPropertyInfo() as $field_name => $property) {
- if (preg_match('/taxonomy_term/', $property['type'])) {
- // If the wrapped field is an instance of EntityListWrapper class, that
- // means that field contains multiple values.
- if ($wrapper->commerce_product->$field_name instanceof EntityListWrapper) {
- foreach ($wrapper->commerce_product->$field_name as $wrapper_term) {
- if (($key = array_search($wrapper_term->getIdentifier(), $terms_name)) !== FALSE) {
- unset($terms_name[$key]);
- }
- }
- }
- elseif ($term = $wrapper->commerce_product->$field_name->value()) {
- if (($key = array_search($term->tid, $terms_name)) !== FALSE) {
- unset($terms_name[$key]);
- }
- }
- }
- }
- }
-
- return empty($terms_name);
-}
-
-/**
- * Implements hook_rules_condition_info_alter().
- *
- * Adds new rule conditions to commerce_line_item entity type.
- */
-function commerce_product_rules_condition_info_alter(&$conditions) {
- $inline_conditions = inline_conditions_get_info();
-
- $conditions['commerce_product_contains_products'] = array(
- 'label' => t('Line item contains a specific product'),
- 'parameter' => array(
- 'commerce_line_item' => array(
- 'type' => 'commerce_line_item',
- 'label' => t('Line item'),
- 'description' => t('The line item.'),
- 'wrapped' => TRUE,
- ),
- 'sku' => array(
- 'type' => 'text',
- 'label' => t('SKU'),
- 'description' => t('Enter a comma-separated list of product SKU(s) to compare against the passed product line item.'),
- ),
- ),
- 'module' => 'commerce_line_item',
- 'group' => t('Commerce Line Item'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_product_contains_products']['callbacks']['build'],
- ),
- );
-
- if (module_exists('taxonomy')) {
- $conditions['commerce_product_has_specified_terms'] = array(
- 'label' => t('Line item product contains specific terms ID'),
- 'parameter' => array(
- 'commerce_line_item' => array(
- 'type' => 'commerce_line_item',
- 'label' => t('Line item'),
- 'description' => t('The line item.'),
- 'wrapped' => TRUE,
- ),
- 'terms' => array(
- 'type' => 'text',
- 'label' => t('Terms ID'),
- 'description' => t('Enter a comma-separated list of term ID to compare against the passed product line item.'),
- ),
- ),
- 'module' => 'commerce_line_item',
- 'group' => t('Commerce Line Item'),
- 'callbacks' => array(
- 'execute' => $inline_conditions['commerce_product_has_specified_terms']['callbacks']['build'],
- ),
- );
- }
-}