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

fix/28214 Backorders have a wrong availability on application/ld+json #37837

Merged
merged 3 commits into from Apr 19, 2023

Conversation

NguyenBao10
Copy link

@NguyenBao10 NguyenBao10 commented Apr 19, 2023

Closes #28214.

Testing instruction:

  • Create some products with different stock status, we have 3 status now (instock, outofstock, onbackorder), so 3 products is minimum
  • You can set stock status for those products at Inventory tab in product admin page as below

image

  • Visit each product's page and view page source, then find the availability attribute in LD+JSON schema
  • Make sure this value is correct with product's stock status:
    onbackorder = BackOrder
    instock = InStock
    outofstock = OutOfStock

image

@github-actions github-actions bot added plugin: woocommerce Issues related to the WooCommerce Core plugin. type: community contribution labels Apr 19, 2023
@woocommercebot woocommercebot requested review from a team and barryhughes and removed request for a team April 19, 2023 12:49
@jorgeatorres jorgeatorres requested review from jorgeatorres and removed request for barryhughes April 19, 2023 12:58
Copy link
Member

@jorgeatorres jorgeatorres left a 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 = '';
Copy link
Member

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?

Copy link
Author

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
Copy link

codecov bot commented Apr 19, 2023

Codecov Report

Merging #37837 (c5285ec) into trunk (dfb23ea) will decrease coverage by 0.0%.
The diff coverage is 0.0%.

❗ Current head c5285ec differs from pull request most recent head 4dd38a4. Consider uploading reports for the commit 4dd38a4 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
...ocommerce/includes/admin/class-wc-admin-assets.php 0.0% <ø> (ø)
...eta-boxes/class-wc-meta-box-product-categories.php 0.0% <0.0%> (ø)
plugins/woocommerce/includes/class-wc-ajax.php 4.3% <0.0%> (ø)
.../woocommerce/includes/class-wc-structured-data.php 0.0% <0.0%> (ø)

... and 1 file with indirect coverage changes

@jorgeatorres jorgeatorres self-requested a review April 19, 2023 15:20
Copy link
Member

@jorgeatorres jorgeatorres left a 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!

@jorgeatorres jorgeatorres merged commit 9aa4fd6 into woocommerce:trunk Apr 19, 2023
16 of 19 checks passed
@github-actions github-actions bot added this to the 7.8.0 milestone Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backorders have a wrong availability on application/ld+json
2 participants