1

I have added a custom attribute in PDP page, I want to get that value in Observer/SaveToQuote.php to save in the quote table after the product is added to cart. I am using the event checkout_cart_product_add_after . Can anyone tell me how to do this? the custom attribute is comments

here is my pdp page code

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View
 */
// form action  is here:- action="<?= $block->escapeUrl($block->getSubmitUrl($_product))




?>
<?php $_helper = $this->helper(Magento\Catalog\Helper\Output::class); ?>
<?php $_product = $block->getProduct(); ?>

<div class="product-add-form">
    <form data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>"
           method="post" action="<?= $block->escapeUrl($block->getSubmitUrl($_product))?>"
          id="product_addtocart_form"<?php if ($_product->getOptions()) :?> enctype="multipart/form-data"<?php endif; ?>>
        <input type="hidden" name="product" value="<?= (int)$_product->getId() ?>" />
        <input type="hidden" name="selected_configurable_option" value="" />
        <input type="hidden" name="related_product" id="related-products-field" value="" />
        <input type="hidden" name="item"  value="<?= (int)$block->getRequest()->getParam('id') ?>" />
          <h4>This is custom attribute comments section in PDP page </h4>
          Custom Attribute Comments :  <input type="text" id="comments" name="comments" value="">
        <?= $block->getBlockHtml('formkey') ?>
        <?= $block->getChildHtml('form_top') ?>
        <?php if (!$block->hasOptions()) :?>
            <?= $block->getChildHtml('product_info_form_content') ?>
        <?php else :?>
            <?php if ($_product->isSaleable() && $block->getOptionsContainer() == 'container1') :?>
                <?= $block->getChildChildHtml('options_container') ?>
            <?php endif;?>
        <?php endif; ?>

        <?php if ($_product->isSaleable() && $block->hasOptions() && $block->getOptionsContainer() == 'container2') :?>
            <?= $block->getChildChildHtml('options_container') ?>
        <?php endif;?>
        <?= $block->getChildHtml('form_bottom') ?>
    </form>
</div>

<script type="text/x-magento-init">
    {
        "[data-role=priceBox][data-price-box=product-id-<?= $block->escapeHtml($_product->getId()) ?>]": {
            "priceBox": {
                "priceConfig":  <?= /* @noEscape */ $block->getJsonConfig() ?>
            }
        }
    }
</script>

here is my Observer/SaveToQuote.php code

<?php

namespace DCKAP\Comments\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;


class SaveToQuote implements ObserverInterface
{
    /**
     * Below is the method that will fire whenever the event runs!
     *
     * @param Observer $observer
     */

    /**
      *request parameter
      *
      *@param RequestInterface $request
      */

      /**
      * @var \Magento\Quote\Model\QuoteRepository
      */
       protected $quoteRepository;

     public function _costruct(
       \Magento\Quote\Model\QuoteRepository $quoteRepository
       )
     {
       $this->quoteRepository = $quoteRepository;

     }
    public function execute(Observer $observer)
    {
        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/savetoquote.log');
        $logger = new \Zend_Log();
        $logger->addWriter($writer);
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $quote = $quoteItem->getQuote();
        $quoteId = $quote->getId();
        $logger->info("l45 the quoteId value is ".$quoteId);

         $product = $observer->getProduct(); // or $observer->getData('product');
         $comments = $product->getCustomAttribute('comments');
        // $comments = $this->getRequest()->getParam('comments');

        // $logger->info("l51 the comments value is ".$comments);

        
        
        $quote->setData('comments', $comments); // Fill data
        $this->quoteRepository->save($quote); // Save quote
    }
}

the comments value is empty in savetoquote.php

Any other idea to save the custom attribute in quote table is also appreciable.

1
  • Is "comments" an input field? if so then you need to get it from the form post. Commented Aug 24, 2022 at 9:32

1 Answer 1

1

Since "comments" is an input field, in your observer you need to get the data from Post data using RequestInterface

<?php

namespace DCKAP\Comments\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;


class SaveToQuote implements ObserverInterface
{
    /**
     * Below is the method that will fire whenever the event runs!
     *
     * @param Observer $observer
     */

    /**
      *request parameter
      *
      *@param RequestInterface $request
      */

      /**
      * @var \Magento\Quote\Model\QuoteRepository
      */
       protected $quoteRepository;

     public function _costruct(
       ....
       RequestInterface $request
       )
     {
       ....
       $this->request = $request;

     }
    public function execute(Observer $observer)
    {
        ....
         $comments = $this->request->getPostValue('comments');
        // $comments = $this->getRequest()->getParam('comments');

        // $logger->info("l51 the comments value is ".$comments);



        $quote->setData('comments', $comments); // Fill data
        $this->quoteRepository->save($quote); // Save quote
    }
}
1
  • I will try and update you @Magepal Commented Aug 24, 2022 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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