1

I've created custom option to store text for products programatically using after save observer as below:

/var/www/html/mylocal/app/code/Custom/Addcustomoption/Observer/Catalog/ProductSaveAfter.php

<?php
namespace Custom\Addcustomoption\Observer\Catalog;


use Magento\Catalog\Model\Product\Option;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory as CustomOptionFactory;

class AddCustomOptions implements ObserverInterface
{
    /**
     * @var ProductRepository
     */
    protected $productRepository;

    /**
     * @var CustomOptionFactory
     */
    protected $customOptionFactory;

    /**
     * @var ProductCustomOptionInterface
     */
    protected $productCustomOption;

    /**
     * AddCustomOptions constructor.
     *
     * @param ProductRepository            $productRepository
     * @param ProductCustomOptionInterface $productCustomOption
     */
    public function __construct(
        ProductRepository $productRepository,
        ProductCustomOptionInterface $productCustomOption
    ) {
        $this->productRepository = $productRepository;
        $this->productCustomOption = $productCustomOption;
    }

    /**
     * {@inheritdoc}
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $observer->getData('product');


        try{
            $pop = $product->getPop();
            if( !$product->getHasOptions() /*&& $pop != NULL*/){

                $customOptionData = [
                    Option::KEY_TITLE => 'Customer Profile',
                    Option::KEY_TYPE => 'field',
                    Option::KEY_IS_REQUIRE => true,
                    Option::KEY_SORT_ORDER => 1,
                    Option::KEY_PRICE => 0.00,
                    Option::KEY_PRICE_TYPE => 'fixed',
                    Option::KEY_MAX_CHARACTERS => 100,
                    Option::KEY_PRODUCT_SKU => $product->getSku()

                ];
                $customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
                $customOptions[] = $customOption;
                $product->setOptions($customOptions)->setCanSaveCustomOptions(true)->save();    
                }
            } catch (\Exception $e) {
                var_dump($e->getMessage());
            }
    }
    /**
     * @return CustomOptionFactory
     */
    private function getCustomOptionFactory()
    {
        if (null === $this->customOptionFactory) {
            $this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
                ->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
        }
        return $this->customOptionFactory;
    }
}

In default the custom option input field is present in product detail page. I do not want to use this input field so that I've hidden it.

In cart page for each product I take input from a dropdown for each product which comes dynamically based on logged in user. I need to save that value selected by user to the custom option field programmatically.

https://i.stack.imgur.com/sYYfj.png

How can I do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.