In Magento 2.3 I want to append a shipping method to my collection of methods but it's not working. This is what I'm doing:
- I'm selecting the cheapest rate from the collection of rates with
$cheapestRate = $this->getResult()->getCheapestRate()
. - Then, I reset the original collection with
$this->getResult()->reset()
I append the cheapest rate to the now clean collection of methods with
$method->setCarrier($cheapestRate['carrier']); $method->setCarrierTitle($cheapestRate['carrier_title']); $method->setMethod($cheapestRate['method']); $method->setMethodTitle($cheapestRate['method_title']); $method->setCost($cheapestRate['cost']); $method->setPrice($cheapestRate['price']); $result->append($method); $this->getResult()->append($result);
I'm displaying the cheapest rate so far. Now, I want to add a custom method with a cost of $0.00. This is what I do:
private function addCustomShippingToMethods($method, $result) { $method->setCarrier('custom_shipping'); $method->setCarrierTitle('custom_shipping'); $method->setMethod('custom_shipping'); $method->setMethodTitle('custom_shipping'); $method->setPrice(0); $method->setCost(0); $result->append($method); return $this; }
- So, to insert the custom method in step 4, I call addCustomShippingToMethods right after the last line in step 3 like this
$this->addCustomShippingToMethods($method, $result);
The problem is that I only see the custom method in the checkout page but not the cheapest one. If I remove the call to add the custom method then I only see the cheapest one.
In a few words, all the shipping options I want to see in the checkout page are the cheapest rate and the custom shipping at $0.00.
What am I doing wrong?
Thanks for any advice.