Magento – Magento2 : Calculate shipping charge based on cart value in Store Pickup method

magento-2.1magento2

I am working on magento 2.1.3 and using storepickup plugin for shipping methods.

My shipping charge for store pickup is 5% of the cart value.

Also there is 16% VAT for all products and shipping charge and shipping charge and cart values are inclusive of VAT.

5% of shipping charge is working fine with normal situation.

But calculation is wrong when i am applying discount codes.

Then also its showing correct values on cart and checkout. But values are wrong on order email and backend.

Here is my code

File : app/code/Vendor/Storepickup/Model/Carrier/Custom:

public function collectRates(RateRequest $request) {
    if (!$this->getConfigFlag('active')) {
        return false;
    }


    $freeBoxes = 0;
    if ($request->getAllItems()) {
        foreach ($request->getAllItems() as $item) {


            if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                continue;
            }

            if ($item->getHasChildren() && $item->isShipSeparately()) {
                foreach ($item->getChildren() as $child) {
                    if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                        $freeBoxes += $item->getQty() * $child->getQty();
                    }
                }
            } elseif ($item->getFreeShipping()) {
                $freeBoxes += $item->getQty();
            }
        }
    }





    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');

    $subTotal = $cart->getQuote()->getSubtotal();
    $grandTotal = $cart->getQuote()->getGrandTotal();


$this->setFreeBoxes($freeBoxes);

    /** @var Result $result */
    $result = $this->_rateResultFactory->create();




    if ($this->getConfigData('type') == 'O') {
        // per order
        $shippingPrice = $this->getConfigData('price');
    } elseif ($this->getConfigData('type') == 'I') {
        // per item
        $shippingPrice = $request->getPackageQty() * $this->getConfigData(
                        'price'
                ) - $this->getFreeBoxes() * $this->getConfigData(
                        'price'
        );
    } else {
        $shippingPrice = false;
    }



    //$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);


    if ($shippingPrice !== false) {
        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->_rateMethodFactory->create();
        $method->setCarrier('storepickup');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('storepickup');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
            $shippingPrice = '0.00';
        }


        $shippingPrice = $grandTotal * (5/100);



        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);
    }



    return $result;
}

Best Answer

I did something similar. I did the following, but there are probably different ways to do this.

  • Make sure you have the proper blocks to display/save these variables to be visible in the emails/order views from you layout
  • Save the data to the orders table in the data base. See code below on how I did this.
  • Use events.xml to run the code below during the event 'sales_model_service_quote_submit_before'

    <?php
    namespace vendor\module\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class ConvertQuoteToOrder implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $order = $observer->getData('order');
            $quote = $observer->getData('quote');
            $order->setData('extrafee', $quote->getExtraFee());
        }
    }
    

Also, as a side note. I used checkoutSession to help access the variables since my fee variables are dynamic per the users checkbox input.

I hope this helps you. There are a few different examples on Magento Exchange to calculate extra fees in checkout. I would try looking at those to help you out as well. This one below was very helpful to me. He wrote out the entire module. Add custom fee Magento 2

Related Topic