0

I have installed my Shipping Tracker module in Magento 2.3.5p1, I have added a custom Shipping method which is showing in the dropdown in the backend on the Order shipment page as below,

Note: This issue is only on Magento 2.3.5p1, it's working fine for Magento 2.3.4

s1

After selecting the custom shipping method, the carrier is showing Custom Value instead of the Shiprocket1, Although it is showing for default methods. Check below screenshot,

s2

You can check the inspect element in the below screenshot so you can have more idea,

s3

Here is the code for adding a custom shipping method to dropdown,

Vendor/Shippingtracker/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Block\Adminhtml\Order\Tracking">
        <plugin name="carrier_options_backend_shipment" type="Vendor\Shippingtracker\Plugin\Block\Adminhtml\Order\Tracking" sortOrder="5" />
    </type>
</config>

Vendor/Shippingtracker/Plugin/Block/Adminhtml/Order/Tracking.php

<?php
 
namespace Vendor\Shippingtracker\Plugin\Block\Adminhtml\Order;

class Tracking
{
    public function __construct(
        \Vendor\Shippingtracker\Helper\Data $shippingTrackerHelper
    ) {
        $this->shippingTrackerHelper = $shippingTrackerHelper;
    }

    
    public function afterGetCarriers(\Magento\Shipping\Block\Adminhtml\Order\Tracking $subject, $result)
    {
        $customCarrier = $this->shippingTrackerHelper->getCustomCarrierTitle();
        if (!empty($customCarrier)) {
            foreach ($customCarrier as $code => $carrierTitle) {
                $result[$code] = $carrierTitle;
            }
        }
        return $result;
    }
}

Helper Method getCustomCarrierTitle()

public function getCustomCarrierTitle()
    {
        $j = 15;
        $customCarrier = [];
        for ($i=1; $i <= $j; $i++) {
            $enable = 'shippingtracker_section/custom_shippingtracker_'.$i.'/custom_shippingtracker_enable_'.$i;
            $Title = 'shippingtracker_section/custom_shippingtracker_'.$i.'/custom_shippingtracker_title_'.$i;
            $carrierEnable = $this->getConfigValue($enable);
            $carrierTitle = $this->getConfigValue($Title);
            if ($carrierEnable) { 
                if ($carrierTitle) {
                    $customCarrier['customcarrier'.$i] = $carrierTitle;
                }
            }
        }
        return $customCarrier;
    }

System.xml for $enable and $Title

<group id="custom_shippingtracker_1" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Custom Shipping Tracker 1</label>
                    <field id="custom_shippingtracker_enable_1" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Enable</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>  
                    <field id="custom_shippingtracker_title_1" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Method Title</label>
                        <validate>required-entry</validate>
                        <depends>
                            <field id="shippingtracker_section/custom_shippingtracker_1/custom_shippingtracker_enable_1">1</field>
                        </depends>
                    </field>
                    <field id="custom_shippingtracker_url_1" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Url</label>
                        <validate>required-entry</validate>
                        <comment>
                            <![CDATA[replace the TrackingNumber to {{tracking_code}},<br/> example:<br/><strong>"http://www.example.com?trackingNumber=#TRACKINGNUMBER#"</strong> To <br/><strong>"http://www.example.com?TrackingNumber={{tracking_code}}"</strong>]]>
                        </comment>
                        <depends>
                            <field id="shippingtracker_section/custom_shippingtracker_1/custom_shippingtracker_enable_1">1</field>
                        </depends>
                    </field>    
            </group>

I have also tried overriding the \Magento\Shipping\Block\Adminhtml\Order\Tracking class, but I don't see my custom shipping method in the dropdown so I don't think that's working as well.

Any help would be appreciated.

4
  • Can you provide the XML you're using to define $enable and $Title in your helper method? – kookaburra May 25 at 14:00
  • @kookaburra Please check the updated question – Mohit Rane May 25 at 14:24
  • Thanks. Do you have any default values for these fields set in config.xml? – kookaburra May 25 at 15:09
  • @kookaburra No, I have not set the default values for this field in config.xml – Mohit Rane May 26 at 5:33
1

Without seeing your config.xml, I believe Magento is failing to create a Carrier object, thus returning 'Custom Value' as a fallback title:

\Magento\Rma\Block\Adminhtml\Rma\Edit\Tab\General\Shipping\Tracking

public function getCarrierTitle($code)
    {
        $carrier = $this->_carrierFactory->create($code);
        return $carrier ? $carrier->getConfigData('title') : __('Custom Value');
    }

So why can't Magento create the Carrier object? Well, Magento attempts to create it from config, and returns false if it can't get a value:

\Magento\Shipping\Model\CarrierFactory

public function get($carrierCode)
    {
        $className = $this->_scopeConfig->getValue(
            'carriers/' . $carrierCode . '/model',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        if (!$className) {
            return false;
        }
        // operations if configuration value is found
    }

The next question, then, is twofold: (1) where is the carrier configured, and (2) what values does Magento need? Note that Magento is specifically looking for a model value at a carriers/{carrier_code}/model location. Here's how Magento does this for the UPS carrier:

vendor/magento/module-ups/etc/config.xml

<default>
        <carriers>
        <ups>
            <!-- some values -->
            <title>United Parcel Service</title>
            <model>Magento\Ups\Model\Carrier</model>
            <!-- more values -->
        </ups>
    </carriers>
</default>

So Magento is looking for a valid model class name that the ObjectManager can use to create the carrier object, from which Magento can then extract the carrier's title. If there's no such class or no such config value, then object creation will abort and your shipping method title will default to 'Custom Value.'

If this is your problem, then this should be enough to get you started. Adobe has a good tutorial for creating a custom shipping carrier which should help you ensure you've created and configured your carrier correctly.

1
  • Thank you for your time and efforts, I really appreciate it. I will checkout the link you provided. – Mohit Rane May 27 at 7:32
0

Finally, I found the answer by overriding this class Magento\Shipping\Block\Adminhtml\Order\Tracking\View

etc/di.xml

<preference for="Magento\Shipping\Block\Adminhtml\Order\Tracking\View" type="Vendor\Shippingtracker\Block\Adminhtml\Order\Tracking\View" />

Vendor/Shippingtracker/Block/Adminhtml/Order/Tracking/View.php

<?php
 
namespace Vendor\Shippingtracker\Block\Adminhtml\Order\Tracking;

class View extends \Magento\Shipping\Block\Adminhtml\Order\Tracking\View
{
    /**
      * @var \Magento\Shipping\Model\CarrierFactory
      */
    protected $_carrierFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Shipping\Model\Config $shippingConfig
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Shipping\Model\CarrierFactory $carrierFactory
     * @param \Vendor\Shippingtracker\Helper\Data $shippingTrackerHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Shipping\Model\Config $shippingConfig,
        \Magento\Framework\Registry $registry,
        \Magento\Shipping\Model\CarrierFactory $carrierFactory,
        \Vendor\Shippingtracker\Helper\Data $shippingTrackerHelper,
        array $data = []
    ) {
        $this->_carrierFactory = $carrierFactory;
        $this->shippingTrackerHelper = $shippingTrackerHelper;
        parent::__construct(
            $context,
            $shippingConfig,
            $registry,
            $carrierFactory,
            $data
        );
    }

    /**
     * Get carrier title
     *
     * @param string $code
     *
     * @return \Magento\Framework\Phrase|string|bool
     */
    public function getCarrierTitle($code)
    {
        $enable = $this->shippingTrackerHelper->getConfigValue('shippingtracker_section/shippingtracker_general/shippingtracker_enable');
        if ($enable) {
            $customCarrier = $this->shippingTrackerHelper->getCustomCarrierTitle();
            if (!empty($customCarrier)) {
                foreach ($customCarrier as $carrierCode => $carrierTitle) {
                    if ($code == $carrierCode) {
                        return $carrierTitle;
                    }
                }
            }
        }
        
        $carrier = $this->_carrierFactory->create($code);
        return $carrier ? $carrier->getConfigData('title') : __('Custom Value');
    }
}

Hope it helps someone.

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.