I am adding custom option programatically in below observer.
<event name="checkout_cart_product_add_after">
<observer name="mymodule_checkout_cart_product_add_after" instance="ExtendStripeSubscriptionObserverCheckoutCartProductAddAfterObserver" />
</event>
and observer code in below file its not increasing quantity in same cart item , its showing separate products in cart.
<?php
namespace ExtendStripeSubscriptionObserver;
use MagentoFrameworkEventObserver as EventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkSerializeSerializerInterface;
class CheckoutCartProductAddAfterObserver implements ObserverInterface
{
/**
* @var MagentoFrameworkViewLayoutInterface
*/
protected $_layout;
/**
* @var MagentoStoreModelStoreManagerInterface
*/
protected $_storeManager;
protected $_request;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoFrameworkViewLayoutInterface $layout
*/
public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkViewLayoutInterface $layout,
MagentoFrameworkAppRequestInterface $request,
SerializerInterface $serializer
)
{
$this->_layout = $layout;
$this->_storeManager = $storeManager;
$this->_request = $request;
$this->serializer = $serializer;
}
/**
* Add order information into GA block to render on checkout success pages
*
* @param EventObserver $observer
* @return void
*/
public function execute(EventObserver $observer)
{
/* @var MagentoQuoteModelQuoteItem $item */
$item = $observer->getQuoteItem();
$additionalOptions = array();
$additionalOptions[] = [
'label' => 'Repeats Every',
'value' => '1 Month'
];
if(count($additionalOptions) > 0)
{
$item->addOption(array(
'code' => 'additional_options',
'value' => $this->serializer->serialize($additionalOptions)
));
}
/* To Do */
// Edit Cart - May need to remove option and readd them
// Pre-fill remarks on product edit pages
// Check for comparability with custom option
}
}