I was working on shipping method to remove some method based on product attribute. I've used preference for shipping model class \Magento\Shipping\Model\Shipping
this is code of that class.
public function collectCarrierRates($carrierCode, $request)
{
$carrier = $this->_carrierFactory->createIfActive($carrierCode, $request->getStoreId());
if (!$carrier) {
return $this;
}
//unset other method if cart has speific product attribute
if($carrierCode=="flatrate"):
return $this;
endif;
$carrier->setActiveFlag($this->_availabilityConfigField);
$result = $carrier->checkAvailableShipCountries($request);
if (false !== $result && !$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
$result = $carrier->proccessAdditionalValidation($request);
}
/*
* Result will be false if the admin set not to show the shipping module
* if the delivery country is not within specific countries
*/
if (false !== $result) {
if (!$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
if ($carrier->getConfigData('shipment_requesttype')) {
$packages = $this->composePackagesForCarrier($carrier, $request);
if (!empty($packages)) {
$sumResults = [];
foreach ($packages as $weight => $packageCount) {
$request->setPackageWeight($weight);
$result = $carrier->collectRates($request);
if (!$result) {
return $this;
} else {
$result->updateRatePrice($packageCount);
}
$sumResults[] = $result;
}
if (!empty($sumResults) && count($sumResults) > 1) {
$result = [];
foreach ($sumResults as $res) {
if (empty($result)) {
$result = $res;
continue;
}
foreach ($res->getAllRates() as $method) {
foreach ($result->getAllRates() as $resultMethod) {
if ($method->getMethod() == $resultMethod->getMethod()) {
$resultMethod->setPrice($method->getPrice() + $resultMethod->getPrice());
continue;
}
}
}
}
}
} else {
$result = $carrier->collectRates($request);
}
} else {
$result = $carrier->collectRates($request);
}
if (!$result) {
return $this;
}
}
if ($carrier->getConfigData('showmethod') == 0 && $result->getError()) {
return $this;
}
// sort rates by price
if (method_exists($result, 'sortRatesByPrice') && is_callable([$result, 'sortRatesByPrice'])) {
$result->sortRatesByPrice();
}
$this->getResult()->append($result);
}
return $this;
}
I'm trying to get product info here, but I am not able to get it. Anybody has idea how to get it?