Prestashop Price update API(CURD)
Streamlining Your E-commerce: Updating Prices in PrestaShop Using the API
In the fast-paced world of e-commerce, keeping your product prices up-to-date is crucial for maintaining competitiveness and accuracy. PrestaShop, a popular open-source e-commerce platform, offers a powerful API that allows you to automate this process. In this article, we'll explore how to update prices in PrestaShop using its API, enabling you to manage your online store more efficiently.
Understanding PrestaShop's API
PrestaShop's API is based on REST principles, making it easy to interact with your store's data programmatically. Before diving into price updates, ensure you have:
API access enabled in your PrestaShop back office
Generated API credentials (key)
Steps to Update Prices via API
Authentication
First, you need to authenticate your requests. PrestaShop uses basic HTTP authentication:
Authorization: Basic {base64_encoded_key}
Identify the Product from Sku/Reference
To update a price, you need the product's ID. You can retrieve this through a GET request to:
// Your PrestaShop API Key $apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; $shopUrl = 'http://your-prestashop-domain'; // The SKU of the product to update $sku = 'SKU12345'; function getCurl($url, $apiKey) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Basic ' . base64_encode($apiKey . ':'), ]); $output = curl_exec($ch); curl_close($ch); return $output; } // Step 1: Get Product ID by SKU $productUrl = $shopUrl . '/api/products?filter[reference]=' . $sku; $productResponse = getCurl($productUrl, $apiKey); $productData = new SimpleXMLElement($productResponse); // Check if product exists if (isset($productData->products->product)) { $productId = (int) $productData->products->product['id']; } else { die('Product not found.'); }
Update the Price
Once you have the product ID, you can update its price using a PUT request:
// New price $newPrice = 100; // Step 2: Get Full Product Data by ID $productDetailUrl = $shopUrl . '/api/products/' . $productId; $productDetailResponse = getCurl($productDetailUrl, $apiKey); $productDetailData = new SimpleXMLElement($productDetailResponse); // Step 3: Modify the XML to update price and quantity unset($productDetailData->product->manufacturer_name); unset($productDetailData->product->quantity); $productDetailData->product->price = $newPrice; // Step 4: Send the PUT request to update the product $updatedXml = $productDetailData->asXML(); $updateUrl = $shopUrl . '/api/products/' . $productId; $updateResponse = putCurl($updateUrl, $updatedXml, $apiKey); // Check for successful update if ($updateResponse) { echo "Price updated successfully."; } else { echo "Failed to update price."; }
Conclusion
Leveraging PrestaShop's API for price updates can significantly streamline your e-commerce operations. By automating this process, you can ensure price accuracy, save time, and focus on growing your business. As with any API integration, remember to test thoroughly in a development environment before implementing in production.
Happy selling!
Subscribe to my newsletter
Read articles from Jeevan K B directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by