magento2的checkout totals添加产品属性

magento2的checkout totals添加产品属性,

totals items的数据流程

  • MagentoQuoteModelCartTotalsItem(MagentoQuoteApiDataTotalsItemInterface)

  • MagentoCheckoutModelDefaultConfigProvider::getConfig()

  • MagentoCheckoutBlockOnepage::getCheckoutConfig()

  • window.checkoutConfig.totalsData.items

  • quote.totals().items (Magento_Checkout/js/model/quote as quote)

可用plugin修改Magento\Checkout\Model\DefaultConfigProvider::getConfig()来改变totals items

以下是为totals 中的product item添加brand属性的过程

etc/frontend/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\Checkout\Model\DefaultConfigProvider">
        <plugin name="infinity_checkout_model_defaultconfigprovider" type="Infinity\Checkout\Plugin\Magento\Checkout\Model\DefaultConfigProvider" />
    </type>
</config>
 
namespace Infinity\Checkout\Plugin\Magento\Checkout\Model;

class DefaultConfigProvider
{
    protected $productRepository;
    public function __construct(
        \Magento\Catalog\Model\ProductRepository $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $return) {
        if(!empty($return['totalsData']['items']) &&
            is_array($return['totalsData']['items']) &&
            !empty($return['quoteItemData']) &&
            is_array($return['quoteItemData'])
        ) {
            $items = $return['totalsData']['items'];
            $quoteItemData = $return['quoteItemData'];
            $quoteItemData = array_column($quoteItemData, NULL, 'item_id');
            foreach($items as $key => $item) {
                $id = $quoteItemData[$item['item_id']]['product_id'];
                if($id) {
                    $product = $this->productRepository->getById($id);
                    $items[$key]['brand'] = $product->getAttributeText('general_brand');
                }
            }
            $return['totalsData']['items'] = $items;
        }
        return $return;
    }
}

上一篇:magento2 debug排查方式汇总 下一篇:Magento2.2 设置产品属性出现“The value of Admin scope can't be empty.”错误
  • 版权声明:内容来自互联网不代表本站观点,2018-05-11发表于 magento教程栏目。
  • 转载请注明: magento2的checkout totals添加产品属性| magento教程 +复制链接