0

Trying to change the Super attribute for my Magento 2 configurable product. Current I have 100 configurable product that has a super attribute test_colour, and I've created another attribute called colour, and I want to update the configurable product with colour instead of test_colour programmatically.

Does anyone know how to solve it ? Thanks in advance.

1 Answer 1

-1

The methods on this class will create an custom option when this execute . you can use this way create super attribute to your product .

the method deletecustomoption() will remove existing super attribute from the product.

<?php
namespace Today\Slot\Block;

class CustomOption extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Model\Product\Option $optionFactory,
        array $data = []
    ) {
        $this->productRepository = $productRepository;
        $this->optionFactory = $optionFactory;
        parent::__construct($context,$data);
    }

    /**
    * @param string $sku
    */
    public function addCustomOption($productId)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
        $string =  $product->getDemo();
        $str_arr = explode (",", $string);
        $values =array();
        for ($i = 0;$i<count($str_arr);$i++)
        {
            $values[] = [
                'record_id'=>$i,                                        
                'title'=>$str_arr[$i],
                'price'=>10,
                'price_type'=>"fixed",
                'sort_order'=>1,
                'is_delete'=>0
            ];
        }
        $date_from = $product->getchooseDateFrom();
        $date_from = strtotime($date_from);
        $date_to = $product->getchooseDateTo();
        $date_to = strtotime($date_to);
        $dates = array();

            $options = [

                [
                    "sort_order"    => 3,
                    "title"         => "Choose Slots",
                    "price_type"    => "fixed",
                    "price"         => "",
                    "type"          => "multiple",
                    "values"        => $values,
                    "is_require"    => 0
                ]
            ];

        $product->setHasOptions(1);
        $product->setCanSaveCustomOptions(true);
        foreach ($options as $arrayOption) {
            $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
                    ->setProductId($productId)
                    ->setStoreId($product->getStoreId())
                    ->addData($arrayOption);
            $option->save();
            $product->addOption($option);
        }
    }

    public function deletecustomoption($productId)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
        if ($product->getOptions()) {
                    foreach ($product->getOptions() as $opt) {
                        $opt->delete();
                    }
                }
    }
}

PS : Trying this code directly wont work , unless you change it for yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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