I have this piece of code:

$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
//result is: mycarrier_mymethodcode

My problem is that it is a string, not an object. So I can not use the


How can I get a real shipping method object like this:

 $order = $observer->getEvent()->getData('order');
 $shippingMethod = $order->getShippingMethod(true);
 //the true flag means returns with object not string

by my mycarrier_mymethodcode string?

  • Can you specify what you are trying to do? – Dhiren Vasoya May 3 '18 at 14:06
  • I have a custom shipping method, mymethod. Sadly, magento2 uses as carrercode_methodcode. When I show my block, I should need to check, is this carrier is my carrier. I do not want to use preg_* funcitons. So I want to get the shipping method carrier code. – vaso123 May 3 '18 at 14:11
  • Later, I also need to get some items from my repositories by method code. I could hack the whole thing with regex, I just want to do not. – vaso123 May 3 '18 at 14:17
  • Want to for order or Quote? – Amit Bera May 3 '18 at 14:44
  • Definetly quote. For order it's ok. I want to do this when I create an order at the dashboard, and no real order object exists. – vaso123 May 3 '18 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.