Magento – Update prices of all simple products via their configurable product in Magento 2

magento-2.1magento2price

I'm new to Magento 2, and understand this is a change from Magento 1.

Example: I have a T-Shirt as a configurable product with colour options as Simple Products. These Simple Products all require the same price.

  • If I want to update the price of the configurable product (and thus the Simple Product variations) how do I easily do this?
  • Same question with regards to applying a special price
  • How would I apply tiered pricing to all Simple Product variants?

As far as I can see, this is only possible via the product catalogue screen: find a way to select all the Simple Product variations of that configurable product then apply a bulk action to change the price / special price. This seems very long-winded and tricky – say I had 50 variations on a product all with different SKUs: even filtering then selecting all of those in the Product Catalogue would be a task.

Therefore, I'm assuming I'm missing something! I'd guess a huge portion (majority?) of users who create configurable products will want to apply a single price to all variations (Simple Products). Simple Products should have the option of inheriting the price from their configurable product.

I believe Magento 1 handled this differently and did use the configurable product's price until explicitly overridden via a Simple Product variation. This seems far more sensible to me… though happy to be educated here!

Best Answer

I was getting same issue when Migrated data from Magento to Magento 2.As Magento 2 change scenario to set the Special price for configurable product and we need to require set special price for every associated product of configurable product.

    <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;

try{

    require __DIR__ . '/app/bootstrap.php';

    $params = $_SERVER;
    $bootstrap = Bootstrap::create(BP, $params);
    $obj = $bootstrap->getObjectManager();
    $obj->get('Magento\Framework\Registry')->register('isSecureArea', true);
    $appState = $obj->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $products = $obj->create('\Magento\Catalog\Model\Product')->getCollection();
    $products->addAttributeToSelect(array('special_price','price'))
        ->addFieldTofilter('type_id','configurable')
        ->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
        ->addFieldToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
        ->load();



    echo "Total products count: " . $products->count() . "<hr>";
    echo "<pre>";
    if ($products->getSize() > 0) {
        foreach ($products as $product) {


            $configSpecial = $product->getSpecialPrice();

            if($configSpecial > 0)
            {
                echo 'product id<br/>'.$product_id = $product->getId();
                $configProduct = $obj->create('Magento\Catalog\Model\Product')->load($product_id);
                $_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);



                 foreach ($_children as $child){



                     $childProductId = $child->getID();

                     $childproduct = $obj->create('Magento\Catalog\Model\Product')->load($childProductId);


                     $childproduct->setStoreId(9);
                     //$childproduct->getName(); 
                     $childproduct->setSpecialPrice($configSpecial); 
                     $childproduct->save();
                     //echo $childproduct->getSpecialPrice(); 
                }

                //echo "count: ".count($_children);



           }



            //print_r($product->getData());
        }
    }

} catch (Exception $e) {
    echo $e->getMessage();
}

I hope this script will help you.