Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user

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

deleted 2 characters in body
Source Link

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

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

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

Source Link

Magneto2 : Calculate shipping charge based on cart value in Store Pickup method

I am working on magento 2.1.3. I am 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;
}