Magento2 添加动态自定义费用magento 2
我正在为magento 2构建一个自定义费用模块。我在结帐页面中添加了一个复选框,如果用户勾选了该复选框,我将向订单中添加自定义费用。在本教程之后,我成功地将自定义费用添加到订单中: 然而,我的自定义费用是动态的,因为它是从外部API获取的。让我们来看看: app\code\Sugarcode\Test\Model\Total\Fee.phpMagento2 添加动态自定义费用magento 2,magento2,fee,Magento2,Fee,我正在为magento 2构建一个自定义费用模块。我在结帐页面中添加了一个复选框,如果用户勾选了该复选框,我将向订单中添加自定义费用。在本教程之后,我成功地将自定义费用添加到订单中: 然而,我的自定义费用是动态的,因为它是从外部API获取的。让我们来看看: app\code\Sugarcode\Test\Model\Total\Fee.php <?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYIN
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Model\Total;
class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* Collect grand total address amount
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
*/
protected $quoteValidator = null;
public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
{
$this->quoteValidator = $quoteValidator;
}
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);
$exist_amount = 0; //$quote->getFee();
// $fee = 100; //Excellence_Fee_Model_Fee::getFee();
// TAKE A LOOK AT THIS:
$fee = getFeeFromExternalAPI()
// I successfully get the FEE but how to update the value of checkbox in the UI???
$balance = $fee - $exist_amount;
$total->setTotalAmount('fee', $balance);
$total->setBaseTotalAmount('fee', $balance);
$total->setFee($balance);
$total->setBaseFee($balance);
$total->setGrandTotal($total->getGrandTotal() + $balance);
$total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);
return $this;
}
protected function clearValues(Address\Total $total)
{
$total->setTotalAmount('subtotal', 0);
$total->setBaseTotalAmount('subtotal', 0);
$total->setTotalAmount('tax', 0);
$total->setBaseTotalAmount('tax', 0);
$total->setTotalAmount('discount_tax_compensation', 0);
$total->setBaseTotalAmount('discount_tax_compensation', 0);
$total->setTotalAmount('shipping_discount_tax_compensation', 0);
$total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
$total->setSubtotalInclTax(0);
$total->setBaseSubtotalInclTax(0);
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array|null
*/
/**
* Assign subtotal amount and label to address object
*
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
return [
'code' => 'fee',
'title' => 'Fee',
'value' => 100
];
}
/**
* Get Subtotal label
*
* @return \Magento\Framework\Phrase
*/
public function getLabel()
{
return __('Fee');
}