Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to require shipping tracking code when changing order status to shipped #1244

Closed
moldedjelly opened this issue Jan 30, 2020 · 6 comments
Closed

Comments

@moldedjelly
Copy link

Description

I'm trying to use the orderStatusChange event to detect when changing the order status to shipped, to then check if a shipping code has been supplied

This is my code:
Event::on(OrderHistories::class, OrderHistories::EVENT_ORDER_STATUS_CHANGE, function(OrderStatusEvent $e) {
$order = $e->order;
if (empty($order->trackingCode)) {
$e->isValid = false;
// would be nice to also show an error with feedback?
}
});

Looking at the OrderStatusEvent class, it appears to extend CancelableEvent so should be able to do this?

How might I also provide some feedback too?

@moldedjelly
Copy link
Author

@lukeholder that trello link is telling me that the card is not found

@lukeholder
Copy link
Member

Sorry @moldedjelly we were testing an integration.

The order status change should not be cancellable, that is a bug, sorry about that. Will be fixed in the next release.

In commerce 3, you should hook into the beforeValidate order event and add a validation error to the order if they are trying to save the order with a different status ID (and they have no trackingCode present). You would need to do a query of the order from the DB to get the current status ID and compare it to the new status ID.

@moldedjelly
Copy link
Author

so I updated my code as below which seems to work and gives a very general error at top of CP order edit page: "The order is not valid". The only thing is, it doesn't present the error on the trackingCode field - but it does show at base of "Order Details" tab as red JSON - should it be showing the error on the field?

        Event::on(Order::class, Order::EVENT_BEFORE_VALIDATE, function (Event $event) {
            $order = $event->sender;
            if ($order->orderStatusId == 2 && empty($order->trackingCode) && $order->shippingCompany->value != 'noTracking') {
                $order->addError('trackingCode', Craft::t('app', 'Need a tracking code'));
                $event->handled = true;
            }
        });

@lukeholder
Copy link
Member

Hi @moldedjelly

You would use $order->addError('field:trackingCode',

lukeholder added a commit that referenced this issue Feb 1, 2020
@moldedjelly
Copy link
Author

@lukeholder thanks for the help on this. I updated my code as you suggested. It still doesn't present the error on the field though - it presents the error at base of Order Details tab screenshot here
Is that intended behaviour?

@nfourtythree
Copy link
Contributor

Hi @moldedjelly

We are currently working on updates to the Order Edit screen for 3.1 and this is something we are taking into consideration.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants