我正在尝试将自定义产品属性添加到Magento 2结帐中“摘要”部分的项目列表中。模板文件位于,Magento_Checkout/web/template/summary/item/details.html
并希望在产品名称之前显示自定义属性的值。是否知道如何将此值添加到ko模板?貌似还有另外一个问题,这在这里,但从来没有回答。
我正在尝试将自定义产品属性添加到Magento 2结帐中“摘要”部分的项目列表中。模板文件位于,Magento_Checkout/web/template/summary/item/details.html
并希望在产品名称之前显示自定义属性的值。是否知道如何将此值添加到ko模板?貌似还有另外一个问题,这在这里,但从来没有回答。
Answers:
您将必须为此创建一个插件。我想在订单摘要中添加产品风味。这是我创建插件并实现我想要的方式。
供应商= Sejal
您需要创建的文件:
app\code\Sejal\Flavor\registration.php
app\code\Sejal\Flavor\etc\di.xml
app\code\Sejal\Flavor\etc\module.xml
app\code\Sejal\Flavor\Plugin\ConfigProviderPlugin.php
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>
而已!希望能帮助到你!
如果要在订单摘要中添加自定义属性,则需要覆盖:(布局)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());
}
});
}
);
那套,你会发现你的属性值谢谢:)
对我来说$ 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;
}
我需要显示可配置的简单产品名称。所以我使用如下代码。但是,当我在结帐顺序摘要中选择相同的可配置选项时,会显示相同的简单名称。那么,如何显示正确的简单产品名称?
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;
}