1

I making a promo on Magento 2.4.5 Like buy any specific 5 products and get 1 free. We have products having FPT on them. I want to add this FPT product to cart with 0 price and 0 FPT when someone buy 5 products.

I have achieved this promo by using this event

checkout_cart_product_add_after

and using this code

$product = $this->productRepository->get($productSku);
$product->setTaxClassId(0);
// If the product is not in the cart, create a new item
$item = $this->quoteItemFactory->create();
$item->setProduct($product);
$item->setQty($freeQtyToAdd); // Set the quantity

// Set the final price of the item to 0
$item->setCustomPrice(0);
$item->setOriginalCustomPrice(0);
 // Remove FPT
$item->setTaxAmount(0);
$item->setName('text name');
// Add the item to the cart
$quote->addItem($item);
$quote->collectTotals(); // Recalculate totals
$quote->save();

Product price get 0 and its adding to cart but the issue I am having is the price is 0 but its still showing FPT on this product. I want to remove PFT as well.

enter image description here

1 Answer 1

0

try it like this etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote">
        <plugin name="check_six_products" type="YourVendor\YourModule\Plugin\Quote" sortOrder="1" disabled="false" />
    </type>
</config>

    <?php
namespace YourVendor\YourModule\Plugin;

class Quote
{
    public function afterAddProduct(\Magento\Quote\Model\Quote $subject, $result, $product, $info = null)
    {
        $cartItems = $subject->getAllItems();

        // Check if all 5 previously added products have 'in_offer' flag set to 'yes'
        $inOfferProductCount = 0;
        foreach ($cartItems as $item) {
            $itemProduct = $item->getProduct();
            if ($itemProduct->getData('in_offer') === 'yes') {
                $inOfferProductCount++;
            }
        }

        if ($inOfferProductCount >= 5) {
            // If all 5 products have 'in_offer' flag set to 'yes', find the 6th product and remove its FPT
            foreach ($cartItems as $item) {
                $itemProduct = $item->getProduct();
                if ($itemProduct->getData('in_offer') === 'yes' && $item->getSku() === $product->getSku()) {
                    $item->setFptAmount(0);
                    $item->setBaseFptAmount(0);
                    $item->setTaxAmount(0);
                    $item->setBaseTaxAmount(0);
                    $item->save();
                }
            }
        }

        return $result;
    }
}

you can modify the code accordingly

i have not tested this code, but do let me know if it works.

2
  • I already did the 0 price part i have issue with remove FPT. FPT is not removing tried many things
    – OBAID
    Nov 6, 2023 at 15:12
  • check updated ans
    – Pramod
    Nov 6, 2023 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.