Pages

Saturday, September 21, 2019

Woocommerce Products Filter by Attribute Code

<?php
/**
 * Deasier.astra Theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Deasier.astra
 * @since 1.0.0
 */

/**
 * Define Constants
 */
define( 'CHILD_THEME_DEASIER_ASTRA_VERSION', '1.0.0' );

/**
 * Enqueue styles
 */





//** filter **//
add_action( 'woocommerce_product_query', 'so_20990199_product_query' );

function so_20990199_product_query( $q ){
    if (isset($_POST['selected_attributes_data'])){
       
        echo "<div style='display:none;'><pre>";
        print_r($_POST);
        echo "</pre></div>";
        // exit();
        $living_room_size_x = $_POST['living_room_size_length'] * $_POST['living_room_size_width'];
        //echo $living_room_size_x;
        if ($living_room_size_x < 320.42) {
            $living_room_size_x = 'smalllength-x-width-˂32042';
        }else {
            $living_room_size_x = 'large-length-x-width-≥32042';
        }
        $height_of_nearest_sofa = $_POST['height-of-the-nearest-sofa'];
        if ($height_of_nearest_sofa >= '19'){
            $height_of_nearest_sofa = 'high-h-˃-19-inches';
        }else if ($height_of_nearest_sofa >= '17.5' && $height_of_nearest_sofa <= '19'){
            $height_of_nearest_sofa = 'meduim-175-≤-h-≤-19-inches';
        }else {
            $height_of_nearest_sofa = 'low-h-˂-175-inches';
        }
       
        $q->set('tax_query', array(
            'relation'=>'AND',
            array(
                'taxonomy' => 'pa_the-main-uses',
                'field'    => 'slug',
                'terms'    => array($_POST['pa_the-main-uses']), // Don't display products in this category on the shop page
                'operator' => 'IN',
            ),
            array(
              'taxonomy'        => 'pa_living-room-style',
              'field'           => 'slug',
              'terms'           => array($_POST['living-room-style']),
              'operator'        => 'IN'
            ),
            array(
              'taxonomy'        => 'pa_living-room-size',
              'field'           => 'slug',
              'terms'           => array($living_room_size_x),
              'operator'        => 'IN'
            ),
            array(
                  'taxonomy' => 'pa_height-of-the-nearest-sofa',
                  'field'    => 'slug',
                  'terms'    => array($height_of_nearest_sofa), // Don't display products in this category on the shop page
                  'operator' => 'IN',
              ),
            array(
              'taxonomy'        => 'pa_type-of-the-nearest-sofa',
              'field'           => 'slug',
              'terms'           => array($_POST['pa_type-of-the-nearest-sofa']),
              'operator'        => 'IN'
            )
        ));
    }

     $product_ids_on_sale = wc_get_product_ids_on_sale();

     $q->set( 'post__in', (array) $product_ids_on_sale );
    //  echo "<pre>";
    //     print_r($q);
    //     echo "</pre>";
    //     echo "hello";exit;
   

}

No comments:

Post a Comment