I'm trying to add custom option programmatically after product save event. I've found some solution on stackexchange which are not working for me. So I'm posting all my codes here for finding any errors in it.
This is my observer class:
class ProductSaveAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
protected $ObjectManager;
protected $product;
public function __construct(
\Magento\Framework\ObjectManagerInterface $ObjectManager,
\Magento\Catalog\Model\Product $product
){
$this->_objectManager = $ObjectManager;
$this->_product = $product;
}
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$_product = $observer->getProduct(); // you will get product object
$productId = $_product->getId();
try{
$customOption = $this->_objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterface');
$var = NULL;
$customOption
->setOptionId($var)
->setProductId($productId)
->setType('field')
->setIsRequire("0")
->setSku("")
->setMaxCharacters("0")
->setFileExtension("")
->setImageSizeX("0")
->setImageSizeY("0")
->setSortOrder("1")
->setDefaultTitle("Profile Name")
->setStoreTitle("Profile Name")
->setTitle("Profile Name")
->setDefaultPrice("0.00")
->setDefaultPriceType("fixed")
->setStorePrice("0.00")
->setStorePriceType("0.00")
->setPrice("0.00")
->setPriceType('fixed')
->setRecordId("0")
->setProductSku($_product->getSku());
$customOptions[] = $customOption;
$_product->setOptions($customOptions)->save();
} catch (\Exception $e) {
var_dump($e->getMessage());
}
}
}
After saving data from backend the page does not respond due to this code. What is the problem with it?
Another instance of code is as follows which I found in this link https://magento.stackexchange.com/questions/97014/magento2-programmatically-adding-a-custom-option
<?php
namespace Custom\Addcustomoption\Observer\Catalog;
class ProductSaveAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
protected $ObjectManager;
protected $product;
public function __construct(
\Magento\Framework\ObjectManagerInterface $ObjectManager,
\Magento\Catalog\Model\Product $product
){
$this->_objectManager = $ObjectManager;
$this->_product = $product;
}
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$_product = $observer->getProduct(); // you will get product object
$productId = $_product->getId();
try{
/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $customOption */
$customOption = $this->objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterface');
$customOption->setTitle('Text')
->setType('area')
->setIsRequire(true)
->setSortOrder(1)
->setPrice(1.00)
->setPriceType('fixed')
->setMaxCharacters(50)
->setProductSku($_product->getSku());
$customOptions[] = $customOption;
$_product->setOptions($customOptions)->save();
} catch (\Exception $e) {
var_dump($e->getMessage());
}
}
}
The problem with this is that the product gets saved but no custom options are seen in backend. Any suggestions, solutions to this problem?