1

I have code for add extra fee in order in magento 2, now my requirement is the extra fee can only show will be not add in order total

enter image description here

the extra free can also add in order total but I can not add that

I need order total 155 not 160

my code is that

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);
        if (!count($shippingAssignment->getItems())) {
            return $this;
        }

        $enabled = $this->helperData->isModuleEnabled();
        $minimumOrderAmount = $this->helperData->getMinimumOrderAmount();
        $subtotal = $total->getTotalAmount('subtotal');
        if ($enabled && $minimumOrderAmount <= $subtotal) {
            $fee = $quote->getFee();
            $total->setTotalAmount('fee', $subtotal);
            $total->setBaseTotalAmount('fee', $subtotal);
            $total->setFee($fee);
            $quote->setFee($fee);
            $total->setGrandTotal($total->getGrandTotal());
            $total->setBaseGrandTotal($total->getBaseGrandTotal());
        }
        return $this;
    }
1
  • can you please share the code for an extra fee. I have a requirement to add extra fee into order totals and summary as well. Commented May 25, 2021 at 6:01

2 Answers 2

2

can you add following updated code ?

  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();
    $balance = $fee - $exist_amount;

    $total->setTotalAmount('fee', $balance);
    $total->setBaseTotalAmount('fee', $balance);

    $total->setFee($balance);
    $total->setBaseFee($balance);

    $total->setGrandTotal($total->getGrandTotal());
    $total->setBaseGrandTotal($total->getBaseGrandTotal());


    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);
}
1
  • hello i have add new question can you please review that and please help me
    – Jay Parmar
    Commented Jun 30, 2020 at 12:27
1

you can change following function in your code like this

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);
            if (!count($shippingAssignment->getItems())) {
                return $this;
            }

            $enabled = $this->helperData->isModuleEnabled();
            $minimumOrderAmount = $this->helperData->getMinimumOrderAmount();
            $subtotal = $total->getTotalAmount('subtotal');
            if ($enabled && $minimumOrderAmount <= $subtotal) {
                $fee = $quote->getFee();
                $total->setFee($fee);
                $quote->setFee($fee);
            }
            return $this;
} 
12
  • in witch file can you guide me
    – Jay Parmar
    Commented Jun 11, 2020 at 10:03
  • app\code\Webkul\Test\Model\Total\Customfee.php Commented Jun 11, 2020 at 10:21
  • $enabled = $this->helperData->isModuleEnabled(); $minimumOrderAmount = $this->helperData->getMinimumOrderAmount(); $subtotal = $total->getTotalAmount('subtotal'); if ($enabled && $minimumOrderAmount <= $subtotal) { $fee = $quote->getFee(); $total->setTotalAmount('fee', $subtotal); $total->setBaseTotalAmount('fee', $subtotal); $total->setFee($fee); $quote->setFee($fee); $total->setGrandTotal($total->getGrandTotal()); $total->setBaseGrandTotal($total->getBaseGrandTotal
    – Jay Parmar
    Commented Jun 11, 2020 at 10:27
  • 1
    check I have I edited my answer Commented Jun 11, 2020 at 11:11
  • 1
    Please check my new answer. Commented Jun 17, 2020 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.