Hi I have create custom shipping method. I have enabled it from back end. Now I want show this only if product custom attribute match some conditions.
2 Answers
It's your custom shipping method which have collectRates method in somewhere in model folder ex. for Flatrate shipping method collectRates method is located at Magento\OfflineShipping\Model\Carrier\Flatrate
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
if($customeProductConditionFalse)
{
return false;
}
........
}
In this method you can check your condition for product custom attribute match or not and
return false;
to disable shipping method
-
i can add product attribute condition but what is the code for disable shipping method. Commented Mar 27, 2018 at 12:30
-
-
Is this working for you? I do not want to disable shipping method. But I want to hide the shipping method on some product custom option condition. Commented Jul 7, 2018 at 9:17
-
-
Create 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\Api\ShipmentEstimationInterface">
<plugin name="hide_show_shipping_methods" type="Vendor\Module\Plugin\Quote\Api\ShipmentEstimationPlugin"/>
</type>
</config>
Create Vendor\Module\Plugin\Quote\Api\ShipmentEstimationPlugin.php
.
<?php
namespace Vendor\Module\Plugin\Quote\Api;
use Magento\Quote\Api\ShipmentEstimationInterface;
class ShipmentEstimationPlugin
{
public function __construct(
\Magento\Customer\Model\Session $customerSession
) {
$this->customerSession = $customerSession;
}
public function aroundEstimateByExtendedAddress(
ShipmentEstimationInterface $subject,
\Closure $proceed,
$cartId,
\Magento\Quote\Api\Data\AddressInterface $address
) {
$shippingMethods = $proceed($cartId, $address);
if ($this->customerSession->isLoggedIn()) {
foreach ($shippingMethods as $key => $shippingMethod) {
//Replace 'freeshipping' with your shipping method which you want to hide
if ($shippingMethod->getMethodCode() == 'freeshipping') {
unset($shippingMethods[$key]);
}
}
return $shippingMethods;
}
}
}
It will work for you.