You can use the event checkout_cart_product_add_after
to modify the product image information.
Firstly in your product detail page you need to add a hidden field in the add to cart form something like:
<input type="hidden" name="[option][front_designurl]" id="front_designurl"/>
And using javascript add the value to the field for the generated image url by the user, this value gets saved in the info_buyRequest
of the quote item options
We have to create the file app/code/Foo/CustomImage/etc/events.xml
to attach observers to the events:
- checkout_cart_product_add_after: Event is fired on Add to Cart
- checkout_cart_product_update_after: Event is fired on Cart Update (For add to cart from cart edit page)
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="foo_customimage_observer_set_price_for_item_add" instance="Foo\CustomImage\Model\Observer\SetImageForItem"/>
</event>
<event name="checkout_cart_product_update_after">
<observer name="foo_customimage_observer_set_price_for_item_update" instance="Foo\CustomImage\Model\Observer\SetImageForItem"/>
</event>
</config>
Now for the observer logic we create a file at app/code/Foo/CustomImage/Model/Observer/SetImageForItem.php
<?php
namespace Foo\CustomImage\Model\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\ProductOptionFactory;
use Magento\Quote\Api\Data\ProductOptionExtensionFactory;
use Magento\Catalog\Model\CustomOptions\CustomOptionFactory;
class SetImageForItem implements ObserverInterface
{
/** @var \Magento\Quote\Model\Quote\ProductOptionFactory */
protected $productOptionFactory;
/** @var \Magento\Quote\Api\Data\ProductOptionExtensionFactory */
protected $extensionFactory;
/** @var CustomOptionFactory */
protected $customOptionFactory;
/**
* @param ProductOptionFactory $productOptionFactory
* @param ProductOptionExtensionFactory $extensionFactory
* @param CustomOptionFactory $customOptionFactory
*/
public function __construct(
ProductOptionFactory $productOptionFactory,
ProductOptionExtensionFactory $extensionFactory
CustomOptionFactory $customOptionFactory
) {
$this->productOptionFactory = $productOptionFactory;
$this->extensionFactory = $extensionFactory;
$this->customOptionFactory = $customOptionFactory;
}
public function execute(Observer $observer)
{
/** @var $item \Magento\Quote\Model\Quote\Item */
$item = $observer->getEvent()->getQuoteItem();//Gets the Quote Item Instance
$request = $item->getBuyRequest(); // Gets the posted data sent to "Add to cart" controller action
$this->processOptions($item);
return $this;
}//end execute()
/**
* @inheritDoc
*/
public function processOptions(CartItemInterface $cartItem)
{
$options = $this->getOptions($cartItem);
if (!empty($options) && is_array($options)) {
$this->updateOptionsValues($options);
$productOption = $cartItem->getProductOption()
? $cartItem->getProductOption()
: $this->productOptionFactory->create();
/** @var \Magento\Quote\Api\Data\ProductOptionExtensionInterface $extensibleAttribute */
$extensibleAttribute = $productOption->getExtensionAttributes()
? $productOption->getExtensionAttributes()
: $this->extensionFactory->create();
$extensibleAttribute->setCustomOptions($options);
$productOption->setExtensionAttributes($extensibleAttribute);
$cartItem->setProductOption($productOption);
}
}
/**
* Receive custom option from buy request
*
* @param CartItemInterface $cartItem
* @return array
*/
protected function getOptions(CartItemInterface $cartItem)
{
$buyRequest = !empty($cartItem->getOptionByCode('info_buyRequest'))
? unserialize($cartItem->getOptionByCode('info_buyRequest')->getValue())
: null;
return is_array($buyRequest) && isset($buyRequest['options'])
? $buyRequest['options']
: [];
}
/**
* Update options values
*
* @param array $options
* @return null
*/
protected function updateOptionsValues(array &$options)
{
foreach ($options as $optionId => &$optionValue) {
/** @var \Magento\Catalog\Model\CustomOptions\CustomOption $option */
$option = $this->customOptionFactory->create();
$option->setOptionId($optionId);
if (is_array($optionValue)) {
$optionValue = implode(',', $optionValue);
}
$option->setOptionValue($optionValue);
$optionValue = $option;
}
}
}
I have not tried out the code, but should help you add new data to your product option.