Magento 2 - 在结帐摘要框中添加额外的产品属性

时间:2017-01-11 17:07:01

标签: knockout.js checkout magento2

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

我必须覆盖: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>

所以你看到实际的文字&#34;作者就到这儿了!&#34;我必须打电话给$ parent.authors。

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

Checkout summary box - image

2 个答案:

答案 0 :(得分:2)

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

我引用了此链接https://www.magevision.com/blog/post/get-a-product-attribute-in-checkout-summary-magento-2/

SampWork / ConfigCheckoutDynamicPCB / registration.php

nullptr

SampWork / ConfigCheckoutDynamicPCB / etc / module.xml

<?php

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

SampWork / ConfigCheckoutDynamicPCB / etc / catalog_attributes.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 / di.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 / Plugin / Checkout / Model / DefaultConfigProvider.php

<?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 / SampWork / ConfigCheckoutDynamicPCB / view / frontend / web / js / view / summary / item / details.js

<?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 / ConfigCheckoutDynamicPCB / view / frontend / web / template / summary / item / details.html

/**
 * 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 / layout / checkout_index_index.xml

<!-- 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>

答案 1 :(得分:0)

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

供应商= 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:供应商\ magento \ module-checkout \ view \ frontend \ web \ template \ summary \ item \ details.html
  6. 的副本

    您可以像这样

    覆盖主题中的此文件

    应用\设计\前端\卖方\ THEMENAME \ Magento_Checkout \网络\模板\摘要\项目\ details.html

    代码: 为registration.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>
    

    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="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>
    

    那就是它! 希望它有所帮助!