I want to apply free shipping for subtotal more than $50 and it’s working fine. but if customer applied any discount coupon then free shipping should not apply because grand total is less than $50. For now, Free shipping still applying because Subtotal is still greater than $50.
Can anyone help me how to remove free shipping if Grand total less than $50 after applying discount coupon?
Here is my code:
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
// exclude Virtual products price from Package value if pre-configured
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;
foreach ($request->getAllItems() as $item) {
if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
continue;
}
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->getConditionMRName()) {
$conditionName = $this->getConfigData('condition_name');
$request->setConditionMRName($conditionName ? $conditionName : $this->defaultConditionName);
}
// Package weight and qty free shipping
$oldWeight = $request->getPackageWeight();
$oldQty = $request->getPackageQty();
$request->setPackageWeight($request->getFreeMethodWeight());
$request->setPackageQty($oldQty - $freeQty);
/** @var MagentoShippingModelRateResult $result */
$result = $this->rateResultFactory->create();
$zipRange = $this->getConfigData('zip_range');
$rateArray = $this->getRate($request, $zipRange);
$request->setPackageWeight($oldWeight);
$request->setPackageQty($oldQty);
$foundRates = false;
foreach ($rateArray as $rate) {
if (!empty($rate) && $rate['price'] >= 0) {
/** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
$method = $this->resultMethodFactory->create();
$method->setCarrier('matrixrate');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('matrixrate_' . $rate['pk']);
$method->setMethodTitle(__($rate['shipping_method']));
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);
$foundRates = true; // have found some valid rates
}
}
if (!$foundRates) {
/** @var MagentoQuoteModelQuoteAddressRateResultError $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 MagentoQuoteModelQuoteAddressRateRequest $request
* @param bool $zipRange
* @return array|bool
*/
public function getRate(MagentoQuoteModelQuoteAddressRateRequest $request, $zipRange)
{
return $this->matrixrateFactory->create()->getRate($request, $zipRange);
}
/**
* @param string $type
* @param string $code
* @return array
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function getCode($type, $code = '')
{
$codes = [
'condition_name' => [
'package_weight' => __('Weight vs. Destination'),
'package_value' => __('Order Subtotal vs. Destination'),
'package_qty' => __('# of Items vs. Destination'),
],
'condition_name_short' => [
'package_weight' => __('Weight'),
'package_value' => __('Order Subtotal'),
'package_qty' => __('# of Items'),
],
];
if (!isset($codes[$type])) {
throw new LocalizedException(__('Please correct Matrix Rate code type: %1.', $type));
}
if ('' === $code) {
return $codes[$type];
}
if (!isset($codes[$type][$code])) {
throw new LocalizedException(__('Please correct Matrix Rate code for type %1: %2.', $type, $code));
}
return $codes[$type][$code];
}
/**
* Get allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
return ['matrixrate' => $this->getConfigData('name')];
} ```