Welcome TO magento1 and magento2 store market
How to add update product Tier Price programmatically Magento 2?
You can add/update tier price to the Product from the Admin Panel Manually by click on the Products -> Add or Edit Product -> Price (Click on Advanced Pricing).
If you have to task to add tier price in the product programmatically in Magento using add() method from the ScopedProductTierPriceManagementInterface.
Core Method to add tier price,
public function add($sku, \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice);
$sku is the Product SKU.
$tierPrice is the ProductTierPriceInterface where you need to create ProductTierPriceInterfacefactory with set Qty(float), Price Value(float) and Customer Group id to add tier price.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php namespace Jesadiya\AddTierPrice\Model; use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory; use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface; class AddUpdateTierPrice { /** * @var ScopedProductTierPriceManagementInterface */ private $tierPrice; /** * @var ProductTierPriceInterfaceFactory */ private $productTierPriceFactory; public function __construct( ScopedProductTierPriceManagementInterface $tierPrice, ProductTierPriceInterfaceFactory $productTierPriceFactory ) { $this->tierPrice = $tierPrice; $this->productTierPriceFactory = $productTierPriceFactory; } /** * Add Tier price to the Product * * @return bool */ public function addTierPrice() { $qty = 10.00;//must be float value. $price = 150.00;//must be float value. $customerGroupId = 8; $sku = '102-mts'; try { $tierPriceData = $this->productTierPriceFactory->create(); $tierPriceData->setCustomerGroupId($customerGroupId) ->setQty($qty) ->setValue($price); // If you want to set price_type as 'discount' use below code. $extensionAttributes = $tierPriceData->getExtensionAttributes(); $extensionAttributes->setPercentageValue($price); $tierPriceData->setExtensionAttributes($extensionAttributes); $tierPrice = $this->tierPrice->add($sku, $tierPriceData); } catch (NoSuchEntityException $exception) { throw new NoSuchEntityException(__($exception->getMessage())); } } } ?> |
You can add tier price by call method,
echo $tierPriceData = $this->addTierPrice();