将自定义产品属性添加到结帐摘要Magento 2


14

我正在尝试将自定义产品属性添加到Magento 2结帐中“摘要”部分的项目列表中。模板文件位于,Magento_Checkout/web/template/summary/item/details.html并希望在产品名称之前显示自定义属性的值。是否知道如何将此值添加到ko模板?貌似还有另外一个问题,这在这里,但从来没有回答。



1
@Arjun这是不同的。本文引用的文章确实显示购物车页面未结帐。购物车是一个简单的phtml模板。结帐页面是ko页面,并且从迷你购物车以外的其他地方获取其来源。不确定为什么迷你购物车,购物车和结帐中显示的所有购物车商品都以不同的方式构建。但是实际的结帐摘要是我需要查看如何添加自定义属性的地方。
sudopratt '16

@sudopratt,您是否知道这一点,如何在Magento 2的结帐中的摘要部分的项目列表中添加自定义产品属性?
Sarfaraj Sipai

Answers:


16

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

供应商= 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\module.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

代码: 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

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

而已!希望能帮助到你!


我已经在Aheadworks的onestepcheck扩展程序中尝试过了,但是它不起作用。我能怎么做 ?
Manish Maheshwari


@Sejal Shah如何在此处添加条件
sumeet bajaj

1
这对运输步骤很有用,但是在结算步骤。.product-item-flavor保持为空
jonasG

Sejal这里回答我的问题:magento.stackexchange.com/questions/178398/...
jonasG

3

如果要在订单摘要中添加自定义属性,则需要覆盖:(布局)1)checkout_cart_index:

<referenceBlock name="checkout.cart.totals">
        <arguments>
            <argument name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="block-totals" xsi:type="array">
                        <item name="children" xsi:type="array">
                            <item name="processingfee" xsi:type="array">
                                <item name="component"  xsi:type="string">Dedicated_Processingfee/js/view/checkout/cart/totals/processingfee</item>
                                <item name="sortOrder" xsi:type="string">20</item>
                                <item name="config" xsi:type="array">
                                    <item name="template" xsi:type="string">Dedicated_Processingfee/checkout/cart/totals/processingfee</item>
                                    <item name="title" xsi:type="string" translate="true">Processing Fee</item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>

2)checkout_index_index:

<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="totals" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="processingfee" xsi:type="array">
                                                        <item name="component"  xsi:type="string">Dedicated_Processingfee/js/view/checkout/cart/totals/processingfee</item>
                                                        <item name="sortOrder" xsi:type="string">20</item>
                                                        <item name="config" xsi:type="array">
                                                            <item name="template" xsi:type="string">Dedicated_Processingfee/checkout/cart/totals/processingfee</item>
                                                            <item name="title" xsi:type="string" translate="true">Processing Fee</item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                            <item name="cart_items" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="details" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="subtotal" xsi:type="array">
                                                                <item name="component" xsi:type="string">Magento_Tax/js/view/checkout/summary/item/details/subtotal</item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>

3)sales_order_view:

<referenceContainer name="order_totals">
        <block class="Dedicated\Processingfee\Block\Sales\Order\ProcessingFee" name="processingfee"/>
    </referenceContainer>

然后添加自定义js以在模块中获取您的自定义属性值,如下所示:/view/frontend/web/js/view/checkout/cart/totals/processingfee.js:

define(
[
    'Dedicated_Processingfee/js/view/checkout/summary/processingfee'
],
function (Component) {
    'use strict';

    return Component.extend({

        /**
        * @override
        */
        isDisplayed: function () {
            return true;
        }
    });
}

);

在/view/frontend/web/js/view/checkout/summary/processingfee.js中添加另一个js以计算总帐单金额的值

define(
[
    'Magento_Checkout/js/view/summary/abstract-total',
    'Magento_Checkout/js/model/quote',
    'Magento_Catalog/js/price-utils',
    'Magento_Checkout/js/model/totals'
],
function (Component, quote, priceUtils, totals) {
    "use strict";
    return Component.extend({
        defaults: {
            isFullTaxSummaryDisplayed: window.checkoutConfig.isFullTaxSummaryDisplayed || false,
            template: 'Dedicated_Processingfee/checkout/summary/processingfee'
        },
        totals: quote.getTotals(),
        isTaxDisplayedInGrandTotal: window.checkoutConfig.includeTaxInGrandTotal || false,
        isDisplayed: function() {
            return this.isFullMode();
        },
        getValue: function() {
            var price = 0;
            if (this.totals()) {
                price = totals.getSegment('processingfee').value;
            }
            return this.getFormattedPrice(price);
        },
        getBaseValue: function() {
            var price = 0;
            if (this.totals()) {
                price = this.totals().base_fee;
            }
            return priceUtils.formatPrice(price, quote.getBasePriceFormat());
        }
    });
}

);

那套,你会发现你的属性值谢谢:)

在此处输入图片说明


1
我不认为@sudopratt要在总计列中添加一行,而不是在产品名称下方的产品属性(如简短说明)中添加一行。
Sunil Verma

@ Sunil Verma您对此有解决方案吗?我需要做的完全一样,但无法获得任何参考
Rohit Goel

是的,其显示自定义属性,但是转到下一步进行#payment时,自定义属性会消失。为什么?
哈菲兹·乌默尔

1

对我来说$ result ['totalsData'] ['items']为空。我改用以下实现:

public function afterGetConfig(
    \Magento\Checkout\Model\DefaultConfigProvider $subject,
    array $result

) {
    foreach ($result['quoteItemData'] as $index => $itemData) {
        $product = $this->productRepository->getById($itemData['product_id']);
        $result['quoteItemData'][$index]['flavor'] = $product->getFlavor();
    }
    return $result;
}

0

我需要显示可配置的简单产品名称。所以我使用如下代码。但是,当我在结帐顺序摘要中选择相同的可配置选项时,会显示相同的简单名称。那么,如何显示正确的简单产品名称?

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);
        $productTypeInstance = $product->getTypeInstance();
        $usedProducts = $productTypeInstance->getUsedProducts($product);

        foreach ($usedProducts  as $child) {
            $childName = $child->getName(); //Child Product Name
        }           

        $items[$i]['childname'] = $childName;
    }
    $result['totalsData']['items'] = $items;
    return $result;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.