Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Handle out of stock product visibility setting in All Products block. #3859

Merged
merged 3 commits into from
Feb 18, 2021

Conversation

ralucaStan
Copy link
Contributor

A new hideOutOfStockItems setting flag has been added to reflect the user setting Hide out of stock items from the catalog. When true, the products call will include a field for stock_status: ['instock', 'onbackorder'].

Fixes #2824

How to test the changes in this Pull Request:

  1. Add All Products block to a page
  2. Mark one product as Out of stock, and one as On back order
  3. Select WC > Settings > Products > Inventory > Hide out of stock items from the catalog
  4. Go to the products block and see that the Out of stock product is missing, but the On back order is visible

PHP Changes

Not sure if the code here is the best, feel free to suggest improvements. Also make sure the code is not missing any edge cases.

Changelog

Handle out-of-stock product visibility setting in All Products block.

@ralucaStan ralucaStan added type: bug The issue/PR concerns a confirmed bug. focus: blocks Specific work involving or impacting how blocks behave. block-type: product-grid Issues related to/affecting all product-grid type blocks. labels Feb 17, 2021
@ralucaStan ralucaStan added this to the 4.6.0 milestone Feb 17, 2021
@ralucaStan ralucaStan requested a review from a team as a code owner February 17, 2021 10:59
@ralucaStan ralucaStan self-assigned this Feb 17, 2021
@ralucaStan ralucaStan requested review from Aljullu and removed request for a team February 17, 2021 10:59
@github-actions
Copy link
Contributor

github-actions bot commented Feb 17, 2021

Size Change: +106 B (0%)

Total Size: 1.15 MB

Filename Size Change
build/all-products-frontend.js 34.8 kB +55 B (0%)
build/all-products.js 36.4 kB +51 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 8.35 kB 0 B
build/active-filters.js 8.52 kB 0 B
build/all-reviews.js 9.89 kB 0 B
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 3.37 kB 0 B
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 335 B 0 B
build/atomic-block-components/add-to-cart-frontend.js 9.23 kB 0 B
build/atomic-block-components/add-to-cart.js 7.69 kB 0 B
build/atomic-block-components/button-frontend.js 2.39 kB 0 B
build/atomic-block-components/button.js 840 B 0 B
build/atomic-block-components/category-list-frontend.js 470 B 0 B
build/atomic-block-components/category-list.js 476 B 0 B
build/atomic-block-components/image-frontend.js 1.77 kB 0 B
build/atomic-block-components/image.js 1.23 kB 0 B
build/atomic-block-components/price-frontend.js 1.94 kB 0 B
build/atomic-block-components/price.js 1.96 kB 0 B
build/atomic-block-components/rating-frontend.js 520 B 0 B
build/atomic-block-components/rating.js 526 B 0 B
build/atomic-block-components/sale-badge-frontend.js 857 B 0 B
build/atomic-block-components/sale-badge.js 866 B 0 B
build/atomic-block-components/sku-frontend.js 389 B 0 B
build/atomic-block-components/sku.js 393 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 569 B 0 B
build/atomic-block-components/stock-indicator.js 573 B 0 B
build/atomic-block-components/summary-frontend.js 920 B 0 B
build/atomic-block-components/summary.js 925 B 0 B
build/atomic-block-components/tag-list-frontend.js 465 B 0 B
build/atomic-block-components/tag-list.js 473 B 0 B
build/atomic-block-components/title-frontend.js 1.35 kB 0 B
build/atomic-block-components/title.js 1.21 kB 0 B
build/attribute-filter-frontend.js 18.3 kB 0 B
build/attribute-filter.js 12.5 kB 0 B
build/blocks-checkout.js 17 kB 0 B
build/blocks.js 3.49 kB 0 B
build/cart-frontend.js 75.7 kB 0 B
build/cart.js 38.4 kB 0 B
build/checkout-frontend.js 80.4 kB 0 B
build/checkout.js 41.3 kB 0 B
build/editor-rtl.css 14.9 kB 0 B
build/editor.css 14.9 kB 0 B
build/featured-category.js 7.83 kB 0 B
build/featured-product.js 10.1 kB 0 B
build/handpicked-products.js 7.51 kB 0 B
build/price-filter-frontend.js 14.6 kB 0 B
build/price-filter.js 9.95 kB 0 B
build/price-format.js 1.34 kB 0 B
build/product-best-sellers.js 7.58 kB 0 B
build/product-categories.js 3.23 kB 0 B
build/product-category.js 8.52 kB 0 B
build/product-new.js 7.75 kB 0 B
build/product-on-sale.js 8.14 kB 0 B
build/product-search.js 3.58 kB 0 B
build/product-tag.js 6.58 kB 0 B
build/product-top-rated.js 7.72 kB 0 B
build/products-by-attribute.js 8.5 kB 0 B
build/reviews-by-category.js 12 kB 0 B
build/reviews-by-product.js 13.5 kB 0 B
build/reviews-frontend.js 9.52 kB 0 B
build/single-product-frontend.js 37.9 kB 0 B
build/single-product.js 10.3 kB 0 B
build/style-rtl.css 18.9 kB 0 B
build/style.css 18.8 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 6.53 kB 0 B
build/vendors-style-rtl.css 1.05 kB 0 B
build/vendors-style.css 1.05 kB 0 B
build/vendors.js 418 kB 0 B
build/wc-blocks-data.js 6.97 kB 0 B
build/wc-blocks-middleware.js 1.1 kB 0 B
build/wc-blocks-registry.js 2.67 kB 0 B
build/wc-payment-method-bacs.js 820 B 0 B
build/wc-payment-method-cheque.js 816 B 0 B
build/wc-payment-method-cod.js 913 B 0 B
build/wc-payment-method-paypal.js 853 B 0 B
build/wc-payment-method-stripe.js 12.2 kB 0 B
build/wc-settings.js 2.4 kB 0 B
build/wc-shared-context.js 1.53 kB 0 B
build/wc-shared-hocs.js 1.69 kB 0 B

compressed-size-action

Copy link
Contributor

@Aljullu Aljullu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see this being fixed. I needed to make a change to the code to get this working, but after that it was testing fine. I commented below the change I needed to do.

I would like @mikejolley to take a second look to this PR since I'm never 100% confident when reviewing code that queries the database, but to me it's looking good.

Also wondering if this is a breaking change to the store API. Should we keep supporting stock_status being a string in addition to an array?

'items' => array(
'type' => 'string',
),
'default' => [],
'sanitize_callback' => 'sanitize_text_field',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sanitize_callback needs to be inside items. Otherwise, it was not working for me. 😕

@@ -309,10 +309,14 @@ public function get_collection_params() {

$params['stock_status'] = array(
'description' => __( 'Limit result set to products with specified stock status.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'enum' => array_keys( wc_get_product_stock_status_options() ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if I wasn't clear yesterday. We should keep enum if possible, I just wasn't sure if it needs to be defined within or outside of items. Ideally we can keep this in place so invalid values are rejected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes pushed, please check again. I also moved sanitize_callback and validate_callback under items.

@mikejolley
Copy link
Member

mikejolley commented Feb 17, 2021

@Aljullu Supporting both a single string or an array would be interesting. I know you can define an array of types, but I am unsure who this would behave in conjunction with enum and items. It would probably be better to keep it consistent as an array however.

@ralucaStan Is any other code inside blocks using this query?

There are some docs too in the StoreAPI folder which need updating if this changes from string to array.

@@ -316,7 +316,7 @@ public function add_query_clauses( $args, $wp_query ) {

if ( $wp_query->get( 'stock_status' ) ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.stock_status = %s ', $wp_query->get( 'stock_status' ) );
$args['where'] .= ' AND wc_product_meta_lookup.stock_status IN ("' . implode( '","', array_map( 'esc_sql', $wp_query->get( 'stock_status' ) ) ) . '")';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikejolley I did not find another block using this query.

Thanks for the heads-up, I've updated the docs file.

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job 👍🏻 Also magically still works with strings and comma separated value, so must be some magic behind the scenes to convert those values automatically :)

A new `hideOutOfStockItems` setting flag has been added and it is used to hide the out of stock products
from the block. Products that are not out of stock can have 'instock', 'onbackorder' as stock status.
@ralucaStan ralucaStan force-pushed the fix/2824-all-products-stock branch from 99c01b0 to fe93c51 Compare February 17, 2021 16:14
@ralucaStan ralucaStan merged commit 93319b7 into trunk Feb 18, 2021
@ralucaStan ralucaStan deleted the fix/2824-all-products-stock branch February 18, 2021 09:53
@Magganpice
Copy link

My product blocks are still displaying out of stock products in WooCommerce 5.1.0. Am I missing something?

@opr opr mentioned this pull request Jul 6, 2021
5 tasks
@fishblizz
Copy link

Is this issue the same as when you don't put a price on a variation? Because they show up aswell.

@ralucaStan
Copy link
Contributor Author

Hi there @fishblizz,
thank you for reaching out. Could you please describe in detail the bug you are referring to? Some steps to reproduce the bug would be helpful. Depending on them we might need to open another issue.

@fishblizz
Copy link

Hi @ralucaStan, my reference is this issue wich kinda got forwarded to Github: https://wordpress.org/support/topic/product-with-variations-shows-product-without-a-price/page/2/#post-14787001

@ralucaStan
Copy link
Contributor Author

HI @fishblizz,
thanks for your comment. The issue from your link is different from this one and has a separate GitHub ticket on WC.

@fishblizz
Copy link

Hi @ralucaStan, thank you for reaction and confirmation that the other ticket is still actual. I will leave this one and move to the one you've pointed out.

@Magganpice
Copy link

This issue is still there. Wrong products are still displayed. Not sensitive to the "display out of stock" setting in WooCommerce settings. So the block still shows out of stock products. Is the issue not properly understood here?

@frontdevde
Copy link
Contributor

This issue is still there.

@Magganpice I wasn't able to reproduce the issue with the latest version of WooCommerce and WooCommerce Blocks. Could you please provide us with detailed steps on how to reliably reproduce the issue? We'd also appreciate it if you could share which versions of WordPress, WooCommerce, and WooCommerce Blocks you are using. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block-type: product-grid Issues related to/affecting all product-grid type blocks. focus: blocks Specific work involving or impacting how blocks behave. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Product block don't respect out-of-stock product visibility setting
6 participants