4

What I am trying to achieve is displaying a input field inside a bundle product where I give an option to the user to set a name for that bundle product. I am trying to transfer this user input to quote item which is create after the bundle is added to cart and later to order item. I need to display this data in the cart and in order details.

In my custom module I am creating two observers. One for writing the custom attribute in the product from the request and one for transferring it from the product to the quoteItem. I have events.xml to register the events, fields.xml to define the mapping (I am not sure is this the correct way for magento 2) and two observer classes. Here is my code:

events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer name="CustomModules_AddProductCompleteObserver" instance="CustomModules\Bundle\Model\AddProductCompleteObserver" />
    </event>
    <event name="sales_quote_item_set_product">
        <observer name="CustomModules_QuoteItemObserver" instance="CustomModules\Bundle\Model\QuoteItemObserver" />
    </event>
</config>

fields.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../../../../lib/internal/Magento/Framework/Object/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_quote_item">
            <field name="bundle_product_name">
                <aspect name="to_order_item" />
                <aspect name="to_invoice_item" />
                <aspect name="to_shipment_item" />
                <aspect name="to_cm_item" />
            </field>
        </fieldset>
        <fieldset id="sales_convert_order_item">
            <field name="bundle_product_name">
                <aspect name="to_quote_item" />
                <aspect name="to_invoice_item" />
                <aspect name="to_shipment_item" />
                <aspect name="to_cm_item" />
            </field>
        </fieldset>
    </scope>
</config>

AddProductCompleteObserver.php

...
    public function execute(\Magento\Framework\Event\Observer $observer)
{
    $product = $observer->getEvent()->getProduct();
    $reqeust = $observer->getEvent()->getRequest();
    //bundle_product_name is send by the submit form
    $bundleProductName = $reqeust->getParam('bundle_product_name');
    if(empty($bundleProductName)){
        $bundleProductName = 'No name';
    }
    $product->setBundleProductName($bundleProductName);
}
...

QuoteItemObserver.php

...
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $product = $observer->getProduct();
    $quoteItem = $observer->getQuoteItem();
    $quoteItem->setBundleProductName($product->setBundleProductName());
}
...

After that I am trying to display the quotedItem in the cart by changing Magento_Checkout/templates/cart/item/default.html in my custom theme

default.html

...
<?php echo $block->escapeHtml($_item->getBundleProductName()) ?>
...

but the printed value is empty. I tried to debug the flow and what I have found is that AddProductCompleteObserver.php correctly sets the product attribute, but it is cleared out when the next event occurs.

I will appreciate any help on that.

1

From your posted code so far, it looks like there is no database component to this. The code as is here

$quoteItem->setBundleProductName($product->setBundleProductName());

would only set this value for the length of the request and is not saved anywhere. In Magento 1 you could have added a column on the sales_flat_quote_item table and the value would be saved, however in Magento 2 I would suggest taking a look at using extension_attributes for problems like these. Even with the use of extension_attributes you would still need to handle the saving to the database part (either via an observer or a plugin).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.