I have override magento 2 tablerate module class. Below are the override file Custom/Shipping/Model/Carrier/Tablerate.php
namespace Custom\Shipping\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
class Tablerate extends \Magento\OfflineShipping\Model\Carrier\Tablerate
{
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
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;
$customItemPrice = 0;
$cartHasItems = false;
$total=[];
// exclude Virtual products price from Package value if pre-configured
$i=0;
foreach ($request->getAllItems() as $item) {
if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
continue;
}
$itemPrice = 0;
$myItemPrice = 0;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
$total = $this->compareWeight($product, $total, $i, $item);
$i++;
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
$weight=$total;
$oldWeight = $request->getPackageWeight();
$oldQty = $request->getPackageQty();
$rate_price=0;
list($result, $rate) = $this->getFinalRateShip($request, $total, $oldQty, $freeQty, $rate_price);
$oldWeight = $request->getPackageWeight();
$oldQty = $request->getPackageQty();
$request->setPackageWeight($request->getFreeMethodWeight());
$request->setPackageQty($oldQty - $freeQty);
/** @var \Magento\Shipping\Model\Rate\Result $result */
$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;
}
/**
* @param RateRequest $request
* @param $total
* @param $oldQty
* @param $freeQty
* @param $rate_price
* @return array
*/
public function getFinalRateShip(RateRequest $request, $total, $oldQty, $freeQty, $rate_price)
{
foreach ($total_dim as $value) {
$request->setPackageWeight($value['w']);
$request->setPackageQty($oldQty - $freeQty);
$result = $this->_rateResultFactory->create();
$rate = $this->getRate($request);
$rate_price += $rate['price'] * $value['q'];
}
return [$result, $rate];
}
/**
* @param $product
* @param $total
* @param $i
* @param $item
* @return mixed
*/
public function compareWeight($product, $total, $i, $item)
{
$width = $product->getDimensionalWidth();
$height = $product->getDimensionalHeight();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$WeightFactor= $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('customshipping/general/display_text');
$ShippingStatus= $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('customshipping/general/enable');
$dim = round(($width * $height) / $WeightFactor);
$total[$i]['w'] = $dim;
$total[$i]['q'] = $item->getQty();
return $total;
}
}
I want to get product details by id and system configuration without using object manager.
Currently I am using
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
and
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$WeightFactor= $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('customshipping/general/display_text');
$ShippingStatus= $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('customshipping/general/enable');
Which is wrong. When I am using any product and system configuration then it always showing me error regarding construction.
When I used default construction from Table rate class then it always showing me argument 4 error.
Best Answer
NEW PROCEDURE USING PLUGIN
I strongly suggest to use plugin instead of using rewrite class.
Plugin in the best way to modify a shipping Carrier rated.
We have to create the plugin on
collectRates(RateRequest $request)
method and use around method on this method.Plugin class
OLD PROCEDURE
In magento2, if you want to override a class then suggestion is that you should copy all code mean all functions ,variables from your preference class to
new class, then you need to modify in new class
.like