Custom Attribute for quote table is not saving value

Viewed 1835

i am trying to add custom_attribute to quote table name as is_requested. this work fine, column is created. here is Upgrade schem

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()
            ->addColumn(
                $setup->getTable('quote'),
                'is_requested',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'length' => 255,
                    'nullable' => true,
                    'default' => 0,
                    'comment' => 'Requested Quote'
                ]
            );
        $setup->endSetup();
    }
}

now the problem is when i try to add value by this code

Magento\Quote\Model\QuoteFactory $quote;

$quote = $this->quote->create();
$quote->setCustomAttribute('is_requested', '1')->save();

value is not save in quote table.

1 Answers

You should add custom attributes through the InstallData or UpgradeData classes. The Schema classes are used to create custom tables.

First add the following to 'Setup/InstallData.php'

namespace [Vendor]\[Module]\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

class InstallData implements InstallDataInterface {
    private $eavSetupFactory;
    private $quoteSetupFactory;
    private $salesSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, SalesSetupFactory $salesSetupFactory, QuoteSetupFactory $quoteSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]);
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'is_requested',
            [
                'type' => 'int',
                'label' => 'Is Requested',
                'input' => 'boolean',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => false,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'enabled' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );

        $attributeOptions = [
            'type'     => 'int',
            'visible'  => true,
            'required' => false
        ];
        $quoteSetup->addAttribute('quote_item', 'is_requested', $attributeOptions);
        $salesSetup->addAttribute('order_item', 'is_requested', $attributeOptions);

    }
}

Create the file 'etc/events.xml' in your module (if it doesn't exist yet) and add this code.

<?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_item_set_product">
        <observer name="set_item_custom_attributes" instance="[Vendor]\[Module]\Observer\SetItemCustomAttributes" />
    </event>
</config>

Then create the observer at 'Observer/SetItemCustomAttributes.php' in your module and add the following code.

namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\ObserverInterface;

class SetItemCustomAttributes implements ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $quoteItem = $observer->getQuoteItem();
        $product = $observer->getProduct();
        $quoteItem->setIsRequested($product->getAttributeText('is_requested'));
    }
}

After that add the following to the 'etc/di.xml' in your module.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="iefd_delivery_quote_to_order_item" type="[Vendor]\[Module]\Plugin\IsRequestedQuoteToOrderItem"/>
    </type>
</config>

Finally create 'Plugin/IsRequestedQuoteToOrderItem.php' and add the following code.

namespace [Vendor]\[Module]\Plugin;

class IsRequestedQuoteToOrderItem
{
    public function aroundConvert(
        \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote\Item\AbstractItem $item,
        $additional = []
    ) {
        /** @var $orderItem \Magento\Sales\Model\Order\Item */
        $orderItem = $proceed($item, $additional);
        $orderItem->setIsRequested($item->getIsRequested());
        return $orderItem;
    }
}
Related