0

I have successfully created simple product Programmatically using magento 2.

But i have created a filed in back end and add that in product but how to fill that data when creating a product.

enter image description here

I have created my product by

$product = $this->productFactory->create();
                $product->setSku($productsku);
                $product->setName($productname);
                $product->setDescription($productname."Description3");
                $product->setShortDescription($productname."Short Description3");
                $product->setWebsiteIds([1]);
                $categories = ["3"];
                $product->setCategoryIds($categories);
                $product->setWeight(1);
                $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
                $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); // To make product visible in both catalog,search
                $product->setPrice($p);
                $product->setAttributeSetId(4); // Attribute set for products
                $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
                $product->setUrlKey($productsku);
                $product->setStockData(
                    array(
                        'use_config_manage_stock' => 0,
                        'manage_stock' => 1,
                        'is_in_stock' => 1,
                        'qty' => 1
                    )
                );
                $product = $this->productRepository->save($product);
                $product->save();

Like i have to add some custom data while creating a product Programmatically.How to do that ?

Any Help is appreciated.

5
  • Can you provide more information regarding the creation of the product so we can better understand the method in which you are using e.g. API / Patch / Install Data Sep 8, 2020 at 11:33
  • Updated my product create code please check @DavaGordon
    – Prits
    Sep 8, 2020 at 11:47
  • 1
    You shouldnt really use custom_data as a attribute code as getCustomData is a reserved function. but you should be able to set your data value using setCustomAttribute('myattribute', $value); or setMyattribute($value); Sep 8, 2020 at 11:52
  • Thanks for the response i have changed that no problem will try and update you @DavaGordon
    – Prits
    Sep 8, 2020 at 11:55
  • Thanks it works perfectly @DavaGordon
    – Prits
    Sep 8, 2020 at 13:45

1 Answer 1

1

Custom attributes values in magento 2 can be set and retrieved by

Getting Value getCustomAttribute('myattribute')->getValue() / getMyattribute()

Setting Value setCustomAttribute('myattribute', $value); / setMyattribute($value)

3
  • By using getCustomAttribute i will get Magento\Framework\Api\AttributeValue Object ( [_data:protected] => Array ( [attribute_code] => custom_price_value [value] => 99 ) ) How do i will get value ?@Dave
    – Prits
    Sep 8, 2020 at 17:18
  • please see my answer i have updated it you can see from your output the value is 99 so just format it like so getCustomAttribute('custom_price_value')->getValue() Sep 8, 2020 at 17:21
  • It works perfectly Thanks @Dave will you please help me with this magento.stackexchange.com/questions/320997/… question
    – Prits
    Sep 8, 2020 at 17:26

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.