2

I am able add tier prices using csv

$this->tierPrice->add($sku, $customerGroupId, $price, $qty);

but it is taking "value_type" as Fixed , i want to dynamically give the value for "value_type"

1

I done the same by doing

$product_obj = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($prod_id);
    $tierPrices[] = array(
                                 'website_id'  => 0,
                                 'cust_group'  => 1,
                                 'price_qty'   => $qty,
                                 'price'       => $price,
                                 'percentage_value'  => $percentage);
                          $product_obj->setTierPrice($tierPrices);
                        $product_obj->save(); 
Here in the above array   `percentage_value` is given for the price range having discount type.         
0

I used Rakesh Jesadiya article information and done some debugging to create more "elegant" solution for percentage_value tier price type. Here is my code:

use Magento\Catalog\Api\Data\ProductTierPriceInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface;

class ProductPriceListsResolver
{
    /**
     * @var ScopedProductTierPriceManagementInterface
     */
    private $tierPriceManagement;
    /**
     * @var ProductTierPriceInterface
     */
    private $productTierPriceFactory;

    public function __construct(
        ScopedProductTierPriceManagementInterface $tierPriceManagement,
        ProductTierPriceInterfaceFactory $productTierPriceFactory
    ) {

        $this->tierPriceManagement = $tierPriceManagement;
        $this->productTierPriceFactory = $productTierPriceFactory;
    }

    public function resolve()
    {
        $sku = 'sku_83519'; //Product sku
        $customerGroupId = 6; //premium group id
        $price = 50.00;
        $qty = 1.00;

        /** @var ProductTierPriceInterface $productTierPrice */
        $productTierPrice = $this->productTierPriceFactory->create();
        $productTierPrice->setCustomerGroupId($customerGroupId)
            ->setQty($qty)
            ->setValue(20.00);

        $extensionAttributes = $productTierPrice->getExtensionAttributes();
        $extensionAttributes->setPercentageValue(20.00);
        $productTierPrice->setExtensionAttributes($extensionAttributes);
        try {
            $this->tierPriceManagement->add($sku, $productTierPrice);
        } catch (\Exception $e) {
            throw new $e;
        }
    }
}

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.