【问题标题】:Magento 2 - add extra product attribute to checkout summary boxMagento 2 - 在结帐摘要框中添加额外的产品属性
【发布时间】:2017-05-26 14:04:36
【问题描述】:

如何将产品的额外属性添加到结帐摘要框中?

我必须覆盖:Magento_Catalog/web/template/summary/item/details.html

<div class="product-item-inner">
    <div class="product-item-name-block">
        <strong class="product-item-name" data-bind="text: $parent.name"></strong>
        <strong class="product-item-authors">**Author goes here!**</strong>
        <div class="details-qty">
            <span class="label"><!-- ko i18n: 'Qty' --><!-- /ko --></span>
            <span class="value" data-bind="text: $parent.qty"></span>
        </div>
    </div>
    <!-- ko foreach: getRegion('after_details') -->
        <!-- ko template: getTemplate() --><!-- /ko -->
    <!-- /ko -->
</div>

所以你看到的实际文本“作者在这里!”我必须调用 $parent.authors 之类的东西。

product[authors] 是后端目录中的多选属性。

Checkout summary box - image

【问题讨论】:

    标签: knockout.js checkout magento2


    【解决方案1】:

    我创建了一个在结帐摘要中添加自定义属性“ma​​ster_id”的模块。

    我参考了这个链接https://www.magevision.com/blog/post/get-a-product-attribute-in-checkout-summary-magento-2/

    SampWork/ConfigCheckoutDynamicPCB/registration.php

    <?php
    
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'SampWork_ConfigCheckoutDynamicPCB',
        __DIR__
    );
    

    SampWork/ConfigCheckoutDynamicPCB/etc/module.xml

    <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
            <module name="SampWork_ConfigCheckoutDynamicPCB" setup_version="1.0.0"> </module>
        </config>
    

    SampWork/ConfigCheckoutDynamicPCB/etc/catalog_attributes.xml

    <?xml version="1.0"?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
        <group name="quote_item">
            <attribute name="master_id"/>
        </group>
    </config>
    

    SampWork/ConfigCheckoutDynamicPCB/etc/di.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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="checkout-summary-product-attribute" type="SampWork\ConfigCheckoutDynamicPCB\Plugin\Checkout\Model\DefaultConfigProvider" />
        </type>
    </config>
    

    SampWork/ConfigCheckoutDynamicPCB/Plugin/Checkout/Model/DefaultConfigProvider.php

    <?php
    namespace SampWork\ConfigCheckoutDynamicPCB\Plugin\Checkout\Model;
    
    use Magento\Checkout\Model\Session as CheckoutSession;
    
    class DefaultConfigProvider
    {
        /**
         * @var CheckoutSession
         */
        protected $checkoutSession;
    
        /**
         * Constructor
         *
         * @param CheckoutSession $checkoutSession
         */
        public function __construct(
            CheckoutSession $checkoutSession
        ) {
            $this->checkoutSession = $checkoutSession;
        }
    
        public function afterGetConfig(
            \Magento\Checkout\Model\DefaultConfigProvider $subject,
            array $result
        ) {
            $items = $result['totalsData']['items'];
            foreach ($items as $index => $item) {
                $quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
                $result['quoteItemData'][$index]['master_id'] = $quoteItem->getProduct()->getData('master_id');
            }
            return $result;
        }
    }
    

    SampWork/SampWork/ConfigCheckoutDynamicPCB/view/frontend/web/js/view/summary/item/details.js

    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    define(
        [
            'uiComponent'
        ],
        function (Component) {
            "use strict";
            var quoteItemData = window.checkoutConfig.quoteItemData;
            return Component.extend({
                defaults: {
                    template: 'SampWork_ConfigCheckoutDynamicPCB/summary/item/details'
                },
                quoteItemData: quoteItemData,
                getValue: function(quoteItem) {
                    return quoteItem.name;
                },
                getMaster: function(quoteItem) {
                    var item = this.getItem(quoteItem.item_id);
                    if(item.master_id){
                        return 'Master ID'+item.master_id;
                    }else{
                        return '';
                    }
                },
                getItem: function(item_id) {
                    var itemElement = null;
                    _.each(this.quoteItemData, function(element, index) {
                        if (element.item_id == item_id) {
                            itemElement = element;
                        }
                    });
                    return itemElement;
                }
            });
        }
    );
    

    SampWork/ConfigCheckoutDynamicPCB/view/frontend/web/template/summary/item/details.html

    <!-- ko foreach: getRegion('before_details') -->
        <!-- ko template: getTemplate() --><!-- /ko -->
    <!-- /ko -->
    <div class="product-item-details">
    
        <div class="product-item-inner">
            <div class="product-item-name-block">
                <strong class="product-item-name" data-bind="text: $parent.name"></strong>
                 <!-- ko if: (getMaster($parent))-->
                    <span class="product-item-pcb-master" data-bind="text: getMaster($parent)"></span>
                <!-- /ko -->
                <div class="details-qty">
                    <span class="label"><!-- ko i18n: 'Qty' --><!-- /ko --></span>
                    <span class="value" data-bind="text: $parent.qty"></span>
                </div>
            </div>
            <!-- ko foreach: getRegion('after_details') -->
                <!-- ko template: getTemplate() --><!-- /ko -->
            <!-- /ko -->
        </div>
    

    SampWork/ConfigCheckoutDynamicPCB/view/frontend/layout/checkout_index_index.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="checkout.root">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="checkout" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="sidebar" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="summary" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="cart_items" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="details" xsi:type="array">
                                                                <item name="component"
                                                                      xsi:type="string">SampWork_ConfigCheckoutDynamicPCB/js/view/summary/item/details</item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </referenceBlock>
        </body>
    </page>
    

    【讨论】:

      【解决方案2】:

      您必须为此创建一个插件。我想将产品风味添加到订单摘要中。这就是我创建插件并实现我想要的方式。

      供应商 = Sejal

      您需要创建的文件:

      1. Registration.php:app\code\Sejal\Flavor\registration.php
      2. di.xml : app\code\Sejal\Flavor\etc\di.xml
      3. module.xml:app\code\Sejal\Flavor\etc\di.xml
      4. ConfigProviderPlugin.php : app\code\Sejal\Flavor\Plugin\ConfigProviderPlugin.php
      5. details.html : vendor\magento\module-checkout\view\frontend\web\template\summary\item\details.html 的副本

      你可以像这样在你的主题中覆盖这个文件

      app\design\frontend\Vendor\themename\Magento_Checkout\web\template\summary\item\details.html

      代码: 注册.php

      <?php
      
      \Magento\Framework\Component\ComponentRegistrar::register(
          \Magento\Framework\Component\ComponentRegistrar::MODULE,
          'Sejal_Flavor',
          __DIR__
      );
      

      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="AddAttPlug" type="Sejal\Flavor\Plugin\ConfigProviderPlugin" />
          </type>
      </config>
      

      模块.xml

      <?xml version="1.0"?>
      
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
          <module name="Sejal_Flavor" setup_version="1.0.0">
          </module>
      </config>
      

      ConfigProviderPlugin.php

      <?php
      
      namespace Sejal\Flavor\Plugin;
      
      class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel
      {
      
          public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
          {
      
              $items = $result['totalsData']['items'];
      
              $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
              for($i=0;$i<count($items);$i++){
      
                  $quoteId = $items[$i]['item_id'];
                  $quote = $objectManager->create('\Magento\Quote\Model\Quote\Item')->load($quoteId);
                  $productId = $quote->getProductId();
                  $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
                  $productFlavours = $product->getResource()->getAttribute('flavors')->getFrontend()->getValue($product);         
                  if($productFlavours == 'No' || $productFlavours == 'NA'){
                      $productFlavours = '';
                  }
                  $items[$i]['flavor'] = $productFlavours;
              }
              $result['totalsData']['items'] = $items;
              return $result;
          }
      
      }
      

      details.html

      在主题中复制 vendor\magento\module-checkout\view\frontend\web\template\summary\item\details.html 并添加

      <div class="product-item-flavor" data-bind="text: $parent.flavor"></div>
      

      下面

      <strong class="product-item-name" data-bind="text: $parent.name"></strong>
      

      就是这样! 希望对您有所帮助!

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多