Tags/topics: 
1
Answers
Vote up!
0
Vote down!

"Add Product to Cart" action not working

Our store has a "product" whose first copy costs 4$ and additional copies would cost 9$. We don't want to do an order-level discount and would prefer to have 2 separate products.

So I created a rule saying if the user tried to add more than 1 [4$ product], add the [9$ product] to the cart. When I try to add 3 [4$ product], I'm expecting 1 [4$ product] to be added and 2 [9$ products]. But what happens is only 1 [4$ product] is added to the cart. When I try to add a [4$ product] again, it works as expected: a [9$ product] gets added to the cart.

I debugged values in my rule and they all have the expected value but the [9$ product] just doesn't get added to the cart when the line item doesn't exist yet!

Here are the values: http://dropcanvas.com/5al05/1

And here is an export of my rule:

{ "rules_buy_monthly_premium_1_" : {
    "LABEL" : "Buy Monthly Premium (1)",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "devel", "commerce_cart", "entity" ],
    "ON" : [ "commerce_line_item_presave" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-product:product-id" ],
          "value" : "2392"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:quantity" ],
          "op" : "\u003E",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "devel_debug" : { "value" : [ "commerce-line-item:quantity" ] } },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:quantity" ],
            "op" : "-",
            "input_2" : "1"
          },
          "PROVIDE" : { "result" : { "qty_of_copies" : "Qty of Additional Copies" } }
        }
      },
      { "component_rules_get_premium_path" : { "PROVIDE" : { "premium_path" : { "premium_sku" : "Premium SKU" } } } },
      { "devel_debug" : { "value" : [ "premium-sku" ] } },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "qty-of-copies" ] },
          "PROVIDE" : { "conversion_result" : { "qty_of_copies_int" : "Qty of Additional Copies (Int)" } }
        }
      },
      { "devel_debug" : { "value" : [ "qty-of-copies-int" ] } },
      { "commerce_cart_product_add_by_sku" : {
          "USING" : {
            "user" : [ "site:current-user" ],
            "sku" : [ "premium-sku" ],
            "quantity" : [ "qty-of-copies-int" ],
            "combine" : 1
          },
          "PROVIDE" : { "product_add_line_item" : { "premium_copies" : "Added copies of premium" } }
        }
      },
      { "devel_debug" : { "value" : [ "premium-copies" ] } },
      { "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } },
      { "entity_save" : { "data" : [ "commerce-line-item:order" ], "immediate" : 1 } }
    ]
  }
}

I also checked the rules log and checked each rule that fires. Is this a bug? I posted over the project page in drupal but was told to post it here.

Asked by: akosipax
on May 10, 2013

1 Answer

Vote up!
0
Vote down!

Never mind. Somehow, it works when the event is 'After adding a product to the cart'.

Answer by: akosipax
Posted: May 10, 2013