0

We (almost) always use UPS for our shipments, so it would be nice for that to be the default dropdown menu option that is selected when we go into an order, click ship, and go to add tracking information. The current default is 'Custom Value'. How/where in the code would I go about changing it to one of the other carrier options by default?

Magento 2.4.5p1

order shipment carrier dropdown menu order shipment carrier dropdown preferred option

1 Answer 1

1

To set a carrier option as default when creating a shipment, you need to override the template file responsible for displaying carrier options: vendor/magento/module-shipping/view/adminhtml/templates/order/tracking.phtml. Add the selected HTML attribute to the option for the desired carrier.

Assuming you want to set the UPS (United Parcel Service) option as the default option. It has the carrier code ($_code) is ups.

Original code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"><?= $block->escapeHtml($_name) ?></option>

Modified code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"<?= $_code === 'ups' ? ' selected' : '' ?>><?= $block->escapeHtml($_name) ?></option>

You can use one of the following methods to override that template file:

  1. Implement the changes in your custom module
  2. Implement the changes in your custom admin theme
  3. Implement the changes via a custom composer patch

Let's go through each method.

  1. Implement the changes in your custom module: Assuming you have a custom module with the vendor name is TuVan and the module name is Shipping:
  • Create app/code/TuVan/Shipping/view/adminhtml/layout/adminhtml_order_shipment_new.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="shipment_tracking" template="TuVan_Shipping::order/tracking.phtml"/>
    </body>
</page>
  • Create app/code/TuVan/Shipping/view/adminhtml/layout/sales_order_invoice_new.xml for the create new invoice page, which also has the tracking form:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="tracking" template="TuVan_Shipping::order/tracking.phtml"/>
    </body>
</page>
  • Copy the original template file vendor/magento/module-shipping/view/adminhtml/templates/order/tracking.phtml to your module. The new file will have the following path:
    app/code/TuVan/Shipping/view/adminhtml/templates/order/tracking.phtml
    Next, modify the code as I mentioned above, which is:

Original code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"><?= $block->escapeHtml($_name) ?></option>

Modified code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"<?= $_code === 'ups' ? ' selected' : '' ?>><?= $block->escapeHtml($_name) ?></option>
  1. Implement the changes in your custom admin theme: Assuming you have a custom theme with the vendor name is TuVan and the theme name is backend
  • Copy the original template file vendor/magento/module-shipping/view/adminhtml/templates/order/tracking.phtml to your theme. The new file will have the following path:
    app/design/adminhtml/TuVan/backend/Magento_Shipping/templates/order/tracking.phtml
    Next, modify the code as I mentioned above, which is:

Original code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"><?= $block->escapeHtml($_name) ?></option>

Modified code:

<option value="<?= $block->escapeHtmlAttr($_code) ?>"<?= $_code === 'ups' ? ' selected' : '' ?>><?= $block->escapeHtml($_name) ?></option>
  1. Implement the changes via a custom composer patch: refer to this page to know how to create a custom composer patch: How to create a Magento 2 Patch?

To apply a custom composer patch, I recommend using Vaimo Composer Patches plugin.
You also can use the Cweagans Composer Patches plugin: https://github.com/cweagans/composer-patches
There is a Magento official document on how to apply a custom composer patch: https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/patches/apply.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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