【问题标题】:Magento 2 How to set custom data / option in a quote item?Magento 2 如何在报价项目中设置自定义数据/选项?
【发布时间】:2016-07-14 07:58:33
【问题描述】:

我想在报价项目中添加一些数据,而不是产品。 我现在的做法是

$quoteItems = $this->cart->getItems();
foreach ($quoteItems as $eachQuoteItem) {
    $eachQuoteItem->setCustomname('aaaa');
    $eachQuoteItem->setIsSuperMode(true);
    $eachQuoteItem->save();
};

我可以使用 $eachQuoteItem->getCustomname();在同一页面中取回“aaaa”,但我无法在其他请求中获取数据。

有什么建议吗? 谢谢

【问题讨论】:

  • 您可以通过 Magento 2.2 中的插件和观察者来实现。* Here

标签: magento


【解决方案1】:

已经很晚了,但您可能需要按照here 的建议为这种情况创建插件。

【讨论】:

  • 我从产品页面设计了一张图片,我需要在报价和订单页面上显示,我该如何在插件的帮助下做到这一点
【解决方案2】:

提供的答案解决了将报价项目转换为订单项目的任务。但是,听起来您首先要问如何在报价项上设置数据。

您可以通过以下方式做到这一点:

a) 使用 getAllVisibleItems() 从报价中获取项目,
b) 在每个项目上致电setData('field', val)
c) 使用 setItems(items)
在报价上设置更新项目 d) 然后,保存报价

【讨论】:

    【解决方案3】:

    您可以通过以下操作将自定义字段值保存在quote_item 表中,

    创建ModuleName/CompanyName/etc/di.xml

    <?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">
            <plugin name="to_save_custom_field_to_quote_item" type="ModuleName\CompanyName\Plugin\ToQuoteItem" />
        </type>    
    </config>
    

    创建ModuleName\CompanyName\Plugin\ToQuoteItem.php

    <?php
    namespace ModuleName\CompanyName\Plugin;
    
    use Magento\Quote\Model\Quote\Item;
    
        class ToQuoteItem
        {
        
            public function afterBeforeSave(Item $subject)
            {
                $subject->setCustomField("YOUR_VALUE");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多