Magento 2: How to change Weight field for DHL Shipping Method [SOLVED]
-
04-10-2020 - |
Pregunta
For Product apart from Magento Field Weight, we have another Attribute Called
Total Weight.
Every product has this attribute. So when user will select DHL Shipping Method, instead of Product's weight field I need to use my Total Weight attribute.
How to achieve this? Need to override from
magento\vendor\magento\module-dhl\Model\Carrier.php
&_getAllItems()
function?
Solución
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?
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange