0

I have created a custom column named 'campaign_id' through UpgradeSchema in quote table as below.

$installer->getConnection()->addColumn(
     $installer->getTable('quote'),
     'campaign_id',
     [
         'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
         'length' => 11,
         'nullable' => false,
         'comment' => 'Campaign ID'
     ]
);

I am setting its value through custom controller on add to cart as below

// \Magento\Checkout\Model\Session

$this->checkoutSession->getQuote()
     ->setCampaignId($campaign_id)
     ->save();

The value is getting saved properly when I'm adding product as a guest user or as a logged in user.

However when I add the product as a guest user and then log in, the custom quote column value from guest quote is not getting copied to customer quote on quote merge.

2

I did it using the sales_quote_merge_after event

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="sales_quote_merge_after">
        <observer name="sales_quote_merge_custom_column" instance="Namespace\Module\Observer\SalesQuoteMergeAfter" />
    </event>
</config>

SalesQuoteMergeAfter.php

<?php
namespace Namespace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalesQuoteMergeAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $guestQuote = $observer->getEvent()->getSource();   //before merge
        $customerQuote = $observer->getEvent()->getQuote(); //after merge

        if($guestQuote->getCampaignBlockId()) {
            $customerQuote->setCampaignBlockId($guestQuote->getCampaignBlockId());
        }
    }
}

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.