0

I am trying to get Shipping Method , so I am using

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Api\OrderRepositoryInterface')->get($orderid);

$order->getShippingDescription()

It gives me something like Free Ground Shipping, Shipping Table Rates - 2-day

Should just be 2 -day or Ground Shipping. I don't know how getShippingDescription() gives me extra details and what is this Shipping Table Rates?

I tried this method also $order->getShippingMethod(); gives me freeshipping_freeshipping, freeshipping two times

I checked sales_order table also, so getShippingDescription() content is coming from that column(shipping description).

I tried this also $orderInfo->getShippingAddress()->getShippingMethod() gives me null

0

First, check the sales_order database table, you will find a column name shipping_description and getShippingDescription function return result like Free Ground Shipping, Shipping Table Rates - 2-day.

At freeshipping_freeshipping ,the first freeshipping Carrier code and second freeshipping is means Shipping method code.

    $method = $this->_rateMethodFactory->create();

    $method->setCarrier('freeshipping');
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod('freeshipping');
    $method->setMethodTitle($this->getConfigData('name'));

    $method->setPrice('0.00');
    $method->setCost('0.00');

At Magento,ShippingMethod = carrierCode_ShippingMethodCode,

So, what why you getting freeshipping_freeshipping.

At Magento every shipping Carrie has single Carrier code but can have multiple Method code. An example is the Matrix Shipping Rate.

getShippingDescription() of Sales order object give the combination of

CarrierTitle_MethodTitle.

Most of the cases, Carrier title basically from System Configuration field title of that Shipping Carrier.

If you want to just 2 -day or Ground Shipping then you have to Call System configuration value of Shipping carrier title field.

 protected $scopeConfig;

   const XML_PATH_TITLE_FIELD = 'carriers/flatrate/title';

   public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
   {
      $this->scopeConfig = $scopeConfig;
   }

   /**
   * Sample function returning config value
   **/

  public function getShippingtitle() {
     $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

     return $this->scopeConfig->getValue(self::XML_PATH_TITLE_FIELD, $storeScope);


     }

then str_replace($order->getShippingDescription())

str_replace($this->getShippingtitle(),"",$order->getShippingDescription());

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.