0

I am trying to hide In-Store Pickup on checkout page if certain conditions met.

For example, I need to hide In-Store Pickup on checkout shipping methods section if customer belongs to specific customer group. For this, I found the following function to plugin:

Magento\InventoryInStorePickupShipping\Model\IsInStorePickupDeliveryAvailableForCart.php::execute()

enter image description here

But when I try to send false from this function, even then it still keep showing In-Store pickup method on checkout.

Please suggest which method I should override or plugin to hide In-store Pickup method on checkout.

2 Answers 2

0

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Address\RateCollectorInterface">
        <arguments>
            <argument name="rateCollectorsList" xsi:type="array">
                <item name="[vendor_Module]_shipping_method_restriction" xsi:type="string">Vendor\Module\Model\Quote\ShippingMethodRestriction</item>
            </argument>
        </arguments>
    </type>
</config>

Vendor\Module\Model\Quote\ShippingMethodRestriction.php

<?php
namespace Vendor\Module\Model\Quote;

use Magento\Quote\Model\Quote\Address\RateCollectorInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Quote\Model\Quote\Address\RateResult\Method;

class ShippingMethodRestriction implements RateCollectorInterface
{
    /**
     * @param RateRequest $request
     * @return Method[]
     */
    public function collectRates(RateRequest $request)
    {
        // Get the current customer group ID
        $customerGroupId = $request->getQuote()->getCustomer()->getGroupId();
        
        // Get the shipping methods available in the request
        $methods = $request->getShippingMethods();
        
        // Check if the current customer group is the one you want to hide the In-Store Pickup method for
        if ($customerGroupId == customer_group_id) { //add customer group id
            // Loop through the available shipping methods and remove the In-Store Pickup method
            foreach ($methods as $key => $method) {
                if ($method->getMethodCode() == 'in_store_pickup') {
                    unset($methods[$key]);
                }
            }
        }
        
        return $methods;
    }
}

Let me know if you facing any issues.

Thanks

1
  • Hi @Ronak, thanks for suggesting this solution. I tried but seems it is not triggering. CollectRates method is not called. Commented Jun 8, 2023 at 3:33
0

I plugin the Magento\Shipping\Model\Shipping::collectRates() method after getting the hint from the solution provided by @Ronak Rathod. Here is the solution that is working fine for me.

In Module di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Model\Shipping">
        <plugin name="in_store_pickup_shipping_method_hide" type="Vendor\InStorePickup\Plugin\AfterShippingCollectRates"/>
    </type>
</config>

And Code for my plugin is as follow.

<?php

namespace Vendor\InStorePickup\Plugin;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote\Address\RateRequest as RateRequest;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Magento\Quote\Model\Quote\Address\RateResult\Method;
use Magento\Shipping\Model\Rate\Result;
use Magento\Shipping\Model\Shipping;
use Psr\Log\LoggerInterface;

class AfterShippingCollectRates
{
    /**
     * @var LoggerInterface
     */
    protected LoggerInterface $_logger;

    /**
     * @var ErrorFactory
     */
    private $rateErrorFactory;

    /**
     * @var RateRequest|null
     */
    private $request = null;

    public function __construct(
        ErrorFactory $rateErrorFactory,
        LoggerInterface $logger
    ) {
        $this->rateErrorFactory = $rateErrorFactory;
        $this->_logger = $logger;
    }

    /**
     * @param Shipping $subject
     * @param RateRequest $request
     * @return void
     */
    public function beforeCollectRates(
        Shipping $subject,
        RateRequest $request
    ) {
        $this->request = $request;
    }

    /**
     * @param Shipping $subject
     * @param $result
     * @return Shipping
     * @throws LocalizedException
     * @throws NoSuchEntityException
     */
    public function afterCollectRates(Shipping $subject, $result)
    {
        $result = $subject->getResult();
        $rates = $result->getAllRates();
        $result->reset();
        foreach ($rates as $rate) {
            $restrict = false;
            if ($rate->getCarrier() == 'instore') {
                $checkIfStorePickupNeedtoBeHidden = $this->checkIfStorePickupNeedtoBeHidden();
                if ($checkIfStorePickupNeedtoBeHidden) {
                    $restrict = true;
                    $this->setError($result, $rate);
                }
            }
            if (!$restrict) {
                $result->append($rate);
            }
        }
        return $subject;
    }

    /**
     * set error message
     * @param Result $result
     * @param Method $rate
     *
     * @return bool
     */
    private function setError($result, $rate)
    {
        $errorMessage = __('Sorry, Pick in Store method is restricted for this Orders.');
        if ($rate !== null && $errorMessage) {
            $error = $this->rateErrorFactory->create();
            $error->setCarrier($rate->getCarrier());
            $error->setCarrierTitle($rate->getCarrierTitle().' ('.$rate->getMethodTitle().')');
            $error->setErrorMessage($errorMessage);
            $result->append($error);
        }
    }
}

Write down a custom function

checkIfStorePickupNeedtoBeHidden

in the same file (or Helper) and put the logic for hiding shipping method in it.

Reference: https://webkul.com/blog/restrict-custom-shipping-methods-magento2/

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.