Code for buy one get one free adds free products for all products in cart
-
07-10-2020 - |
Вопрос
I have modified this code below to add a buy one get one free for all products of a category id 525 but when product of this category is added, it gives one free product for all products in cart. How do I filter this code so that it gives one free if only a product from category 525 is added. Thanks
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
$Qty1 = $item->getQty();
if(($Qty1=>1) && ($rule->getGiftSku()=='TBFTIGI009')){
$f1product = $item->getProduct();
$f1_productsku =$f1product->getSku();
//$rule->setGiftSku($f1_productsku);
$freeItem1 = static::_getFreeQuoteItem($quote, $f1_productsku, $item->getStoreId(), (int)$rule->getDiscountAmount());
$quote->addItem($freeItem1);
$freeItem1->setApplyingRule($rule);
$rule->setIsApplied(true); }
}
////
/** @var Mage_Sales_Model_Quote_Item $freeItem */
$freeItem = static::_getFreeQuoteItem($quote, $rule->getGiftSku(), $item->getStoreId(), (int)$rule->getDiscountAmount());
if ($freeItem && $rule->getGiftSku()!='TBFTIGI009')
{
$quote->addItem($freeItem);
$freeItem->setApplyingRule($rule);
$rule->setIsApplied(true);
}
else
{
Mage::log(
sprintf(
'C4B_Freeproduct: Gift product not saleable. Rule ID: %d, Gift SKU: %s, Store ID: %d',
$rule->getId(), $rule->getGiftSku(), $quote->getStoreId()
), Zend_Log::ERR
);
}
}
Решение
Just add a condition before adding free item to cart like this -
if (in_array("525", $f1product ->getCategoryIds())) {
$freeItem1 = static::_getFreeQuoteItem($quote, $f1_productsku, $item->getStoreId(), (int)$rule->getDiscountAmount());
$quote->addItem($freeItem1);
$freeItem1->setApplyingRule($rule);
$rule->setIsApplied(true);
}
Не связан с magento.stackexchange