RE: Adding Custom Commerce Order Status

Olaf Kock, modified 3 Years ago. New Member Posts: 6 Join Date: 6/23/22 Recent Posts

I am trying to add a custom READY_TO_SHIP commerce order status between PROCESSING and SHIPPED  commerce order status. The PROCESSING status does not end until the shipment goes to SHIPPED status. And then the order goes to the SHIPPED status, but I want to add the READY_TO_SHIP status when the shipment goes to the READY_TO_SHIP status.

So the problem is order status will still be PROCESSING until the shipment gets shipped so how can I insert the READY_TO_SHIP order status when a shipment gets to the READY_TO_SHIP status.

Also, one order will only have one shipment. (Is there any flag for this?)

thumbnail
Jeffrey Handa, modified 3 Years ago. Liferay Master Posts: 541 Join Date: 12/1/08 Recent Posts

Hi Jay, have you tried following the steps in this article?  

https://learn.liferay.com/commerce/latest/en/developer-guide/order-management/implementing-a-custom-order-status.html

If so, can you share what you've done and what problems you're experiencing?  

Also, one order will only have one shipment. (Is there any flag for this?)

No, I don't believe there is a flag for limiting the number of shipments per order.

Olaf Kock, modified 3 Years ago. New Member Posts: 6 Join Date: 6/23/22 Recent Posts

Hello Jeffrey, thanks for answering.

I want to add READY_TO_SHIP status between PROCESSING  and SHIPPED  status, so as per the article that you have suggested I have implemented the same steps and getting this outcome :

I have overridden the ShippedCommerceStatus as it is the next status after my custom status class as shown in below code blocks :

1) SchedulingCommerceOrderStatus.java (I have kept the class name same as given in exaple but changed the label and key for READY_TO_SHIP)

@Component(
    property = {
        "commerce.order.status.key=131167",
        "commerce.order.status.priority:Integer=55"
    },
    service = CommerceOrderStatus.class
)
public class SchedulingCommerceOrderStatus implements CommerceOrderStatus {

    @Override
    public CommerceOrder doTransition(CommerceOrder commerceOrder, long userId) throws PortalException {
        commerceOrder.setOrderStatus(131167);
        return _commerceOrderService.updateCommerceOrder(commerceOrder);
    }

    @Override
    public int getKey() {
        return 131167;
    }

    @Override
    public String getLabel(Locale locale) {
        return "Ready to Ship";
    }

    @Override
    public int getPriority() {
        return 55;
    }

    @Override
    public boolean isComplete(CommerceOrder commerceOrder) {
        ExpandoBridge expandoBridge = commerceOrder.getExpandoBridge();

        String[] values = (String[])expandoBridge.getAttribute(
            "ready to ship");

        if (ArrayUtil.isEmpty(values)) {
            return false;
        }

        return Objects.equals(values[0], "Confirmed");
    }

    @Override
    public boolean isTransitionCriteriaMet(CommerceOrder commerceOrder)
        throws PortalException {

        if (commerceOrder.getOrderStatus() == CommerceOrderConstants.ORDER_STATUS_PROCESSING) {

            return true;
        }
        return true;
    }

    @Reference
    private CommerceOrderService _commerceOrderService;

}
 

2) ShippedCommerceOrderStatus.java


@Component(
        property = {
            "commerce.order.status.key=" + CommerceOrderConstants.ORDER_STATUS_SHIPPED,
            "commerce.order.status.priority:Integer=60",
            "service.ranking:Integer=100"
        },
        service = CommerceOrderStatus.class
)
public class ShippedCommerceOrderStatus implements CommerceOrderStatus {

    @Override
    public CommerceOrder doTransition(CommerceOrder commerceOrder, long userId) throws PortalException {

        commerceOrder.setOrderStatus(CommerceOrderConstants.ORDER_STATUS_SHIPPED);

        return _commerceOrderService.updateCommerceOrder(commerceOrder);
    }

    @Override
    public int getKey() {
        return CommerceOrderConstants.ORDER_STATUS_SHIPPED;
    }

    @Override
    public String getLabel(Locale locale) {
        return "Shipped";
    }

    @Override
    public int getPriority() {
        return 60;
    }

    @Override
    public boolean isComplete(CommerceOrder commerceOrder) {

        if (commerceOrder.isApproved() && !commerceOrder.isOpen() && (commerceOrder.getOrderStatus() != 131167)) {
            return true;
        }

        return false;
    }

    @Override
    public boolean isTransitionCriteriaMet(CommerceOrder commerceOrder)
        throws PortalException {

        if (commerceOrder.getOrderStatus() == 131167) {
        return true;
    }

        return false;
    }

    @Override
    public boolean isValidForOrder(CommerceOrder commerceOrder)
        throws PortalException {

        if (!_commerceShippingHelper.isShippable(commerceOrder)) {
            return false;
        }

        return true;
    }
    @Reference
    private CommerceOrderService _commerceOrderService;
    @Reference
    private CommerceOrderEngine _commerceOrderEngine;
    @Reference
    private CommerceShippingHelper _commerceShippingHelper;
}