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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-28214
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

fix stock status is not correct in JSON structure data if product is onbackorder
18 changes: 17 additions & 1 deletion plugins/woocommerce/includes/class-wc-structured-data.php
Expand Up @@ -257,9 +257,25 @@
);
}

$stock_status = $product->get_stock_status();

Check warning on line 260 in plugins/woocommerce/includes/class-wc-structured-data.php

View check run for this annotation

Codecov / codecov/patch

plugins/woocommerce/includes/class-wc-structured-data.php#L260

Added line #L260 was not covered by tests

switch ( $stock_status ) {
case 'instock':
$stock_status_schema = 'InStock';
break;
case 'outofstock':
$stock_status_schema = 'OutOfStock';
break;
case "onbackorder":
$stock_status_schema = 'BackOrder';
break;

Check warning on line 271 in plugins/woocommerce/includes/class-wc-structured-data.php

View check run for this annotation

Codecov / codecov/patch

plugins/woocommerce/includes/class-wc-structured-data.php#L263-L271

Added lines #L263 - L271 were not covered by tests
default:
$stock_status_schema = '';

Check warning on line 273 in plugins/woocommerce/includes/class-wc-structured-data.php

View check run for this annotation

Codecov / codecov/patch

plugins/woocommerce/includes/class-wc-structured-data.php#L273

Added line #L273 was not covered by tests
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

}

$markup_offer += array(
'priceCurrency' => $currency,
'availability' => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
'availability' => 'http://schema.org/' . $stock_status_schema,

Check warning on line 278 in plugins/woocommerce/includes/class-wc-structured-data.php

View check run for this annotation

Codecov / codecov/patch

plugins/woocommerce/includes/class-wc-structured-data.php#L278

Added line #L278 was not covered by tests
'url' => $permalink,
'seller' => array(
'@type' => 'Organization',
Expand Down