I created my own shipping method but I want the price to be set as 10% of the subTotal.
I think I should use something of the session or the quote cart inside the collectRates function of Magento but I can't seem to figure it out.
Can you help me out?
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier('percentagetotaal');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('percentagetotaal');
$method->setMethodTitle($this->getConfigData('name'));
$percentage = 10;
// I want to get the session and get the cart subtotal and calculate 10% of the $subTotal.
// How to get $subTotal;
$price = ($subTotal * $percentage) / 100; //calculate 10% of the product cost
$method->setPrice($price);
$method->setCost($price);
$result->append($method);
return $result;
}
So I want to know how to get $subTotal inside the collectRates function.