Magento2 add product Tier Price programmatically

So If you have a task to add Magento2 tier price when saving product. you can use event “controller_action_catalog_product_save_entity_after”

app/code/MageChallenge/GroupPrice/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_catalog_product_save_entity_after">
        <observer name="group_price" instance="MageChallenge\GroupPrice\Observer\ProductGroupPrice" />
    </event>
</config>

app/code/MageChallenge/GroupPrice/Observer/ProductGroupPrice.php

<?php

namespace Magechallenge\GroupPrice\Observer;

use Magento\Framework\Event\ObserverInterface;

/**
 * Class ProductGroupPrice
 * @package Magechallenge\GroupPrice\Observer
 */
class ProductGroupPrice implements ObserverInterface
{
    protected $logger;

    /**
     * ProductGroupPrice constructor.
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        try {
            $product = $observer->getProduct();
            $tierPrice = [];

            $tierPrice[] = ['website_id' => 0,'cust_group' => 1, 'price_qty' => 1 , 'price' => 4 ];
            $tierPrice[] = ['website_id' => 0,'cust_group' => 2, 'price_qty' => 1 , 'price' => 5 ];
            $tierPrice[] = ['website_id' => 0,'cust_group' => 3, 'price_qty' => 1 , 'price' => 6 ];
            $tierPrice[] = ['website_id' => 0,'cust_group' => 4, 'price_qty' => 1 , 'price' => 7 ];

            $product->setTierPrice($tierPrice);
            $product->save();
            
        } catch (\Exception $e) {
            $this->logger->info($e->getMessage());
        }
    }
}

Latest posts by Chathura Janaka Liyanage (see all)

6 thoughts on “Magento2 add product Tier Price programmatically

  1. I’m also commenting to let you be aware of of the cool discovery my child encountered checking your blog. She came to understand a wide variety of details, most notably how it is like to have a marvelous coaching heart to get folks easily fully grasp a variety of complicated matters. You actually did more than my desires. I appreciate you for delivering such good, dependable, edifying and also cool guidance on that topic to Evelyn.

  2. I am only writing to make you understand what a superb experience our princess undergone viewing yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  3. I am glad for writing to make you know what a nice encounter my wife’s girl had studying your web site. She mastered several pieces, which include what it’s like to possess an awesome helping nature to let other individuals very easily know precisely some problematic subject matter. You truly exceeded people’s expectations. Thank you for presenting these practical, trusted, informative and in addition fun tips about this topic to Kate.

  4. Thank you for your entire labor on this web site. My mom take interest in participating in investigations and it is obvious why. My partner and i notice all relating to the powerful ways you render vital tips and tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a very good job.

  5. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render vital tips and tricks via your website and therefore welcome response from website visitors on this theme while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a very good job.

Leave a Reply