Changing Original Price of an Item in Quote Programmatically in Magento
As a senior developer working on a Magento 2.3 website that is connected to multiple third-party sales channels like Amazon, eBay, and BOL, I faced a challenging scenario involving price management across different platforms.
The Initial Setup: Prices Including VAT
Our system was originally configured to handle prices inclusive of VAT in the Magento backend. However, with the recent changes in EU VAT regulations, we were compelled to shift our pricing strategy to exclude VAT for better control and synchronization with all connected sales channels.
BOL Channel Requirements
For the BOL channel specifically, when an order is received, I needed to implement a complex calculation process. The item price provided by BOL includes shipping costs, which are based on the item’s weight and the current VAT percentage in effect. To ensure accurate inventory management, we had to deduct these shipping-related expenses from the received item price after applying our custom tax deductions.
The Custom Price Dilemma
During this process, I successfully managed to set a “Custom Price” for each item by performing the required calculations and updating the “Price” column (renamed as “BOL Price”). However, Magento’s default configuration uses the Original Price setup via the admin panel to calculate taxes. In our case, the Original Price was not aligned with the Custom Price calculated through my custom logic.
The Need for Consistency
To resolve this discrepancy without disrupting the existing architecture and interconnected sales channels, I aimed to set the same Custom Price as the Original Price. This would ensure that the product’s sub-total and all subsequent price calculations would match the correct values based on our system’s requirements.
Attempts at Updating the Original Price
I explored various methods to update the Original Price programmatically, both before adding the item to the cart and after the item had been added. Unfortunately, neither approach yielded the desired result. The Magento system continued to use the admin-set Original Price for tax calculations instead of the Custom Price I had meticulously calculated.
Code Snippets: Unsuccessful Attempts
Here are the relevant code snippets that demonstrate my unsuccessful attempts at updating the Original Price:
public function addProductToCart(string $quoteId, $item, string $storeCode)
{
//... (existing code)
$price = $item->unitPrice()->toScalar();
//CALCULATION TO GET FINAL PRICE BEFORE ADDING IT HERE
$product = $this->getProductByEan($item->product()->ean()->toString(), $storeCode);
// Trying to set Original Price of product before adding product item to quote
$product->setPrice($price);
$product->setOriginalPrice($price); // <--- Directly setting the Original Price here
//... (additional attempts at price settings)
// Trying to set Original Price of cart item product after item added to quote [NOT WORKING]
$cartItem->getProduct()->setPrice($price);
$cartItem->getProduct()->setOriginalPrice($price); // <--- Directly setting the Original Price here
$this->cartRepository->save($cartItem->getQuote());
}
As you can see, I attempted to set multiple price fields (Price, Base Price, Converted Price, etc.) and even used the setIsSuperMode(true) method. However, none of these attempts successfully updated the Original Price for use in tax calculations.
The Current Challenge
Despite my efforts, I am unable to find a way to update the Original Price with the calculated Custom Price programmatically. This means that our system’s price calculations do not match the actual prices we receive from BOL or our custom tax deductions.
Next Steps and Further Investigation
I am currently exploring potential solutions that may involve:
- Investigating if there is a specific event or observer that can be triggered to update the Original Price during the cart item creation process.
- Exploring any third-party extensions or modules that might provide a solution for this scenario.
- Consulting with Magento’s official documentation and community forums for alternative approaches.
Conclusion
In this blog post, I have highlighted a complex pricing issue faced while integrating multiple sales channels with a Magento 2.3 website. While my current attempts at programmatically updating the Original Price were unsuccessful, I am committed to finding a resolution that aligns with our system’s requirements and ensures accurate price calculations across all connected platforms.
I encourage you to share any insights or experiences you may have had in dealing with similar pricing challenges in Magento. Your knowledge could prove invaluable in resolving this issue efficiently.