I wonder, that function for "in stock" products only (quantitities>0) would be great for clearance sales and etc.
[Resolved] Coupon for "In stock" products only
- Hello,
Can you elaborate more? Would seem to me this is not really needed as when the product is out of stock customer cannot add any more. - We sell as "When out of stock - Allow orders" because we're using dropshipping method etc.
...but sometimes we have a dozen pcs of special product (bought on promotion) in our warehouse (quantity>0) then it would be great have an option to mark all (or all from category etc) the products with quantity>0
if this feature would work with combinations it would be better - example:
you have 200 blue shirts in your warehouse and u want sale them with discount but red and yellow of the same shirt you sell as dropshipping and price should be regular. - Ok, here is some code that can do this.
in www/modules/awocoupon/lib/couponhandler.php, find the function define_cart_items. Within it, around line 2251 is this:<?php
$this->cart->items [] = array(
'key'=>$product['id_product'].':'.$product['id_product_attribute'].':'.$product['id_customization'],
'product_id' => $productId,
'attribute_id' => $product['id_product_attribute'],
'customization_id' => $product['id_customization'],
'product_price' => $product['price_wt'],
'product_price_notax' => $product['price'],
'product_price_tax' => $product['price_wt']-$product['price'],
'qty' => $product['quantity'],
'on_sale' => $product_discount,
'totaldiscount'=>0,
'totaldiscount_notax'=>0,
'total_price_notax_reduction_amount'=>0,
);
?>
Change it to this<?php
$this->cart->items [] = array(
'key'=>$product['id_product'].':'.$product['id_product_attribute'].':'.$product['id_customization'],
'product_id' => $productId,
'attribute_id' => $product['id_product_attribute'],
'customization_id' => $product['id_customization'],
'product_price' => $product['price_wt'],
'product_price_notax' => $product['price'],
'product_price_tax' => $product['price_wt']-$product['price'],
'qty' => $product['quantity'],
'on_sale' => $product_discount,
'totaldiscount'=>0,
'totaldiscount_notax'=>0,
'total_price_notax_reduction_amount'=>0,
'quantity_available'=>$product['quantity_available'],
);
?>
Then in function validate_coupon_code_helper around line 460 is this:<?php
$is_discount_before_tax = $coupon_row->function_type == 'giftcert' ? $this->giftcert_discount_before_tax : $this->coupon_discount_before_tax;
$coupon_row->is_discount_before_tax = $is_discount_before_tax;
?>
Right after that add this<?php
preg_match('/{instock_only}/i', $coupon_row->note, $match);
if(!empty($match[0]))
{
// remove instock products
foreach ($coupon_row->cart_items as $k => $tmp)
if (empty($tmp['quantity_available'])) unset($coupon_row->cart_items[$k]);
if (empty($coupon_row->cart_items))
{
// all products in cart are on special
return $this->return_false('errDiscountedExclude');
}
}
?>
Then to enable the feature in a coupon, in options fields->notes add the text:
{instock_only}
That is it, should work after that. - ....options fields->notes - you meant "Description" field
ok thx a lot :D
this function change my life ! heheheh - :) great. Will be added in future versions so you dont have to worry about re adding the code.
- Unfortunately, this improvement working also for "minus" stocks (below 0)
- Negative stock, did not foresee that. So if instock is checked, currently it will work for all products where the stock count is not 0. To update it to work for products where the stock count is greater than 0, then change this:
<?php
foreach ($coupon_row->cart_items as $k => $tmp)
if (empty($tmp['quantity_available'])) unset($coupon_row->cart_items[$k]);
?>
To this:<?php
foreach ($coupon_row->cart_items as $k => $tmp)
if ($tmp['quantity_available']<=0) unset($coupon_row->cart_items[$k]);
?> - U mean this:
if (empty($quantity_in_stock)) unset($coupon_row->cart_items[$k]);
to:if ($quantity_in_stock<=0) unset($coupon_row->cart_items[$k]);
- Yep, that is it. The code in AwoCoupon is obviously a little different than the code in this forum.