1

I downloaded the BeRocket AJAX Filter plugin and it has changed the way my url query strings are formatted and now my function for getting product images won't work.

This is how I was getting the query string;

<php $filter_colour = $_GET['filter_colour']; ?>

This worked fine when my query string was like so;

www.website.co.uk/?filter_color=blue&filter_type_of_light=something

But now I can only have my query strings like the following by altering the settings in the plugin;

www.website.co.uk/?filters=type-of-light[550]|colour[569]
www.website.co.uk/?filters=type-of-light[ceiling-lights]|colour[pink]
www.website.co.uk/filters/type-of-light=ceiling-lights&colour=green

How can I still use $_GET to get the value of colour in the url with these links?

There are also these settings in the permalinks tab in wordpress, that I can use to alter the query string;

enter image description here

enter image description here

enter image description here

These are my options in the plugin for the query strings;

enter image description here

Here is the function where I am using $_GET to get the colour value, I use this function to set the images on the products;

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'set_product_image', 10);

function set_product_image() {
    global $product;

    if( $product->is_type('variable') ){
        // color of first variation
        $default_image = '';

        foreach ( $product->get_visible_children() as $variation_id ){            
            $variation = wc_get_product( $variation_id );
            $product_colour = strtolower( $variation->get_attribute('colour') );

            // get first variation
            if( $default_image == '' ){
                $default_image = $variation->get_image( array(300, 300) );
            }
            $filter_colour = $_GET['colour'];

            if( $product_colour == $filter_colour){
                // if filter applied, echo and return
                echo $variation->get_image( array(300, 300) );
                return;
            }
        }
        // filter not applied, return default image
        echo $default_image ;
    }

    else if( $product->is_type('simple') ){
        if ( has_post_thumbnail() ) {
            echo $product->get_image( array(300, 300) );
        }else{
            echo '<img src="https://website.co.uk/wp-content/themes/dist/images/placeholder.png">';
        }
    }
}
4
  • Do you just want to get the colour part alone? Commented Sep 3, 2018 at 15:51
  • Yeah I just want the value(s) of colour Commented Sep 3, 2018 at 15:54
  • @user3783243 I was able to slightly modify your code to do what I needed. If you add it as an answer I can accept it. Thanks Commented Sep 4, 2018 at 9:45
  • @Reece I've moved that to an answer. Commented Sep 4, 2018 at 11:12

2 Answers 2

1

For two of the example strings you can use a regex to identify the bit you want. For the third example colour should be its own index so just grab it as is. For the other two search for the colour[ then capture everything until the first ]. This will put the value into $color as the 1 index. It is the 1 index because it is the first capture group, the 0 index has the full match. Also if the u is optional in colour you can add a ? after it.

if(empty($_GET['colour'])){
    if(preg_match('/colour\[(.*?)\]/', $_GET['filters'], $color)){
            echo $color[1] . PHP_EOL;
    }
} else {
     echo $_GET['colour'];//just for demo purposes, will open XSS injection, escape for real usage
}

PHP in action: https://3v4l.org/SbAvR

Sign up to request clarification or add additional context in comments.

Comments

1

Stick with this URL structure - http://www.website.co.uk/?filters=type-of-light[ceiling-lights]|colour[pink]. You can use regex as @user3783243 suggests or you can use PHP explode. This is one of the easiest but not proper way.

$colortemp = explode('colour[', $_GET['filters']);
$colortemp1 = explode(']', $colortemp[1]);
$color = $colortemp[0];

Then you can replace in your function like $filter_colour = $color;.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.