0

I got 2 shipping methods: Tablerates, Free Shipping If certain conditions apply I want to remove the tablerates shipping method in checkout.

How can I do that?

Thanks! :-)

Edit:

I found the code I have to change. How can I get the customer here?

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

    // exclude Virtual products price from Package value if pre-configured
    if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) {
        foreach ($request->getAllItems() as $item) {
            if ($item->getParentItem()) {
                continue;
            }
            if ($item->getHasChildren() && $item->isShipSeparately()) {
                foreach ($item->getChildren() as $child) {
                    if ($child->getProduct()->isVirtual()) {
                        $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
                    }
                }
            } elseif ($item->getProduct()->isVirtual()) {
                $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
            }
        }
    }

    // Free shipping by qty
    $freeQty = 0;
    if ($request->getAllItems()) {
        $freePackageValue = 0;
        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()) {
                        $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
                        $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
                    }
                }
            } elseif ($item->getFreeShipping()) {
                $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
                $freeQty += $item->getQty() - $freeShipping;
                $freePackageValue += $item->getBaseRowTotal();
            }
        }
        $oldValue = $request->getPackageValue();
        $request->setPackageValue($oldValue - $freePackageValue);
    }

    if (!$request->getConditionName()) {
        $conditionName = $this->getConfigData('condition_name');
        $request->setConditionName($conditionName ? $conditionName : $this->_defaultConditionName);
    }

    // Package weight and qty free shipping
    $oldWeight = $request->getPackageWeight();
    $oldQty = $request->getPackageQty();

    $request->setPackageWeight($request->getFreeMethodWeight());
    $request->setPackageQty($oldQty - $freeQty);

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->_rateResultFactory->create();
    $rate = $this->getRate($request);

    $request->setPackageWeight($oldWeight);
    $request->setPackageQty($oldQty);

    if (!empty($rate) && $rate['price'] >= 0) {
        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->_resultMethodFactory->create();

        $method->setCarrier('tablerate');
        $method->setCarrierTitle($this->getConfigData('title'));

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

        if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
            $shippingPrice = 0;
        } else {
            $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
        }

        $method->setPrice($shippingPrice);
        $method->setCost($rate['cost']);

        $result->append($method);
    } else {
        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Error $error */
        $error = $this->_rateErrorFactory->create(
            [
                'data' => [
                    'carrier' => $this->_code,
                    'carrier_title' => $this->getConfigData('title'),
                    'error_message' => $this->getConfigData('specificerrmsg'),
                ],
            ]
        );
        $result->append($error);
    }

    return $result; 

}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.