2

Short: I have commerce product and commerce discount that affects this product's price. I want to presents details of this discount to users in shop.

Long story: I use Drupal Commerce with Drupal Commerce Discount which is based on Rules. Discount module is used for configuring promotions on products. Some of promotion (configured discounts) are time-limited ie. they ends on 11.11.2015. I would like to get information about discounts that affect product when displaying details. First of all I need to display information how much time is left before discount ends.

Is there any way to get information about active rules on product level?

1 Answer 1

2

On the line item, you have the field_unit_price field. Inside of that field's data attribute, you'll find information about the discounts that are applied to that product:

array(1) {
  ["components"]=>
  array(2) {
    ...
    [1]=>
    array(3) {
      ["name"]=>
      string(22) "discount|discount_test"
      ["price"]=>
      array(3) {
        ["amount"]=>
        float(-240)
        ["currency_code"]=>
        string(3) "USD"
        ["data"]=>
        array(2) {
          ["discount_name"]=>
          string(13) "discount_test"
          ["discount_component_title"]=>
          string(5) "Test"
        }
      }
      ["included"]=>
      bool(true)
    }
  }
}

Iterate over the components. If there is a discount applied to one of the items, you'll find one with a name that begins with discount|. Then you can then load that discount and grab more information about it:

$discount = entity_load('commerce_discount', array('discount_test'));

From here, if you've added dates, the date fields will be accessible on the field commerce_discount_date.

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.