2

I'm following Programmatically add tier prices

On top of below post i'm trying to add Tier Pricing

Magento 2: Save all product data outside Magento with Images

I have done below thing

$tierPrices = array(
      [ 'customer_group_id' => '2', // GROUP ID
        'price_qty' => 1,
        'price' => 100,
        'website' => 'all'
       ]
);
$_product->setTierPrice($tierPrices);

But above code is not working. It's not showing on Admin Side.

While running script it's not giving any kind of error.

For Customer Group Id price will be 100 & for all other groups it will be 150

1
  • It's for Magento 1 this is for Magento 2
    – Jackson
    Commented Jun 22, 2017 at 8:52

3 Answers 3

7

I have tried this in my custom controller and the tier price was updated.

<?php

namespace Test\Sample\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Catalog\Api\ProductTierPriceManagementInterface;

class Index extends Action
{

    protected $tierPrice;

    public function __construct(Context $context, ProductTierPriceManagementInterface $tier)
    {
        $this->tierPrice = $tier;
        parent::__construct($context);
    }

     public function execute()
    {
        $sku = 'test-sku'; //Product sku
        $customerGroupId = 2; //Wholsaler group id
        $price = 200;
        $qty = 55;

        $this->tierPrice->add($sku,$customerGroupId,$price, $qty);
    }
}
3
  • I know$_productTier = $objectManager->create('Magento\Catalog\Model\TierPriceManagement');. But how can i attach that with $product Object? like $product->add(...);
    – Jackson
    Commented Oct 26, 2016 at 8:23
  • Hi @saravanavely. It's not working for me as i needed.
    – Jackson
    Commented Oct 27, 2016 at 2:09
  • Its working..probably the best solution. Because you dont have load the product at all. Just use the SKU. Commented Jul 6, 2017 at 20:53
7

The following code works on Magento 2.1. It allows you to set the tier pricing for the multiple groups:

<?php
use \Magento\Framework\App\Bootstrap;

require '/www/mag2.local/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

### Setting area code
### NOTE: for more info http://devdocs.magento.com/guides/v2.1/architecture/archi_perspectives/components/modules/mod_and_areas.html
$state = $objectManager->get('\Magento\Framework\App\State');

$state->setAreaCode('base');

$tierPriceFactory = $objectManager->get('\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory');

$tierPrices = [
    // All groups
    $tierPriceFactory->create()->setCustomerGroupId(32000)->setQty(4)->setValue(10.0),
    $tierPriceFactory->create()->setCustomerGroupId(32000)->setQty(9)->setValue(9.0),
    // Not logged in
    $tierPriceFactory->create()->setCustomerGroupId(0)->setQty(5)->setValue(10.1),
    $tierPriceFactory->create()->setCustomerGroupId(0)->setQty(10)->setValue(9.1),
    // Wholesale
    $tierPriceFactory->create()->setCustomerGroupId(2)->setQty(6)->setValue(10.2),
    $tierPriceFactory->create()->setCustomerGroupId(2)->setQty(11)->setValue(9.2),
];

$prodInfoArr = [
    ['AttributeSetId' => 4, 'TypeId' => 'simple', 'Name' => 'My Product 20', 'Price' => 99, 'Sku' => 'MP20', 'Weight' => 10, 'TierPrices' => $tierPrices],
];

foreach ($prodInfoArr as $prodInfo) {
    createProduct($prodInfo);
}

function createProduct($prodInfo = []) {
    global $objectManager;

    #$product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create();
    $product = $objectManager->get('\Magento\Catalog\Api\Data\ProductInterfaceFactory')->create();

    $product->setAttributeSetId($prodInfo['AttributeSetId']); // importatnt
    $product->setTypeId($prodInfo['TypeId']); // important
    $product->setName($prodInfo['Name']);
    $product->setPrice($prodInfo['Price']);
    $product->setTierPrices($prodInfo['TierPrices']);
    $product->setSku($prodInfo['Sku']);
    #$product->setQty($prodInfo['Qty']); // will not work. Use "set stock data" instead.
    $product->setWeight($prodInfo['Weight']);
    $product->setTaxClassId(0); // (0: none, 1: default, 2: taxable, 4: shipping)
    $product->setStatus(1);
    $product->setVisibility(4);

    $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->save($product);
}
2

The following code is tested in magento 2.1.4,

    <?php

    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';
    $bootstrap = Bootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();
    $productCollectionFactory = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(3);

    foreach ($collection as $product){
        $appState = $objectManager->get('\Magento\Framework\App\State');
        $appState->setAreaCode('frontend');

        $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
        $productObj = $productRepository->getById($product->getId());

        $tierPrice[0] = array('website_id' => 0,'cust_group' => 2, 'price_qty' => 1 , 'price' => 100.0 );
        $productObj->setPrice(103.59);
        $productObj->setTierPrice($tierPrice);
        $productObj->save();   
    }  

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