Hi,

First off all thanks for the great module :)

Can we create coupons for selected product types only?

Say now I have cloths,brands,accessories...

I need not to have coupon for accessories.But for other products?

Can I do that using new Beta module or this feature will be developed in future?

Thanks

Comments

waltercat’s picture

Version: 7.x-1.0-beta7 » 7.x-1.x-dev
Category: feature » support

I am trying to do this as well. I only want the coupon to be used if a certain product type is in the cart. However when I take the default percentage rule and add a condition to look for a certain product type the rule breaks and I get this error in my rules error log:

9.158 ms Unable to get a data value. Error: Unable to get the data property type as the parent data structure is not set.
9.373 ms Unable to evaluate condition commerce_order_contains_product_type.

Not sure why it doesn't work, i am able to add this condition to fixed coupon price.

babruix’s picture

I have completed almost the same task, but for specific product ID, you can change my code to use product type instead:

/**
 * Implements hook_rules_action_info_alter().
 * @param $actions
 */
function YOURMODULE_rules_action_info_alter(&$actions) {
  $actions['commerce_coupon_pct_apply_to_product_line_item']['base'] = 'YOURMODULE_apply_pct_to_product_line_item';
}

/**
 * Alters percentage coupon applying rule
 */
function YOURMODULE_apply_pct_to_product_line_item($line_item, $coupon, $component_name, $round_mode) {
  $coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);

  if (!empty($coupon->field_coupon_product[LANGUAGE_NONE])) {
//change this line to your function:
    $products = YOURMODULE_get_coupon_allowed_products('field_coupon_product', $coupon_wrapper);
    if (count($products) > 0) {
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      if ($line_item->type == 'product' && isset($line_item_wrapper->commerce_product->product_id)) {
        $id = $line_item_wrapper->commerce_product->product_id->value();
        // Check if line item is not in coupon product field
        if (!isset($products[$id])) {
          // Do not apply coupon for line items that are not in allowed products list
          return;
        }
      }
    }
  }
  module_load_include('inc', 'commerce_coupon_pct', 'commerce_coupon_pct.rules');
  commerce_coupon_pct_apply_to_product_line_item($line_item, $coupon, $component_name, $round_mode);
}