I want something like: Buy two, and get $5 off your second item.

I've created this rule:

{ "rules_comm" : {
    "LABEL" : "Bulk discount (2)",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "commerce_coupon", "commerce_cart" ],
    "ON" : [ "commerce_cart_product_add", "commerce_cart_product_remove" ],
    "DO" : [
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : "76",
            "commerce_order" : [ "commerce_order" ],
            "amount" : "500",
            "component_name" : "base_price",
            "currency_code" : [ "commerce-order:commerce-order-total:currency-code" ]
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      }
    ]
  }
}

But it does nothing to the order that I can see. How does one add a coupon to an order?

Comments

pcambra’s picture

Status: Active » Fixed

I've made a quick and dirty test.

You'd pobably want to play around this one but the export below is a rule that adds a coupon to the current user's cart on content view

{ "rules_test_on_add_discount" : {
    "LABEL" : "TEST ON ADD DISCOUNT",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_cart", "commerce", "devel", "commerce_coupon" ],
    "ON" : [ "node_view" ],
    "IF" : [
      { "data_is" : { "data" : [ "node:nid" ], "value" : "6" } },
      { "commerce_order_is_cart" : { "commerce_order" : [ "site:current-cart-order" ] } },
      { "entity_exists" : {
          "type" : "commerce_coupon",
          "property" : "commerce_coupon_code",
          "value" : "FIXED_01"
        }
      },
      { "entity_has_field" : {
          "entity" : [ "site:current-cart-order" ],
          "field" : "commerce_coupon_order_reference"
        }
      }
    ],
    "DO" : [
      { "devel_debug" : { "value" : [ "node:title" ] } },
      { "entity_query" : {
          "USING" : {
            "type" : "commerce_coupon",
            "property" : "commerce_coupon_code",
            "value" : "FIXED_01",
            "limit" : "1"
          },
          "PROVIDE" : { "entity_fetched" : { "commerce_coupon" : "Commerce coupon" } }
        }
      },
      { "list_add" : {
          "list" : [ "site:current-cart-order:commerce-coupon-order-reference" ],
          "item" : [ "commerce-coupon:0" ],
          "unique" : 1
        }
      },
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : [ "commerce-coupon:0" ],
            "commerce_order" : [ "site:current-cart-order" ],
            "amount" : "1000",
            "component_name" : "commerce_coupon_fixed_fixed_01",
            "currency_code" : "USD"
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      }
    ]
  }
}
aidanlis’s picture

Great! Thanks so much for providing that, I'm sure it'll come in handy for other developers also.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.