0

Hi i am trying to save custom field on the click of add to cart button to quote and sales_order table.

below is my upgradeschema.php

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1') < 0) {

        $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();
        //cart table
        $connection->addColumn(
            $installer->getTable('quote'),
            'subscription_type',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' =>'Subscription Type'
            ]

        );
        $connection->addColumn(
            $installer->getTable('quote'),
            'start_date',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' =>'Start Date'
            ]

        );

        //Order address table
        $connection->addColumn(
            $installer->getTable('sales_order'),
            'subscription_type',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' =>'Subscription Type'

            ]
        );

        $connection->addColumn(
            $installer->getTable('sales_order'),
            'start_date',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' =>'Start Date'

            ]
        );
     $installer->endSetup(); }
    }
}

Events.xml

<event name="sales_model_service_quote_submit_before">
            <observer name="customfields" instance="Envision\Ecomm\Observer\CustomOptionSave" />


        </event>

customoptionsave.php

class CustomOptionSave implements ObserverInterface
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_logger;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $quoteFactory;

    /**
     * Constructor
     *
     * @param \Psr\Log\LoggerInterface $logger
     */

    public function __construct(\Psr\Log\LoggerInterface $logger,
                               \Magento\Quote\Model\QuoteFactory $quoteFactory) {
        $this->_logger = $logger;
        $this->quoteFactory =   $quoteFactory;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getOrder();
        $quoteId = $order->getQuoteId();
        $quote  =   $this->quoteFactory->create()->load($quoteId);
        $subscriptionType = $quote->getSubscriptionType();
        $order->setSubscriptionType($quote->getSubscriptionType());
        $order->save();
    }   
}

my phtml file

<select name="subscription_type" class="subscription_name" id="subscription_id">
  <option selected="selected">Choose A Plan</option>
  <?php

    $json = $this->helper('Vendor\Module\Helper\Data')->getConfig('general/quantity_ranges/ranges');
    $arr = json_decode($json, true);
    // $products =  json_decode($json, true);
    // Iterating through the product array
    foreach($arr as $data){
      //  echo "<option value= >". $data['plan']." : " ." - "."Fee". $data['price']."INR"."</option>";
      echo "<option value='".$data["price"]."'>". $data['plan']." : " ." - "."Fee". $data['price']."INR"."</option>";
    }
  ?>
</select>

any idea on what am i missing my custom fields are not getting saved in db thanks in advance

8
  • things you first check: 1- subscription_type column is available in table 2- Your observer customoptionsave is calling properly 3- You are getting value in observer – Pawan Jun 11 at 11:04
  • 1
    you need to first save the subscription_type value in the quote table when you click on add to cart button – Devidas Jun 11 at 11:25
  • one's you successfully save the value in quote table then you get in value in observer – Devidas Jun 11 at 11:26
  • am not getting value in $quote->getSubscriptionType(); and am not able to find out why – Pramod Jun 11 at 11:44
  • Yes, first you need to save data in quote table. – Pawan Jun 11 at 12:15

Your Answer

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

Browse other questions tagged or ask your own question.