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
fix/28214 Backorders have a wrong availability on application/ld+json #37837
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @NguyenBao10,
Thanks for your contribution 💯! This works great, but after exploring a bit more how things are working correctly, I left a suggestion.
Even though your code is correct, the current implementation uses is_in_stock()
instead of explicitly checking the stocks status via get_stock_status()
, so it allows 3d party plugins to mark products as in stock regardless of the exact status.
We don't want to lose that, but we do want to handle backorders better, and I described a way to do that, but feel free to implement it other ways.
Also: +100 points on adding the changelog file with the PR, which is something a few people struggle with. I think it'd also be good to write some testing instructions as part of the PR description.
Please let me know what you think!
$stock_status_schema = 'BackOrder'; | ||
break; | ||
default: | ||
$stock_status_schema = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should allow an empty $stock_status_schema
as that might result in broken JSON. Also, looking at how this is working right now, I can see that we are really using the result from is_in_stock()
which involves a filter.
We don't want to lose that flexibility (for other plugins to use that filter to change the "in stock" status) nor affect backwards compatibility, so I'd suggest we do things slightly different.
How about instead of a case
we do a check for is_in_stock()
and set $stock_status_schema
to either InStock
or BackOrder
if the stock status is explicitly "backorder". Otherwise, we set $stock_status_schema
to OutOfStock
.
That way we preserve current behavior and also reduce the chances that we end up with an empty $stock_status_schema
.
For example, something along these lines might work:
if ( $product->is_in_stock() ) {
$stock_status_schema = ( 'onbackorder' === $product->get_stock_status() ) ? 'BackOrder' : 'InStock';
} else {
$stock_status_schema = 'OutOfStock';
}
What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i did not notice about 3d party plugins, so your way is more flexible and better, let me improve this, thank you
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## trunk #37837 +/- ##
==========================================
- Coverage 51.5% 51.5% -0.0%
- Complexity 17280 17283 +3
==========================================
Files 430 430
Lines 80026 80037 +11
==========================================
+ Hits 41215 41216 +1
- Misses 38811 38821 +10
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you @NguyenBao10 for your contribution!
Closes #28214.
Testing instruction:
onbackorder = BackOrder
instock = InStock
outofstock = OutOfStock