Home Forums WoodMart support forum Woodmart widget stock status AND instead of OR

Woodmart widget stock status AND instead of OR

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #457086

    Lexp
    Participant

    Hi,

    I am using the Woodmart stock status widget.
    When a user selects both “in stock” and “on backorder” it shows no results.

    I guess because the filter works with AND value.

    How can I change this to OR so when both are selected, the “in stock” and the “on backorder” will be shown?

    #457557

    Artem Temos
    Keymaster

    Hello,

    Try to add the following PHP code snippet to the child theme functions.php file to do this

    add_action( 'init', 'wd_update_product_query' );
    
    function wd_update_product_query() {
    	if ( class_exists('WOODMART_Stock_Status' ) ) {
    		remove_action( 'woocommerce_product_query', array( 'WOODMART_Stock_Status', 'show_in_stock_products' ) );
    	}
    
    	add_action( 'woocommerce_product_query', function ( $query ) {
    		$current_stock_status = isset( $_GET['stock_status'] ) ? explode( ',', $_GET['stock_status'] ) : array(); //phpcs:ignore
    
    		if ( in_array( 'instock', $current_stock_status, true ) || in_array( 'onbackorder', $current_stock_status, true ) ) {
    			$meta_query = array(
    				'relation' => 'OR',
    			);
    
    			if ( in_array( 'instock', $current_stock_status, true ) ) {
    				$meta_query[] = array(
    					'key'     => '_stock_status',
    					'value'   => 'instock',
    					'compare' => '=',
    				);
    			}
    
    			if ( in_array( 'onbackorder', $current_stock_status, true ) ) {
    				$meta_query[] = array(
    					'key'     => '_stock_status',
    					'value'   => 'onbackorder',
    					'compare' => '=',
    				);
    			}
    
    			$query->set( 'meta_query', array_merge( WC()->query->get_meta_query(), $meta_query ) );
    		}
    	} );

    Kind Regards

Viewing 2 posts - 1 through 2 (of 2 total)