1
votes

I have Magento 2.3 website, which is connected with third-party sales channels like Amazon, eBay and BOL to sync products and get orders data back into the Magento system and send shipment created by retailer in Magento to those third-party sales channels.

Our system was initially configured to have prices in Magento including VAT. But after EU VAT changes we needed to use Magento prices excluding VAT to properly manage prices on each sales channels and in Magento stores also.

For BOL sales channel, when I receive order, I need to deduct the shipping cost based on item weight and VAT % from the item price we receive from BOL, because BOL item price includes all amounts in item while in Magento we set shipping cost separately.

I am able to set the Custom Price after doing all the calculation required to set final excluded VAT item price in Price column(note, I renamed it to BOL Price) but our Magento system is configured to use Original Price(Magento product price) setup via admin to calculate taxes. In this case, our Original Price is not same as Custom Price we set after our custom calculations. So Product Sub-total and all other prices/calculation does not match the correct values. I know we can configure Magento to use Custom Price for TAX calculation but as we have other sales channel also depending on this setting and developed on Original Price TAX calculation, I need to use same configuration for this. Hence, this requires to set same Custom Price to set as Original Price also to get correct VAT and totals. But not able to get that Original Price updated with any method I could found. Which still uses Magento admin price instead of Price I wanted to set which is same as Price I calculated for Custom Price.

Please check code below, where I tried to update Product price before adding it to cart and also tried cart item product price update after adding it to the cart:

public function addProductToCart(string $quoteId, $item, string $storeCode)
{
    $quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'masked_id');

    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $this->cartRepository->get($quoteIdMask->getData('quote_id'));

    $price = $item->unitPrice()->toScalar(); //Item price on BOL

    //CALCULATION TO GET FINAL PRICE BEFORE ADDING IT HERE

    $product = $this->getProductByEan($item->product()->ean()->toString(), $storeCode); //Get product to add in quote by EAN

    // Trying to set Original Price of product before adding product item to quote
    $product->setPrice($price));
    $product->setOriginalPrice($price);
    $product->setBasePrice($price);
    $product->setConvertedPrice($price);
    $product->setBaseOriginalPrice($price);
    $product->setPriceInclTax($price);
    $product->setIsSuperMode(true);

    $cartItem = $quote->addProduct(
        $product,
        $item->quantity()->toScalar()
    );


    $quote->setIsSuperMode(true);


    //Update Custom Price [Works]
    $cartItem->setOriginalCustomPrice($price);    
    $cartItem->setCustomPrice($price);


    // Trying to set Original Price of cart item product after item added to quote [NOT WORKING]
    $cartItem->getProduct()->setPrice($price);
    $cartItem->getProduct()->setOriginalPrice($price);
    $cartItem->getProduct()->setBasePrice($price);
    $cartItem->getProduct()->setConvertedPrice($price);
    $cartItem->getProduct()->setBaseOriginalPrice($price);
    $cartItem->getProduct()->setPriceInclTax($price);
    $cartItem->getProduct()->setIsSuperMode(true);

    $this->cartRepository->save($cartItem->getQuote());
}

enter image description here

1
I got the Product price updated in first column if I use save() method on product after setPrice() but it is actually updating price of product in Magento. What actually I want to do is, just update the Original Price for the order items and not the actual product price in Magento. In both the cases, either I do save() on product before adding to the cart or I update the cart item product after adding product to the cart and do save(), product price getting updated. Examples: $product->save(true); $cartItem->getProduct()->save();Hardiksinh Gohil
I am able to change the Original Price column in order detail from some google references with looping through Order Items and setting price for each items again with code: foreach ($orderObj->getAllItems() as $item) { $item->setOriginalPrice($item->getPrice())->setBaseOriginalPrice($item->getPrice())->save(); } but that is after TAX applied on Magento Price because it already saved, so totals are wrong.Hardiksinh Gohil

1 Answers

0
votes
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="customprice" instance="Ced\Hello\Observer\CustomPrice" />
    </event>
</config>



<?php
    
    namespace Ced\Hello\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;

    class CustomPrice implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $item = $observer->getEvent()->getData('quote_item');           
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $price = 100; //set your price here
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }

    }

Try this......