浏览文章

文章信息

Magento2 【综合】自定义折扣信息|custoim discount amount 13105

1、创建销售sales.xml

你需要做的第一件事是在sales.xml文件。请按照以下路径创建:app/code/Aiweline/HelloWorld/etc/sales.xml,然后使用脚本:

<?xml version="1.0"?>
<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="Aiweline\HelloWorld\Model\Total\Quote\Custom" sort_order="420"/>
   </group>
 </section>
</config>

2、PHP设置折扣

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

<?php
namespace Aiweline\HelloWorld\Model\Total\Quote;
/**
* Class Custom
* @package Aiweline\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、在布局文件中显示自定义折扣信息

我已经完成了以上两个步骤,总金额已经随着更新的价格发生了变化,但您的客户看不到任何有关总折扣的信息。因此,应该布局文件中添加总计

app/code/Aiweline/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">Aiweline_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、创建视图模型knockout

要显示总计,首先调用model knockout :app/code/Aiweline/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: 'Aiweline_HelloWorld/checkout/summary/customdiscount'
           },
           isDisplayedCustomdiscount : function(){
               return true;
           },
           getCustomDiscount : function(){
               return '$10';
           }
       });
   }
);

然后在knockout模板文件中显示折扣

app/code/Aiweline/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 -->

完成所有步骤后,自定义折扣将立即应用到客户的购物车中,并带有自定义折扣的名称和值。使用定制价格并给他们更好的价格是促进你的销售策略。


原创