Magento 2中添加自定义折扣

在Magento 2中添加自定义折扣是您应该在Magento 2商店设置的最佳营销解决方案之一。显然,使用自定义折扣意味着您必须排除客户必须支付的价格购物车的一部分。要提供自定义折扣,本主题将在您直接在控制台中工作时明确指导您。以下是您需要遵循的四个关键步骤,并完成Magento 2中自定义折扣的配置。

概观

  • 第1步:在文件中输入总计 sale.xml
  • 第2步:设置折扣价值
  • 第3步:在布局文件中显示自定义折扣信息
  • 第4步:查看模型淘汰赛

第1步:在sale.xml文件中输入总计

您需要做的第一件事是在sale.xml文件中输入总计。请按照路径找到它:app/code/Mageplaza/HelloWorld/etc/sales.xml,然后使用脚本:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
 <section name="quote">
   <group name="totals">
     <item name="customer_discount" instance="Mageplaza\HelloWorld\Model\Total\Quote\Custom" sort_order="420"/>
   </group>
 </section>
</config>

第2步:设置折扣价值

确定并在模型中插入自定义折扣金额app/code/Mageplaza/HelloWorld/Model/Total/Quote/Custom.php

<?php
namespace Mageplaza\HelloWorld\Model\Total\Quote;
/**
* Class Custom
* @package Mageplaza\HelloWorld\Model\Total\Quote
*/
class Custom extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
   /**
    * @var \Magento\Framework\Pricing\PriceCurrencyInterface
    */
   protected $_priceCurrency;
   /**
    * Custom constructor.
    * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    */
   public function __construct(
       \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
   ){
       $this->_priceCurrency = $priceCurrency;
   }
   /**
    * @param \Magento\Quote\Model\Quote $quote
    * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
    * @param \Magento\Quote\Model\Quote\Address\Total $total
    * @return $this|bool
    */
   public function collect(
       \Magento\Quote\Model\Quote $quote,
       \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
       \Magento\Quote\Model\Quote\Address\Total $total
   )
   {
       parent::collect($quote, $shippingAssignment, $total);
           $baseDiscount = 10;
           $discount =  $this->_priceCurrency->convert($baseDiscount);
           $total->addTotalAmount('customdiscount', -$discount);
           $total->addBaseTotalAmount('customdiscount', -$baseDiscount);
           $total->setBaseGrandTotal($total->getBaseGrandTotal() - $baseDiscount);
           $quote->setCustomDiscount(-$discount);
       return $this;
   }
}

第3步:在布局文件中显示自定义折扣信息

我已完成上述两个步骤,Grand Total已更改为更新后的价格,但您的客户无法看到有关总折扣的任何信息。因此,您应该运行以下命令app/code/Mageplaza/HelloWorld/view/frontend/layout/checkout_cart_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.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="custom_discount" xsi:type="array">
                                   <item name="component"  xsi:type="string">Mageplaza_HelloWorld/js/view/checkout/summary/customdiscount</item>
                                   <item name="sortOrder" xsi:type="string">20</item>
                                   <item name="config" xsi:type="array">
                                       <item name="custom_discount" xsi:type="string" translate="true">Custom Discount</item>
                                   </item>
                               </item>
                           </item>
                       </item>
                   </item>
               </argument>
           </arguments>
       </referenceBlock>
   </body>
</page>

第4步:查看模型淘汰赛

要显示总数,首先app/code/Mageplaza/HelloWorld/view/frontend/web/js/view/checkout/summary/customdiscount.js使用代码段调用模型淘汰赛:

define(
   [
       'jquery',
       'Magento_Checkout/js/view/summary/abstract-total'
   ],
   function ($,Component) {
       "use strict";
       return Component.extend({
           defaults: {
               template: 'Mageplaza_HelloWorld/checkout/summary/customdiscount'
           },
           isDisplayedCustomdiscount : function(){
               return true;
           },
           getCustomDiscount : function(){
               return '$10';
           }
       });
   }
);

然后在模板淘汰赛中获得总折扣app/code/Mageplaza/HelloWorld/view/frontend/web/template/checkout/summary/customdiscount.html

<!-- ko if: isDisplayedCustomdiscount() -->
<tr class="totals customdiscount excl">
   <th class="mark" colspan="1" scope="row" data-bind="text: custom_discount"></th>
   <td class="amount">
       <span class="price" data-bind="text: getCustomDiscount(), attr: {'data-th': custom_discount}"></span>
   </td>
</tr>
<!-- /ko -->

完成所有步骤后,自定义折扣将立即应用于客户的评论购物车中,其中包含自定义折扣的名称和值。使用自定义价格并为其提供更好的价格是提高销售额的好主意。

赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论