1

Currently I have a custom API to live feed the product tier prices with data like this:

[
   {
    "sku":"X772A5",
    "customer_group_id":"1",
    "website_id":"2",
    "qty":"5",
    "price":"500"
   },
   {
    "sku":"X772A5",
    "customer_group_id":"2",
    "website_id":"2",
    "qty":"2",
    "price":"450"
   },
   {
    "sku":"X772A5",
    "customer_group_id":"1",
    "website_id":"1",
    "qty":"5",
    "price":"490"
   },
   {
    "sku":"X8911V",
    "customer_group_id":"2",
    "website_id":"1",
    "qty":"2",
    "price":"120"
   }
 ]

The data contains the product ski , customer group ID, website ID, product qty for the tier price, and product price. I want to remove all the product tier prices and then I can insert this new tier prices data, how can I achieve this?

public function execute(){
  $datas = $this->getInputDatas();
  $this->removeTierPrices($datas);//???
  foreach($datas as $data){
    $product = $this->getProductBySku($data['sku']); 
    $this->addTierPrice($product,$data);//??
  }
}
0

For tier pricing, Magento Catalog module have their own SaveHandler and UpdateHandler files.

File - vendor/magento/module-catalog/Model/Product/Attribute/Backend/TierPrice/UpdateHandler.php

File - vendor/magento/module-catalog/Model/Product/Attribute/Backend/TierPrice/SaveHandler.php

Alternatively, you can truncate the table named catalog_product_entity_tier_price programmtically.

Make sure to check first on any local environment, it may break the pricing. Although, you can run the Magento pricing indexer after truncating the above table.

This is all depends what data being passed in the $datas parameter in $this->removeTierPrices();

\Magento\Catalog\Api\Data\ProductInterface->setTierPrices($data);

One more way as code is already loading the $product object.

/**
     *  constructor.
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     */
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        Psr\Log\LoggerInterface $logger
    )
    {
        $this->productFactory = $productFactory;
        $this->logger = $logger;
    }


    public function execute()
    {
        $datas = $this->getInputDatas();
        //$this->removeTierPrices($datas);//???
        foreach ($datas as $data) {
            //$product = $this->getProductBySku($data['sku']);
            try {
                $product = $this->productFactory->create();
                $product->load($product->getIdBySku($data['sku']));
                $product->setTierPrices($data);
            } catch (Exception $e) {
                $this->logger->debug($data['sku'] . ' SKU not saved due to ' . $e->getMessage());
            }

        }
    }
| improve this answer | |
  • so when we use \Magento\Catalog\Model\ProductFactory::setTierPrices(), it means we don't need to delete the tier prices before as it will override the tier prices automatically? – jojo Apr 6 at 1:06
  • Yes you can give a try. It should work. – sandip Apr 6 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.