Coupons and “dynamically” priced products
-
13-12-2019 - |
Question
We have a product that is setup with a price of $0.00. When the product is added to the cart, programatically, we set the price according to a fee schedule that we have setup in an off line table.
Now, the product has been setup in it's own category and I have setup a coupon to discount it 100%. The coupon also discount another product from another category 100% so the entire cart must be discounted 100% when the 2 products are in the cart.
Now, because we are setting the price when we add the product to the cart, the discount is not being applied to the price set in the cart but to the ORIGINAL price of the item, $0.00.
How can I make this coupon work?
Here are the particulars:
Conditions:
Apply the rule only if the following conditions are met (leave blank for all products)
If ANY of these conditions are TRUE :
If an item is FOUND in the cart with ANY of these conditions true:
Category is 9
If an item is FOUND in the cart with ANY of these conditions true:
Category is 30
Actions:
Update prices using the following information
Apply: Percent of product price discount
Discount amount: 100
Maximum Qty Discount is Applied to:0
Discount Qty Step (Buy X): 0
Apply to Shipping Amount: No
Free shipping: No
Stop further rules processing: No
Apply the rule only to cart items matching the following conditions (leave blank for all items)
If ANY of these conditions are TRUE :
Category is 9
Category is 30
EDITED...
ADDED CODE
// Get the product_id from product from SKU
$product_id = Mage::getModel("catalog/product")->getIdBySku("$sku");
// Load the product object by obtained productID
$product = Mage::getModel("catalog/product")->load($product_id);
// get the current cart
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
$cart = Mage::helper('checkout/cart')->getCart();
// if price is set, the use the passed price otehrwise use product's price
if(isset($_GET['price'])) {
$price = $_GET['price'];
} else {
$price = $product->getPrice();
}
// set params for product to be added to cart.
$params = array(
'product' => $product_id,
'qty' => $qty, // $qty is also passed in the $_GET
'price' => $price
);
/*
I have code here that loads a custom option with JSON data about the user, then sets the option like this:
$params['options'] = array($option_id => json_encode($item_json));
*/
// Now add the product to the cart.
$cart->addProduct($product, $params);
$session->setLastAddedProductId($product_id);
$session->setCartWasUpdated(true);
$cart->save();
Solution
Instead of using the shopping cart rules, I would extend Mage_Checkout_CartController to check for the presence of the coupon code and then perform logic to check if the associated product sku is also in the cart.
If the product exists, you can get the value (and quantity) of it from the cart and then discount it, or as you are discounting it 100% simply set it's value to zero. You can then also notify the customer of the action taken.
You would need the quantity to ensure you only discount it once, unless you are giving more than one away with the coupon. This is in principle very similar to a Coupon X Get Y free module I have developed.
OTHER TIPS
you need to create a small extension for event handle for this task as below,in your config.xml.
<frontend>
<events>
<controller_action_predispatch_checkout_cart_couponPost>
<observers>
<package_extensionname>
<type>singleton</type>
<class>package_extensionname_Model_observer</class>
<method>addProduct</method>
</package_extensionname>
</observers>
</events>
</frontend>
create a observer for adding product with "addProduct" method and place there or code.
Let me know if you have any query
Thanks