Over the time Magento became the first choice just because the functionality and flexibility it offers to store owners to fulfill their own business needs. Not only this Magento offer extensive code functionality with great performance and responsive layout which makes it stand out from the competition.
Among all features, the most used & handy features for the store owner is setting up tier price to offer special deals and discounts based on a product quantity to drive more sales and encourage the customer to purchase more. Sometimes setting up tier price for products can become more time-consuming task when there is a large number of products. Today at MageComp blog we came up with a code that will save tons of time for store owners so they can utilize that time in breaking another business deal.
Simply pass the product ID and tier price to below script and it will set tier price work like a charm to set tier price programmatically for the product.
For this, we have created ‘Tierprice.php‘ file our Magento root directory and have used this below code in that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!--?php use Magento\Framework\AppInterface; try { require_once __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try { $objectManager = $bootstrap->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $tierPrices[] = array( 'website_id' => 0, 'cust_group' => 0, 'price_qty' => 60, 'price' => 6 ); $product_id = 1; //PASS YOUR PRODUCT ID HERE $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id); $product->setTierPrice($tierPrices); $product->save(); echo "Price Save Successfully"; } catch(\Exception $e) { print_r($e->getMessage()); } </pre> <p>You can use this code for setting up tier price for one or more product by modifying this code as per your need.<br ?--> And that’s it! Now your tier price will be visible on the store frontend! Need help regarding following the code? Leave a comment down below. Happy Coding! |