How To Add Additional Options In Magento 2 Cart
I used this to Add Additional Options In Magento 2 Cart
https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/
But this is working only with Simple product.
For configurable products, I did the following change
$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));
After that, the additional_options
going to the cart. But it is not getting saved when complete the order.
seems because of this ….
//filter out config/bundle etc product
if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
{
$this->quoteItems[$item->getId()] = $item;
}
How can I fix it ? I need this functionality for configurable products.
magento2 quoteitem
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I used this to Add Additional Options In Magento 2 Cart
https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/
But this is working only with Simple product.
For configurable products, I did the following change
$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));
After that, the additional_options
going to the cart. But it is not getting saved when complete the order.
seems because of this ….
//filter out config/bundle etc product
if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
{
$this->quoteItems[$item->getId()] = $item;
}
How can I fix it ? I need this functionality for configurable products.
magento2 quoteitem
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I used this to Add Additional Options In Magento 2 Cart
https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/
But this is working only with Simple product.
For configurable products, I did the following change
$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));
After that, the additional_options
going to the cart. But it is not getting saved when complete the order.
seems because of this ….
//filter out config/bundle etc product
if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
{
$this->quoteItems[$item->getId()] = $item;
}
How can I fix it ? I need this functionality for configurable products.
magento2 quoteitem
I used this to Add Additional Options In Magento 2 Cart
https://livesubwpcache.cloudways.com/blog/add-additional-options-in-magento-2/
But this is working only with Simple product.
For configurable products, I did the following change
$item->addOption(array(
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => serialize($additionalOptions)
));
After that, the additional_options
going to the cart. But it is not getting saved when complete the order.
seems because of this ….
//filter out config/bundle etc product
if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
{
$this->quoteItems[$item->getId()] = $item;
}
How can I fix it ? I need this functionality for configurable products.
magento2 quoteitem
magento2 quoteitem
edited Jun 30 '17 at 10:52
Rama Chandran M
2,71181430
2,71181430
asked Jun 30 '17 at 9:30
Samantha RajasinghaSamantha Rajasingha
63
63
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Step 1: Create registration.php and module.xml file the for custom module.
Step 2: Assign Observers to Events
/app/code/Namespace/Mymodule/etc/events.xml
<?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="catalog_product_load_after">
<observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
</event>
</config>
Step 3: Create the Observers
app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php
<?php
namespace NamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
class SetAdditionalOptions implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->_request = $request;
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$product = $observer->getProduct();
$additionalOptions = ;
$additionalOptions = array(
'label' => "Some Label",
'value' => "Your Information",
);
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
run below command after
rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
reference links
https://www.cloudways.com/blog/add-additional-options-in-magento-2/
https://webkul.com/blog/additional-options-cart-item-magento2/
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
add a comment |
After add additional options to cart You need to create another observer for save additional attributes order item from quote item.
Please add this event observer code in Module's etc/events.xml
<event name="sales_model_service_quote_submit_before">
<observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
</event>
Please create observer file in your module
VendorModuleObserverSalesOrderItemAdditionalOptions.php
And here is Observer code for save Additional Options to Order Item.
<?php
namespace VendorModuleObserverSales;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkSerializeSerializerJson;
class OrderItemAdditionalOptions implements ObserverInterface
{
public function __construct(
Json $serializer = null
) {
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
try {
$quote = $observer->getQuote();
$order = $observer->getOrder();
$quoteItems = ;
// Map Quote Item with Quote Item Id
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$quoteItems[$quoteItem->getId()] = $quoteItem;
}
foreach ($order->getAllVisibleItems() as $orderItem) {
$quoteItemId = $orderItem->getQuoteItemId();
$quoteItem = $quoteItems[$quoteItemId];
$additionalOptions = $quoteItem->getOptionByCode('additional_options');
if (count($additionalOptions) > 0) {
// Get Order Item's other options
$options = $orderItem->getProductOptions();
// Set additional options to Order Item
$options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
} catch (Exception $e) {
// catch error if any
}
}
}
Please check this I think it will help you.
add a comment |
after $item->addOption(...)
try calling $item->saveItemOptions()
.
Also a side note, don't add the options as serialized using serialize($additionalOptions)
, use json_encode
or better yet the method MagentoFrameworkSerializeSerializerJson::serialize
.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181480%2fhow-to-add-additional-options-in-magento-2-cart%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Step 1: Create registration.php and module.xml file the for custom module.
Step 2: Assign Observers to Events
/app/code/Namespace/Mymodule/etc/events.xml
<?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="catalog_product_load_after">
<observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
</event>
</config>
Step 3: Create the Observers
app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php
<?php
namespace NamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
class SetAdditionalOptions implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->_request = $request;
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$product = $observer->getProduct();
$additionalOptions = ;
$additionalOptions = array(
'label' => "Some Label",
'value' => "Your Information",
);
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
run below command after
rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
reference links
https://www.cloudways.com/blog/add-additional-options-in-magento-2/
https://webkul.com/blog/additional-options-cart-item-magento2/
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
add a comment |
Step 1: Create registration.php and module.xml file the for custom module.
Step 2: Assign Observers to Events
/app/code/Namespace/Mymodule/etc/events.xml
<?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="catalog_product_load_after">
<observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
</event>
</config>
Step 3: Create the Observers
app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php
<?php
namespace NamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
class SetAdditionalOptions implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->_request = $request;
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$product = $observer->getProduct();
$additionalOptions = ;
$additionalOptions = array(
'label' => "Some Label",
'value' => "Your Information",
);
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
run below command after
rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
reference links
https://www.cloudways.com/blog/add-additional-options-in-magento-2/
https://webkul.com/blog/additional-options-cart-item-magento2/
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
add a comment |
Step 1: Create registration.php and module.xml file the for custom module.
Step 2: Assign Observers to Events
/app/code/Namespace/Mymodule/etc/events.xml
<?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="catalog_product_load_after">
<observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
</event>
</config>
Step 3: Create the Observers
app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php
<?php
namespace NamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
class SetAdditionalOptions implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->_request = $request;
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$product = $observer->getProduct();
$additionalOptions = ;
$additionalOptions = array(
'label' => "Some Label",
'value' => "Your Information",
);
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
run below command after
rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
reference links
https://www.cloudways.com/blog/add-additional-options-in-magento-2/
https://webkul.com/blog/additional-options-cart-item-magento2/
Step 1: Create registration.php and module.xml file the for custom module.
Step 2: Assign Observers to Events
/app/code/Namespace/Mymodule/etc/events.xml
<?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="catalog_product_load_after">
<observer name="set_additional_options" instance="NamespaceMymoduleObserverSetAdditionalOptions"/>
</event>
</config>
Step 3: Create the Observers
app/code/Namespace/Mymodule/Observer/SetAdditionalOptions.php
<?php
namespace NamespaceMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
class SetAdditionalOptions implements ObserverInterface
{
/**
* @var RequestInterface
*/
protected $_request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->_request = $request;
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
// Check and set information according to your need
if ($this->_request->getFullActionName() == 'checkout_cart_add') { //checking when product is adding to cart
$product = $observer->getProduct();
$additionalOptions = ;
$additionalOptions = array(
'label' => "Some Label",
'value' => "Your Information",
);
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
run below command after
rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
reference links
https://www.cloudways.com/blog/add-additional-options-in-magento-2/
https://webkul.com/blog/additional-options-cart-item-magento2/
edited Aug 2 '18 at 14:42
Abhishek Panchal
3,4233929
3,4233929
answered Jun 30 '17 at 11:06
JjoJjo
795114
795114
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
add a comment |
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
Thanks for your answer. But still the same issue. The additional options not in the back-end when I order configurable products.
– Samantha Rajasingha
Jun 30 '17 at 12:12
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
@SamanthaRajasingha - Can you refer the above both links?
– Jjo
Jun 30 '17 at 12:14
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
I used those links already, I got the same issue from there.
– Samantha Rajasingha
Jun 30 '17 at 12:19
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
how to add additional options from the controller by item id in magento 2
– G Prathap
Oct 30 '18 at 15:26
add a comment |
After add additional options to cart You need to create another observer for save additional attributes order item from quote item.
Please add this event observer code in Module's etc/events.xml
<event name="sales_model_service_quote_submit_before">
<observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
</event>
Please create observer file in your module
VendorModuleObserverSalesOrderItemAdditionalOptions.php
And here is Observer code for save Additional Options to Order Item.
<?php
namespace VendorModuleObserverSales;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkSerializeSerializerJson;
class OrderItemAdditionalOptions implements ObserverInterface
{
public function __construct(
Json $serializer = null
) {
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
try {
$quote = $observer->getQuote();
$order = $observer->getOrder();
$quoteItems = ;
// Map Quote Item with Quote Item Id
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$quoteItems[$quoteItem->getId()] = $quoteItem;
}
foreach ($order->getAllVisibleItems() as $orderItem) {
$quoteItemId = $orderItem->getQuoteItemId();
$quoteItem = $quoteItems[$quoteItemId];
$additionalOptions = $quoteItem->getOptionByCode('additional_options');
if (count($additionalOptions) > 0) {
// Get Order Item's other options
$options = $orderItem->getProductOptions();
// Set additional options to Order Item
$options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
} catch (Exception $e) {
// catch error if any
}
}
}
Please check this I think it will help you.
add a comment |
After add additional options to cart You need to create another observer for save additional attributes order item from quote item.
Please add this event observer code in Module's etc/events.xml
<event name="sales_model_service_quote_submit_before">
<observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
</event>
Please create observer file in your module
VendorModuleObserverSalesOrderItemAdditionalOptions.php
And here is Observer code for save Additional Options to Order Item.
<?php
namespace VendorModuleObserverSales;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkSerializeSerializerJson;
class OrderItemAdditionalOptions implements ObserverInterface
{
public function __construct(
Json $serializer = null
) {
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
try {
$quote = $observer->getQuote();
$order = $observer->getOrder();
$quoteItems = ;
// Map Quote Item with Quote Item Id
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$quoteItems[$quoteItem->getId()] = $quoteItem;
}
foreach ($order->getAllVisibleItems() as $orderItem) {
$quoteItemId = $orderItem->getQuoteItemId();
$quoteItem = $quoteItems[$quoteItemId];
$additionalOptions = $quoteItem->getOptionByCode('additional_options');
if (count($additionalOptions) > 0) {
// Get Order Item's other options
$options = $orderItem->getProductOptions();
// Set additional options to Order Item
$options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
} catch (Exception $e) {
// catch error if any
}
}
}
Please check this I think it will help you.
add a comment |
After add additional options to cart You need to create another observer for save additional attributes order item from quote item.
Please add this event observer code in Module's etc/events.xml
<event name="sales_model_service_quote_submit_before">
<observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
</event>
Please create observer file in your module
VendorModuleObserverSalesOrderItemAdditionalOptions.php
And here is Observer code for save Additional Options to Order Item.
<?php
namespace VendorModuleObserverSales;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkSerializeSerializerJson;
class OrderItemAdditionalOptions implements ObserverInterface
{
public function __construct(
Json $serializer = null
) {
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
try {
$quote = $observer->getQuote();
$order = $observer->getOrder();
$quoteItems = ;
// Map Quote Item with Quote Item Id
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$quoteItems[$quoteItem->getId()] = $quoteItem;
}
foreach ($order->getAllVisibleItems() as $orderItem) {
$quoteItemId = $orderItem->getQuoteItemId();
$quoteItem = $quoteItems[$quoteItemId];
$additionalOptions = $quoteItem->getOptionByCode('additional_options');
if (count($additionalOptions) > 0) {
// Get Order Item's other options
$options = $orderItem->getProductOptions();
// Set additional options to Order Item
$options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
} catch (Exception $e) {
// catch error if any
}
}
}
Please check this I think it will help you.
After add additional options to cart You need to create another observer for save additional attributes order item from quote item.
Please add this event observer code in Module's etc/events.xml
<event name="sales_model_service_quote_submit_before">
<observer instance="VendorModuleObserverSalesOrderItemAdditionalOptions" name="vendor_module_observer_sales_model_service_quote_submit_before"/>
</event>
Please create observer file in your module
VendorModuleObserverSalesOrderItemAdditionalOptions.php
And here is Observer code for save Additional Options to Order Item.
<?php
namespace VendorModuleObserverSales;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkSerializeSerializerJson;
class OrderItemAdditionalOptions implements ObserverInterface
{
public function __construct(
Json $serializer = null
) {
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $observer)
{
try {
$quote = $observer->getQuote();
$order = $observer->getOrder();
$quoteItems = ;
// Map Quote Item with Quote Item Id
foreach ($quote->getAllVisibleItems() as $quoteItem) {
$quoteItems[$quoteItem->getId()] = $quoteItem;
}
foreach ($order->getAllVisibleItems() as $orderItem) {
$quoteItemId = $orderItem->getQuoteItemId();
$quoteItem = $quoteItems[$quoteItemId];
$additionalOptions = $quoteItem->getOptionByCode('additional_options');
if (count($additionalOptions) > 0) {
// Get Order Item's other options
$options = $orderItem->getProductOptions();
// Set additional options to Order Item
$options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());
$orderItem->setProductOptions($options);
}
}
} catch (Exception $e) {
// catch error if any
}
}
}
Please check this I think it will help you.
answered Sep 8 '18 at 10:45
Kishor ThummarKishor Thummar
775
775
add a comment |
add a comment |
after $item->addOption(...)
try calling $item->saveItemOptions()
.
Also a side note, don't add the options as serialized using serialize($additionalOptions)
, use json_encode
or better yet the method MagentoFrameworkSerializeSerializerJson::serialize
.
add a comment |
after $item->addOption(...)
try calling $item->saveItemOptions()
.
Also a side note, don't add the options as serialized using serialize($additionalOptions)
, use json_encode
or better yet the method MagentoFrameworkSerializeSerializerJson::serialize
.
add a comment |
after $item->addOption(...)
try calling $item->saveItemOptions()
.
Also a side note, don't add the options as serialized using serialize($additionalOptions)
, use json_encode
or better yet the method MagentoFrameworkSerializeSerializerJson::serialize
.
after $item->addOption(...)
try calling $item->saveItemOptions()
.
Also a side note, don't add the options as serialized using serialize($additionalOptions)
, use json_encode
or better yet the method MagentoFrameworkSerializeSerializerJson::serialize
.
answered Dec 17 '18 at 8:35
Marius♦Marius
164k28312662
164k28312662
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181480%2fhow-to-add-additional-options-in-magento-2-cart%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown