2

I have products with 2 prices, the Commerce default one commerce_price, and a custom field field_price_with_flight. In some cases, depending on the node display term value of a taxonomy, I need to calculate and display the discount prices onto the custom price instead of default Commerce price.

If it can be achieved with rules, I'd appreciate a detailed answer because I'm not very comfortable with the use of rules, especially commerce ones.

How could I achieve that ?

EDIT :

I need to make it possible through discount module UI. I'll try to expose why with the business model below.

The site sells travels. Anytime, depending on sales, the owner decides to create new discounts, with parameters such as:

  • Limited time
  • A different discount value usually fixed amount but could be % sometimes
  • Applied on a selection of travels, choosed across discount UI whether per product or per category, with the help of Commerce Discount Product Category

The flexibility of Commerce Discount does the job most of the time. I could setup rules for one case, but this is just impossible to teach the site owner and sellers how to setup discount rules for each new one.

Now whenever you create a discount like that, The module creates a dedicated rule. Basic one exported below.

The challenge is that by default it will apply on commerce_price. I need to extend Discount module so that :

  • Or it detects the travel type has a field_price_with_flight value
  • Or we tell the module it is that case with a checkbox in the discount form for instance

and applies then the discount to field_price_with_flight.

Or, last, if possible, create a global rule that will overwrite each discount rule whenever necessary?

Basic rule created by discount

{ "commerce_discount_rule_discount_discount_example" : {
    "LABEL" : "Discount example",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-1",
    "OWNER" : "rules",
    "TAGS" : [ "Commerce Discount", "R\u00e9duction par produit" ],
    "REQUIRES" : [
      "commerce_discount",
      "commerce_discount_date",
      "commerce_product_reference"
    ],
    "ON" : { "commerce_product_calculate_sell_price" : [] },
    "IF" : [
      { "commerce_discount_compatibility_check" : {
          "commerce_order" : [ "commerce-line-item:order" ],
          "commerce_discount" : "discount_discount_example"
        }
      },
      { "commerce_discount_date_condition" : { "commerce_discount" : "discount_discount_example" } },
      { "commerce_product_contains_products" : { "sku" : "VE1201#2556#09-05-2016" } }
    ],
    "DO" : [
      { "commerce_discount_fixed_amount" : {
          "entity" : [ "commerce_line_item" ],
          "commerce_discount" : "discount_discount_example"
        }
      }
    ]
  }
}

2 Answers 2

4
+50

All you need is Rules :)

You can start with next steps:

  1. Visit Product pricing rules page: /admin/commerce/config/product-pricing
  2. Click + Add a pricing rule
  3. Enter Name. E.g. "Custom discount calculation"
  4. Click "Save"

After that you can program logic of "some cases, depending on the node display term value of a taxonomy" via rules. If you provide more details I can explain steps more precise.

From this point you should only add conditions and actions. There is a good module rules_conditional which allows you to add conditions in any place of your rule.

Some hints on Rules:

When you want to do some actions and check with field values, you have to check if the field exists first (that's a tricky part of rules).

E.g. if you want to check the value of the field_price_with_flight, you have to add 2 conditions before:

  1. Entity has field: Entity commerce-line-item, Field: commerce_product
  2. Entity has field: Entity commerce-line-item:commerce-product, Field: field_price_with_flight.

After these 2 actions you can do things with the field.

P.S. An alternative way for checking fields is to check the bundle of the entity.

If you need to do some price calculations you can use actions:

  • Data > Add variavle
  • Data > Calculate a value

Once you have finished your calculations, you can apply your price. Check the `Commerce line item' actions for unit price. You can multiply, divide, set unit price.

That is basically it. In this case you do not need the discount module.

Advanced

If you need some very custom calculations you can code a custom action for price calculation. There is a lot of examples if you try to search it. E.g. How To Create A Custom Rules Action

Useful links, videos:

7
  • thanks a lot this is a huge answer ! The fact is I need to start from Discount Module, simply because the end users of that site use it to setup their promotions, and will never perform the kind of config you suggest. So I have to extend Discount Module in a way that if the products discounted meet the right taxonomy conditions (travel value) , then the discount rule that is created automatically by the module will fire on the custom price. Could you update your answer to reflect that approach please ? Tks again
    – Kojo
    Mar 1, 2016 at 20:48
  • You should not generate rules every time. Better provide settings to end users like 'discount value' per user or product. Read those settings in rules and multiply product price by the discount value. Please add logic of your discount system, I will update the answer for you. Will the discount logic be different per product?
    – milkovsky
    Mar 1, 2016 at 21:04
  • Milkovsky, I edited the question to answer you. I hope it's clear enough ? Thanks a lot.
    – Kojo
    Mar 3, 2016 at 10:36
  • Well, I 'm confused, in fact it seems that the discounts applies correctly to a custom price field but in cart only, not in price display, be with any of the calculated displayed prices. I use Commerce Extra Price Formatters and there's an old related issue but it seems this was never solved. And if I simply choose raw amount/ show calculated price this does not show up neither...
    – Kojo
    Mar 3, 2016 at 20:04
  • The Comm Extra Price is indeed developed for cart and order extra prices, which affect total price of the order. E.g. price for a quick service or a special short term delivery.What you need is to change price of line item, that is done by pricing rules. I will try to find time later and update my answer with your new information about the Commerce Discount.
    – milkovsky
    Mar 6, 2016 at 10:00
0

You can use Rules. The rule should be something like Event: Calculate product sell price Conditions: On which conditions this rule should be evaluated Actions: Actions that should be done aka ( set commerce price to custom price ) etc.

For more advanced usage you can also use rules_conditional module.

1
  • Tks. I know Rules is the most common way to customize Discounts. But I could'nt find the way to do that. Could you develop the "set commerce price to custom price" part?
    – Kojo
    Nov 26, 2015 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.