Magento: Adding the Same Product to Cart with Different Prices
As an experienced developer, I understand your concern about managing cart prices for programmatically edited products in Magento. Let’s walk through this issue step-by-step.
The Current Issue
When you allow customers to edit a productâs price and add it to their cart multiple times with different prices, Magento currently displays the total quantity and price incorrectly. For example:
- You add Product A with a price of $20
- Then, you add the same Product A again, but this time at $30
In the shopping cart summary, you see:
-| qty=2 -| total price=$60
This is illogical because the total price should be $50 (not $60), and the quantity should remain 1 for each distinct product with its specific price.
The Challenge: SKU and Uniqueness
The root of this problem lies in how Magento handles cart items. By default, it uses the sku as a primary identifier for adding items to the cart. When you programmatically change prices without modifying the SKU, the system treats these as duplicates due to identical identifiers.
A Practical Solution: Customizing Cart Item Identifiers
To resolve this issue without changing the SKUs, we can modify how Magento identifies unique cart items. Here’s a two-step approach using Observers:
-
Create a custom attribute (let’s call it
custom_price) and set it to private. This will store the dynamic price for each instance of the product in the cart. -
Use an Observer to trigger when an item is added to the cart:
public function addToCartObserver(Observer $observer)
{
$item = $observer->getEvent()->getQuoteItem();
$product = $item->getProduct();
// Check if the price has been edited programmatically
if ($product->hasCustomPrice()) {
$customPrice = $product->getCustomPrice();
$item->setCustomPrice($customPrice);
}
}
In this Observer, we check if the product’s price has been manually modified. If so, we set the custom_price attribute for this specific cart item.
Implementing the Custom Price
To allow customers to edit the price programmatically without changing the SKU, update your controller:
public function updateProductPrice($productId, $newPrice)
{
// Fetch the product
$product = $this->_productModel->load($productId);
// Set a custom attribute for the current session's user
$product->setData('your_session_custom_price', $newPrice);
$product->save();
}
In this function, we store the edited price in a customer-specific session attribute, your_session_custom_price. This ensures that each customer sees the correct price based on their specific interaction with your application.
Conclusion
To resolve this Magento issue without modifying SKUs, we’ve implemented a solution using custom attributes and Observers. By storing dynamic prices in a private attribute and using an Observer to set this value when adding items to the cart, you can achieve the desired behavior:
- Each instance of a product with a modified price is treated as unique
- The total quantity reflects individual products (not combined)
- The total price accurately displays the sum of distinct items
Remember, proper testing is essential to ensure that these changes work seamlessly with your existing Magento installation and customizations.
For more insights on optimizing Magento performance and security, explore our Magento development services at Laravel Company.