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

Commit

Permalink
Removed hideOutOfStock from AllProducts and realized hide out of stoc…
Browse files Browse the repository at this point in the history
…k functionality from SQL query
  • Loading branch information
grogou committed Aug 20, 2021
1 parent 94a54e3 commit 594125a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 43 deletions.
2 changes: 0 additions & 2 deletions assets/js/base/components/product-list/container.js
Expand Up @@ -11,7 +11,6 @@ import ProductList from './product-list';

const ProductListContainer = ( {
attributes,
hideOutOfStockItems = false,
} ) => {
const [ currentPage, setPage ] = useState( 1 );
const [ currentSort, setSort ] = useState( attributes.orderby );
Expand All @@ -31,7 +30,6 @@ const ProductListContainer = ( {
return (
<ProductList
attributes={ attributes }
hideOutOfStockItems={ hideOutOfStockItems }
currentPage={ currentPage }
onPageChange={ onPageChange }
onSortChange={ onSortChange }
Expand Down
48 changes: 12 additions & 36 deletions assets/js/base/components/product-list/product-list.js
Expand Up @@ -31,7 +31,6 @@ const generateQuery = ( {
sortValue,
currentPage,
attributes,
hideOutOfStockItems,
} ) => {
const { columns, rows } = attributes;
const getSortArgs = ( orderName ) => {
Expand All @@ -57,31 +56,11 @@ const generateQuery = ( {
}
};

// eslint-disable-next-line react-hooks/rules-of-hooks
const [ productStockStatus, setProductStockStatus ] = useQueryStateByKey(
'stock_status',
[]
);
// @todo Find way to exclude "outofstock" from existing product filters.
if ( hideOutOfStockItems ) {
let newStockStatus = [ 'instock', 'onbackorder' ];
if ( productStockStatus.length > 0 ) {
newStockStatus = productStockStatus;
if ( productStockStatus.includes( 'outofstock' ) ) {
newStockStatus = productStockStatus.filter(
( slug ) => slug !== 'outofstock'
);
}
}
setProductStockStatus( newStockStatus );
}

return {
...getSortArgs( sortValue ),
catalog_visibility: 'catalog',
per_page: columns * rows,
page: currentPage,
stock_status: productStockStatus,
};
};

Expand Down Expand Up @@ -135,14 +114,24 @@ const ProductList = ( {
onSortChange,
sortValue,
scrollToTop,
hideOutOfStockItems = false,
} ) => {
// These are possible filters.
const [ productAttributes, setProductAttributes ] = useQueryStateByKey(
'attributes',
[]
);
const [ productStockStatus, setProductStockStatus ] = useQueryStateByKey(
'stock_status',
[]
);
const [ minPrice, setMinPrice ] = useQueryStateByKey( 'min_price' );
const [ maxPrice, setMaxPrice ] = useQueryStateByKey( 'max_price' );

const [ queryState ] = useSynchronizedQueryState(
generateQuery( {
attributes,
sortValue,
currentPage,
hideOutOfStockItems,
} )
);
const { products, totalProducts, productsLoading } = useStoreProducts(
Expand All @@ -152,18 +141,6 @@ const ProductList = ( {
const totalQuery = extractPaginationAndSortAttributes( queryState );
const { dispatchStoreEvent } = useStoreEvents();

// These are possible filters.
const [ productAttributes, setProductAttributes ] = useQueryStateByKey(
'attributes',
[]
);
const [ productStockStatus, setProductStockStatus ] = useQueryStateByKey(
'stock_status',
[]
);
const [ minPrice, setMinPrice ] = useQueryStateByKey( 'min_price' );
const [ maxPrice, setMaxPrice ] = useQueryStateByKey( 'max_price' );

// Only update previous query totals if the query is different and the total number of products is a finite number.
const previousQueryTotals = usePrevious(
{ totalQuery, totalProducts },
Expand Down Expand Up @@ -277,7 +254,6 @@ const ProductList = ( {

ProductList.propTypes = {
attributes: PropTypes.object.isRequired,
hideOutOfStockItems: PropTypes.bool,
// From withScrollToTop.
scrollToTop: PropTypes.func,
};
Expand Down
4 changes: 0 additions & 4 deletions assets/js/blocks/products/all-products/block.js
Expand Up @@ -6,7 +6,6 @@ import PropTypes from 'prop-types';
import { ProductListContainer } from '@woocommerce/base-components/product-list';
import { InnerBlockLayoutContextProvider } from '@woocommerce/shared-context';
import { gridBlockPreview } from '@woocommerce/resource-previews';
import { getSetting } from '@woocommerce/settings';

/**
* The All Products Block.
Expand All @@ -26,8 +25,6 @@ class Block extends Component {
return gridBlockPreview;
}

const hideOutOfStockItems = getSetting( 'hideOutOfStockItems', false );

/**
* Todo classes
*
Expand All @@ -42,7 +39,6 @@ class Block extends Component {
<ProductListContainer
attributes={ attributes }
urlParameterSuffix={ urlParameterSuffix }
hideOutOfStockItems={ hideOutOfStockItems }
/>
</InnerBlockLayoutContextProvider>
);
Expand Down
1 change: 0 additions & 1 deletion src/BlockTypes/AllProducts.php
Expand Up @@ -27,6 +27,5 @@ protected function enqueue_data( array $attributes = [] ) {
$this->asset_data_registry->add( 'min_rows', wc_get_theme_support( 'product_blocks::min_rows', 1 ), true );
$this->asset_data_registry->add( 'max_rows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true );
$this->asset_data_registry->add( 'default_rows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true );
$this->asset_data_registry->add( 'hideOutOfStockItems', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), true );
}
}
3 changes: 3 additions & 0 deletions src/StoreApi/Utilities/ProductQuery.php
Expand Up @@ -317,6 +317,9 @@ 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'] .= ' AND wc_product_meta_lookup.stock_status IN ("' . implode( '","', array_map( 'esc_sql', $wp_query->get( 'stock_status' ) ) ) . '")';
} elseif ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] .= ' AND wc_product_meta_lookup.stock_status NOT IN ("outofstock")';
}

if ( $wp_query->get( 'min_price' ) || $wp_query->get( 'max_price' ) ) {
Expand Down

0 comments on commit 594125a

Please sign in to comment.