Temporarily Changing Magento Product Price via External PHP File
In this post, we’ll guide you through changing the price of a Magento product using an external PHP file that receives the product ID and new price via jQuery POST. Our goal is to:
- Update the product’s price temporarily.
- Ensure the updated price reflects when adding the item to the cart.
- Avoid saving the changes permanently in the database.
Prerequisites
Before proceeding, make sure you have a Magento 1.6.2 installation set up and running. You’ll need access to the site’s root folder via FTP or SFTP to upload your PHP files.
Step-by-Step Solution
1. Create the External PHP File (ajaxPriceCal.php)
Create a new file named ajaxPriceCal.php in your Magento root directory. This file will handle the POST request from your frontend jQuery script.
<?php
require_once('app/Mage.php'); // Include the Magento autoloader
Mage::app(); // Initialize Magento
$product_id = $_POST['pid'];
$my_price = $_POST['price'];
// Load the product object by ID
$product = Mage::getModel('catalog/product')->load($product_id);
// Update the price temporarily (tier pricing ignored)
$product->setPrice($my_price);
// Clear all prices for this product
Mage::getSingleton('catalog/product_action')->updateAttributes(array(
$product_id => array(
'price' => $my_price,
)
), $forceUpdate = true, null);
2. Update Your jQuery Script
Ensure your existing jQuery script sends the pid and price parameters correctly via POST to this new PHP file.
var priceNewValue = XX; // My custom price value
var product_id = optionsPrice.productId; // Product id
$.post("http://flyingcakes.in/eshop/ajaxPriceCal.php", { price: priceNewValue, pid: product_id });
3. Refresh the Cart after Price Change
To reflect the updated price in the cart, you’ll need to refresh or re-render the cart items after changing the product’s price.
You can use Magento’s updateCartSummary and updateCartTotals functions for this purpose:
var url = "http://flyingcakes.in/eshop/ajaxPriceCal.php";
$.post(url, { price: priceNewValue, pid: product_id }, function(data) {
window.location.reload();
});
Key Points to Consider
1. Temporarily Changing the Price
The solution above uses Magento’s updateAttributes method with $forceUpdate = true. This ensures that the price is updated immediately without being saved permanently in the database.
2. Price Tier Ignoring
This script ignores tier prices and only updates the regular price of the product.
3. jQuery POST Request
Ensure your server configuration allows CORS (Cross-Origin Resource Sharing) if you’re running the external PHP file from a different domain than your Magento site to avoid browser security restrictions.
Conclusion
You have now successfully learned how to temporarily change the price of a Magento product using an external PHP file and jQuery POST. This approach is particularly useful for dynamic pricing scenarios where prices need to be adjusted on-the-fly without permanently updating the database.
Remember, this method relies on Magento’s built-in updateAttributes functionality to update prices immediately but not save them indefinitely.
For more advanced price management techniques in Magento, we recommend checking out our comprehensive guide on Dynamic Pricing Extensions for Magento.