I'm using Magento 2.1. Currently, I'm having two shipping methods. One is UPS and another is Flatrate. If my product's attribute returns true, I want to hide Flatrate shipping method from frontend only, for Logged in/Guest User and if the product's attribute returns false I have to hide the UPS shipping method. How can I write the code for the same in my custom module.
Magento 2 – Hiding Shipping Method for Logged In/Guest User Based on Condition
checkoutsessionmagento-2.1magento2shipping-methods
Related Solutions
Following blog could help you:
Need to override Model
magento\app\code\Custom\Module\etc\di.xml
<preference for="Magento\Dhl\Model\Carrier" type="Custom\Module\Model\Carrier" />
magento\app\code\Custom\Module\Model\Carrier.php
namespace Custom\Module\Model;
class Carrier extends \Magento\Dhl\Model\Carrier {
/**
* Prepare items to pieces
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function _getAllItems() {
$allItems = $this->_request->getAllItems();
$fullItems = [];
foreach ($allItems as $item) {
if ($item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE && $item->getProduct()->getShipmentType()) {
continue;
}
$qty = $item->getQty();
$changeQty = true;
$checkWeight = true;
$decimalItems = [];
if ($item->getParentItem()) {
if (!$item->getParentItem()->getProduct()->getShipmentType()) {
continue;
}
if ($item->getIsQtyDecimal()) {
$qty = $item->getParentItem()->getQty();
} else {
$qty = $item->getParentItem()->getQty() * $item->getQty();
}
}
$itemWeight = $item->getWeight(); // Original Weight
// START CUSTOM CODE
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProduct()->getId());
$itemWeight = $_product->getData('my_weight_attribute');
// END CUSTOM CODE
if ($item->getIsQtyDecimal() && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
$productId = $item->getProduct()->getId();
$stockItemDo = $this->stockRegistry->getStockItem($productId, $item->getStore()->getWebsiteId());
$isDecimalDivided = $stockItemDo->getIsDecimalDivided();
if ($isDecimalDivided) {
if ($stockItemDo->getEnableQtyIncrements() && $stockItemDo->getQtyIncrements()
) {
$itemWeight = $itemWeight * $stockItemDo->getQtyIncrements();
//$qty = round($item->getWeight() / $itemWeight * $qty);
$qty = round($_product->getData('my_weight_attribute') / $itemWeight * $qty);
$changeQty = false;
} else {
$itemWeight = $this->_getWeight($itemWeight * $item->getQty());
$maxWeight = $this->_getWeight($this->_maxWeight, true);
if ($itemWeight > $maxWeight) {
$qtyItem = floor($itemWeight / $maxWeight);
$decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem];
$weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
if ($weightItem) {
$decimalItems[] = ['weight' => $weightItem, 'qty' => 1];
}
$checkWeight = false;
}
}
} else {
$itemWeight = $itemWeight * $item->getQty();
}
}
if ($checkWeight && $this->_getWeight($itemWeight) > $this->_getWeight($this->_maxWeight, true)) {
return [];
}
if ($changeQty && !$item->getParentItem() && $item->getIsQtyDecimal() && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
) {
$qty = 1;
}
if (!empty($decimalItems)) {
foreach ($decimalItems as $decimalItem) {
$fullItems = array_merge(
$fullItems, array_fill(0, $decimalItem['qty'] * $qty, $decimalItem['weight'])
);
}
} else {
$fullItems = array_merge($fullItems, array_fill(0, $qty, $this->_getWeight($itemWeight)));
}
}
sort($fullItems);
return $fullItems;
}
}
Don't wanna use objectmanager
any alternatives?
Best Answer
Let's say that the attribute code is called
attribute_code
so the getter name isgetAttributeCode()
.You need to create an
arround
plugin on the flatrate carrier model and UPS carrier model where you can check your conditions and return or not the shipping rate result.You need to create a module and add this in the
di.xml
fileThen create the file
[Vendor]/[Module]/Plugin/Flatrate.php
and this plugin class that handles UPS shipping method
[Vendor]/[Module]/Plugin/Ups.php
But keep in mind that I didn't test the code so watch out for typos.