How to Implement a Custom Order Status in Magento 2

How to Implement a Custom Order Status in Magento 2

If you're running a Magento 2 store, you know how important statut de commandees are for managing your sales process. By default, Magento 2 comes with a set of predefined statut de commandees like "Pending," "Processing," and "Complete." But what if your entreprise has unique flux de travails that require custom statut de commandees? That's where implementing a custom statut de commande comes into play.

Dans ce guide, nous'll walk you through the process of creating and implementing a custom statut de commande in Magento 2. Whether you're a beginner or just need a refresher, we'll break it down étape par étape. Plongeons-nous dans le sujet!

Why Would You Need a Custom Order Status?

Custom statut de commandees peut être incredibly useful for tailoring your store's flux de travail to your specific entreprise needs. Par exemple, you might want to add a status like "Awaiting Payment," "Ready for Pickup," or "Customs Clearance" to better reflect the stages of your commande fulfillment process. This not only vous aide àr team stay organized but also provides better transparency for your clients.

Step 1: Create a Custom Module

To implement a custom statut de commande, you'll need to create a custom module. Don't worry—it's easier than it sounds! Voici comment you can do it:

  1. Create the Module Directory: Navigate to your Magento 2 root répertoire and create a new dossier under app/code. Par exemple, let's name it Magefine/CustomOrderStatus.
  2. Create the registration.php File: Inside your répertoire du module, create a fichier named registration.php and add les éléments suivants code:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magefine_CustomOrderStatus',
    __DIR__
);
  1. Create the module.xml File: Ensuite, create a dossier named etc inside your répertoire du module, and within it, create a fichier named module.xml. Add les éléments suivants code:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magefine_CustomOrderStatus" setup_version="1.0.0"/>
</config>

This sets up the basic structure of your custom module.

Step 2: Define the Custom Order Status

Now that your module is set up, it's time to define your custom statut de commande. Voici comment:

  1. Create the InstallData.php File: Inside your répertoire du module, create a dossier named Setup, and within it, create a fichier named InstallData.php. Add les éléments suivants code:
<?php
namespace Magefine\CustomOrderStatus\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;

class InstallData implements InstallDataInterface
{
    protected $statusFactory;
    protected $statusResourceFactory;

    public function __construct(
        StatusFactory $statusFactory,
        StatusResourceFactory $statusResourceFactory
    ) {
        $this->statusFactory = $statusFactory;
        $this->statusResourceFactory = $statusResourceFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $status = $this->statusFactory->create();
        $status->setData([
            'status' => 'custom_status',
            'label' => 'Custom Status'
        ]);

        $statusResource = $this->statusResourceFactory->create();
        $statusResource->save($status);

        $status->assignState(Order::STATE_PROCESSING, false, true);
    }
}

This code creates a new statut de commande named "Custom Status" and assigns it to the "Processing" state. Vous pouvez replace custom_status and Custom Status with your desired status code and label.

Step 3: Assign the Custom Status to an Order State

Dans Magento 2, statut de commandees are linked to commande states. Par exemple, the "Processing" state might have statuses like "Processing" and "Custom Status." To ensure your custom status is properly linked, you can use les éléments suivants code snippet:

$status->assignState(Order::STATE_PROCESSING, false, true);

This line assigns your custom status to the "Processing" state. Vous pouvez replace Order::STATE_PROCESSING with any other state, tel que Order::STATE_NEW or Order::STATE_COMPLETE, depending on your needs.

Step 4: Deploy and Test

Une fois you've set up your custom statut de commande, it's time to déployer and test it. Follow these étapes:

  1. Run Setup Upgrade: Open your terminal and navigate to your Magento 2 root répertoire. Run les éléments suivants command to apply your changes:
php bin/magento setup:upgrade
  1. Clear Cache: Clear the Magento cache to ensure your changes take effect:
php bin/magento cache:clean
  1. Test the Custom Status: Go to your Magento panneau d'administration, navigate to an commande, and try assigning your custom status. Vous devriez see it listed in the status dropdown.

Step 5: Customize Further (Optional)

Si vous want to take your custom statut de commande a étape further, you can customize its appearance in the panneau d'administration or add e-mail notifications. Par exemple, you can create a custom e-mail template that gets sent when an commande is assigned your custom status. To do this, you'll need to:

  1. Create a new e-mail template in the Magento panneau d'administration.
  2. Modify the sales_email_order_comment_template configuration to use your custom template.
  3. Add logic in your module to trigger the e-mail when the custom status is assigned.

Conclusion

Implementing a custom statut de commande in Magento 2 is a powerful way to tailor your store's flux de travail to your entreprise needs. By following the étapes outlined in this guide, you can easily create and assign custom statut de commandees, improving both your team's efficiency and your clients' experience.

If you're looking for more advanced personnalisation or need help with Magento 2 hosting, check out Magefine.com. We offer a range of extensions and hosting solutions to help you get the most out of your Magento store.

Happy coding!