Please add a default rule that will add coupon line items to the order object. Such a rule could look like that:

{ "rules_add_coupon_line_item" : {
      "LABEL" : "Add coupon line item",
      "PLUGIN" : "reaction rule",
      "REQUIRES" : [ "rules", "commerce_coupon" ],
      "ON" : [ "commerce_coupon_redeem" ],
      "IF" : [
        { "data_is" : {
            "data" : [ "coupon:type" ],
            "op" : "=",
            "value" : "commerce_coupon_pct"
          }
        },
        { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_percent_amount" } },
        { "NOT AND" : [
            { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-percent-amount" ] } }
          ]
        },
        { "entity_has_field" : {
            "entity" : [ "commerce_order" ],
            "field" : "commerce_coupon_order_reference"
          }
        },
        { "data_is" : {
            "data" : [ "coupon:commerce-coupon-percent-amount" ],
            "op" : "\u003E",
            "value" : 1
          }
        },
        { "data_is" : { "data" : [ "coupon:is-active" ], "op" : "=", "value" : true } }
      ],
      "DO" : [
        { "data_calc" : {
            "USING" : {
              "input_1" : [ "commerce-order:commerce-order-total:amount" ],
              "op" : "*",
              "input_2" : [ "coupon:commerce-coupon-percent-amount" ]
            },
            "PROVIDE" : { "result" : { "coupon_discount" : "Coupon Discount" } }
          }
        },
        { "data_calc" : {
            "USING" : { "input_1" : [ "coupon-discount" ], "op" : "\/", "input_2" : "100" },
            "PROVIDE" : { "result" : { "discount" : "Discount amount" } }
          }
        },
        { "commerce_coupon_action_create_coupon_line_item" : {
            "USING" : {
              "commerce_coupon" : [ "coupon" ],
              "commerce_order" : [ "commerce_order" ],
              "amount" : [ "discount" ],
              "component_name" : "commerce_coupon_pct_happyhour",
              "currency_code" : "[commerce-order:commerce-order-total:currency_code]"
            },
            "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
          }
        }
      ]
    }
  }

Unfortunately this rule will only work if you disable the default rule "Apply percentage coupons to product line item" in the rules configuration.

Comments

miro_dietiker’s picture

That makes sense to me. Having this covered by line items is the native way to deal with such price items in commerce...

Why is this not the suggested default use case?

pcambra’s picture

Project: Commerce Coupon (D7) » Commerce coupon percentage
Version: 7.x-1.0-beta6 » 7.x-1.x-dev
Category: task » feature

In any case, this request would belong to the percentage coupon module issue queue.

It could be a "mode" defined by settings, but certainly not the default behavior, but I'm afraid that the rule is not enough, as you need to take care of the refresh as well.

twiik’s picture

I agree with this, but like pcambra says you're missing some functionality and you're applying the coupon to the order total. It should only be applied to the product subtotal as before.

I think the default functionality for percentage coupons is very confusing and I'm currently doing what's necessary to have them function like fixed price coupons, ie. similar to how you've started here.

My main gripe with how percentage coupons function out of the box is that they apply directly to you subtotal and as the customer you're not really sure what happened. And because it's applied as a product pricing rule if you leave the checkout and go back to the product page it looks like the products themselves have been discounted in the meantime. It should be clear to the customer that he has applied an X percent coupon in the order summary. Something like this:

Subtotal: 1000,-
10% coupon: -100,-
Shipping: 100,-
Order total: 1000,-

Currently it looks like:

Subtotal: 900,-
Shipping: 100,-
Order total: 1000,-

And you're not really sure where your coupon went.

twiik’s picture

I had to use the rules conditional module to check if the line items in my loop where product line items, but here's my current, seemingly working, rule:

{ "rules_add_coupon_line_item" : {
    "LABEL" : "Redeem a coupon with percentage amount",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "rules_conditional", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_redeem" ],
    "IF" : [
      { "data_is" : {
          "data" : [ "coupon:type" ],
          "op" : "=",
          "value" : "commerce_coupon_pct"
        }
      },
      { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_percent_amount" } },
      { "entity_has_field" : {
          "entity" : [ "commerce_order" ],
          "field" : "commerce_coupon_order_reference"
        }
      },
      { "NOT AND" : [
          { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-percent-amount" ] } }
        ]
      },
      { "data_is" : {
          "data" : [ "coupon:commerce-coupon-percent-amount" ],
          "op" : "\u003E",
          "value" : "0"
        }
      },
      { "data_is" : { "data" : [ "coupon:is-active" ], "op" : "=", "value" : true } }
    ],
    "DO" : [
      { "list_add" : {
          "list" : [ "commerce-order:commerce-coupon-order-reference" ],
          "item" : [ "coupon" ],
          "unique" : 1
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "decimal", "value" : "0" },
          "PROVIDE" : { "variable_added" : { "line_items_total" : "Line items total" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "line_item" : "Current line item" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "data_is" : { "data" : [ "line-item:type" ], "value" : "product" } },
                  "DO" : [
                    { "data_calc" : {
                        "USING" : {
                          "input_1" : [ "line-item:commerce-total:amount" ],
                          "op" : "+",
                          "input_2" : [ "line-items-total" ]
                        },
                        "PROVIDE" : { "result" : { "result" : "Calculation result" } }
                      }
                    },
                    { "data_set" : { "data" : [ "line-items-total" ], "value" : [ "result" ] } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "line-items-total" ],
            "op" : "*",
            "input_2" : [ "coupon:commerce-coupon-percent-amount" ]
          },
          "PROVIDE" : { "result" : { "coupon_discount" : "Coupon Discount" } }
        }
      },
      { "data_calc" : {
          "USING" : { "input_1" : [ "coupon-discount" ], "op" : "\/", "input_2" : "100" },
          "PROVIDE" : { "result" : { "discount" : "Discount amount" } }
        }
      },
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : [ "coupon" ],
            "commerce_order" : [ "commerce_order" ],
            "amount" : [ "discount" ],
            "component_name" : "commerce_coupon_pct_123",
            "currency_code" : "[commerce-order:commerce-order-total:currency_code]"
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      }
    ]
  }
}

I absolutely hate rules and avoid it like the plague so the rule could probably have been solved differently. The only reason I even tried it with rules now was because so much of the functionality was already there.

I've deactivated the 2 rules that ship with the pecentage coupon module and enabled the above rule.

The only thing remaining now is to update the coupon line item whenever the contents of the order changes. Hopefully that isn't too hard to achieve. :)

jberg1’s picture

Thanks TwiiK,
This does seem to partially work. The problem I have is on the checkout screen after hitting the Apply Coupon button. It shows the granted amount as 0.00, and at the top the line item for the coupon has no label. I think this is confusing for the customer.

Any idea how to fix this issue?

The line item does appear in the email receipt along with a label for that line item.
What's confusing is why the Fixed Amount coupon does show up as a line item in the email receipt, right out of the box.

jberg1’s picture

Issue summary: View changes

Added comment about mandatory disabling of default rule

bennybobw’s picture

Adding to this thread -- I've updated the rule provided in #4 so that it uses the coupon's price component name rather than a hard coded value;

{ "rules_add_coupon_line_item" : {
    "LABEL" : "Redeem a coupon with percentage amount",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "rules_conditional", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_redeem" ],
    "IF" : [
      { "data_is" : {
          "data" : [ "coupon:type" ],
          "op" : "=",
          "value" : "commerce_coupon_pct"
        }
      },
      { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_percent_amount" } },
      { "entity_has_field" : {
          "entity" : [ "commerce_order" ],
          "field" : "commerce_coupon_order_reference"
        }
      },
      { "NOT AND" : [
          { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-percent-amount" ] } }
        ]
      },
      { "data_is" : {
          "data" : [ "coupon:commerce-coupon-percent-amount" ],
          "op" : "\u003E",
          "value" : "0"
        }
      },
      { "data_is" : { "data" : [ "coupon:is-active" ], "op" : "=", "value" : true } }
    ],
    "DO" : [
      { "list_add" : {
          "list" : [ "commerce-order:commerce-coupon-order-reference" ],
          "item" : [ "coupon" ],
          "unique" : 1
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "decimal", "value" : "0" },
          "PROVIDE" : { "variable_added" : { "line_items_total" : "Line items total" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "line_item" : "Current line item" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "data_is" : { "data" : [ "line-item:type" ], "value" : "product" } },
                  "DO" : [
                    { "data_calc" : {
                        "USING" : {
                          "input_1" : [ "line-item:commerce-total:amount" ],
                          "op" : "+",
                          "input_2" : [ "line-items-total" ]
                        },
                        "PROVIDE" : { "result" : { "result" : "Calculation result" } }
                      }
                    },
                    { "data_set" : { "data" : [ "line-items-total" ], "value" : [ "result" ] } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "line-items-total" ],
            "op" : "*",
            "input_2" : [ "coupon:commerce-coupon-percent-amount" ]
          },
          "PROVIDE" : { "result" : { "coupon_discount" : "Coupon Discount" } }
        }
      },
      { "data_calc" : {
          "USING" : { "input_1" : [ "coupon-discount" ], "op" : "\/", "input_2" : "100" },
          "PROVIDE" : { "result" : { "discount" : "Discount amount" } }
        }
      },
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : [ "coupon" ],
            "commerce_order" : [ "commerce_order" ],
            "amount" : [ "discount" ],
            "component_name" : [ "coupon:price-component-name" ],
            "currency_code" : "[commerce-order:commerce-order-total:currency_code]"
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      }
    ]
  }
}