0

I have form on my product page where users can enter what they want their height to be. How can I send this data through to cart? I have created a row in quote_item table to save data in already.

  • You can simple create "Height" attribute and assigned it to attribute set. – Prince Patel May 3 '17 at 9:55
  • The user says what they want their height to be, it doesn't want to be a product attribute, just one for the quote and attribute. It will be different for each order. @PrincePatel – Tom May 3 '17 at 9:57
  • If you have different for each product you can use custom option. @Tom – Prince Patel May 3 '17 at 10:13
  • It's a form that calculates a price, but I just at the moment need to pass one field from the form into the cart @PrincePatel – Tom May 3 '17 at 10:21
0

You need to create event for that:-

app/code/CompanyName/ModuleName/etc/frontend/event.xml file

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="cart_product" instance="CompanyName\ModuleName\Observer\CartProduct" />
    </event>
</config>

In observer file

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace CompanyName\ModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
class CartProduct implements ObserverInterface
{
public function __construct(
        \Magento\Framework\App\Request\Http $request)
{
  $this->_request = $request;
}
 public function execute(\Magento\Framework\Event\Observer $observer)
   {
      $height= $this->_request->getPost('height');
      $item = $observer->getQuoteItem();
      $item->setHeight($height);
      //if you want to add as custom option then 
        $item->addOption(
        array(
             'code'  => 'height',
             'value' => $height
        )
    ); 
   }
}

After that flush cache and check it.

  • It's a form that calculates a price, but I just at the moment need to pass one field from the form into the cart – Tom May 3 '17 at 10:21
  • if that not work then i will explain another way – Sunil Patel May 3 '17 at 10:21
  • what is the other way? – Tom May 3 '17 at 10:22
  • Thank you, and then how would I display the height in the cart? – Tom May 3 '17 at 10:41
  • if you want to show into cart without cart file modified then you need to add custom option progrmically. – Sunil Patel May 3 '17 at 10:50

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.