3

I've search the interwebs high and low looking for a way to conditionally display WooCommerce product attribute data on certain pages.

Ex: if attribute somethingcool has a value and it's on /?filtering=1&filter_manufacturer=1648 page, display value

I'd like to display an attribute differently if they are on a specific filtered page

Ex: http://www.colorselectcoil.com/color-match-tool/?filtering=1&filter_manufacturer=1648

Based on this filtered page, display a product attribute 'somethingcool'

<?php if ( is_product_attribute('somethingcool') ) {
    echo 'Hi! This is something cool?';}
    else {
    echo '';
  }
?>

If it were a normal WordPress page, not a filtered page I could hide and show based on body class tag but unfortunately the body class doesn't change based on query string urls.

Tom
  • 459
  • 2
  • 4
  • 17
  • Thanks Steve, I'm not nearly that advanced at PHP though – Tom Feb 06 '16 at 03:44
  • Not sure if is_product_attribute() is your own function but this function certainly seems to have the capacity to display product attributes - add the function to your functions.php in your child theme. http://isabelcastillo.com/show-woocommerce-product-attributes-on-cart-page – Steve Feb 06 '16 at 05:24
  • What does it show - if anything? Can you try `` to see what values - if any - you are getting? – Steve Feb 07 '16 at 00:24
  • Hi Tom - hope the function just posted might work for you! – Steve Feb 09 '16 at 17:14
  • @Tom added possible idea for full automation to function call. – Steve Feb 13 '16 at 23:03

2 Answers2

3

You could use the url search string and extract from it whatever part you wanted. For example, if you only wanted to know if you were on a filtered page then you wouldn't bother checking for the manufacturer.

   <?php if ( $product->get_attribute('Certainteed') && intval($_GET['filtered']) == 1 && intval($_GET['filter_manufacturer']) == 1648 ) {
         echo 'Hi! This is something cool?';
         }
   ?>

The intval() bit should be enough to prevent injects.

If you wanted to find a part of the uri you could use something like:

  if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && is_product_attribute('somethingcool') ){
  echo 'Hi! This is something cool!';
  }

Check to see what echo is_product_attribute('Everlast'); as per your example returns.

$_GET... gets the variables in the search string by their names, which go in the square brackets.

      <?php if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1694 && is_product_attribute('Everlast') ){
             echo 'Hi! This is something cool!';  
             } ?>

Try it with just one item at a time and build up to get the hang of it.

Loads about finding strings in strings How do I check if a string contains a specific word in PHP?

Other ways to get and echo a product attribute if need be: Woocommerce getting custom attributes

I am not sure if this would work, but it might be adaptable enough.

You could pass the filter value $ff the filter integer value you want $ffv (might be different from 1) $fm is the $filter_manufacturer integer value $fmv - is the value you are looking for it to be - is as your example 1648 then the product array $product, your $collection variable and $aiw is the "Attribute I want" in text form but you might also pass it in a variable.

Put this function in your functions.php

 function attribute_i_want( $ff,$ffv, $fm, $fmv, $prod, $coll, $aiw){

      if( $ff == 1 && $fm == $fmv && $collection = $prod->get_attribute($aiw) ){
           echo '<div class="attribute-titles">. $aiw . ' name: ';
           echo ($coll = $prod->get_attribute($aiw) ) ? $collection : __('', 'woocommerce'); echo '</div>';
      }
 }
 add_action( 'wp_enqueue_scripts', 'attribute_i_want' );

And call it with this little lot passed to it

 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), 1648, $product, $coll, 'Certainteed'); ?>

I have assumed that the code you posted is working as it stands.

Looking at your page I can't see how it is getting the value from the dropdown list as it has no name or ID - ideally in the onchange event onchange="setManufacturer()"; - which I also can't find but believe it to be an event listner script - you would use something like this to set a hidden variable which gets the dropdown text value and uses that for the slot in the function call where you would currently have to enter the text manually, like 'Certainteed' in the example:

    <script language=JavaScript>
    function setManufacturer(){
    var manufacturerName = manufacturerDropdown.options[manufacturerName.selectedIndex].innerHTML;
    documentGetElementById('submittedManufacturerName').value = manufacturerName; 
    }
    </script>

This would get the text of what is selected from the select dropdown - which would need the ID "manufacturerName" and insert that text value into the hidden input's value, which PHP would be able to pick up and use in the PHP function call.

The onchange event would trigger the JavaScript and submit the page, the php would pick up the text value from the hidden input and that is what would be put into the function call:

  <input type="hidden" id ="submittedManufacturerName" name="submittedManufacturerName" value = "" />
 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), $manufacturerName, $product, $coll, $submittedManufacturerName); ?>

Unless there is any other way you can get at the value for the manufacturer name in a PHP variable - which may be possible as the manufacturer name does appear elsewhere on the page in the link above your dropdown, so it may already exist as a PHP variable which you could use as it is. That way your function would be fully automatic without the need for this additional JavaScript. The value appears in the a href called "Remove filter" on your existing page.

$manufacturerName would be the submitted value from the dropdown's actual value collected as $manufacturerName = htmlspecialchars($_GET['manufacturerDropdown']);

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14
  • Tried this and it broke the page Hi! This is something cool?'; } ?> Tried this And nothing rendered. Is my syntax correct? Is_product_attribute an allowed tag? – Tom Feb 06 '16 at 03:01
  • I can't even get the filtering part to work. Any ideas? – Tom Feb 06 '16 at 20:59
  • 1
    this actually worked. I ended up doing something like this get_attribute('Certainteed') ){ echo '
    Certainteed name: '; echo ($collection = $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '
    '; } ?>
    – Tom Feb 07 '16 at 18:45
  • Hi Tom Well done! you will be a PHP ace before long! You should post that as an answer too as it has useful additional bits. – Steve Feb 07 '16 at 19:18
1

Steve hooked it up and helped me figure out how to write conditional based on filter, which fixed my issue. Thanks Steve!

<?php if( intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1648 && $collection == $product->get_attribute('Certainteed') ){ echo '<div class="attribute-titles">Certainteed name: '; echo ($collection == $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '</div>'; } ?> 
Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
Tom
  • 459
  • 2
  • 4
  • 17
  • Is `is_product_attribute('` pseudocode or was it real? I couldn't find any references to it. If it doesn't exist you might make a little function and put it into your child theme's `functions.php` so, for example, is_product_attribute('Certainteed')` would `return $product->get_attribute('Certainteed');` – Steve Feb 08 '16 at 19:01
  • That was just something I made up as an example. I don't have any custom functions to allow for that. It would be great if I did – Tom Feb 08 '16 at 22:54
  • Hi Tom - curious to know if the function offered did anything.Please let me know if you want any help trying to implement it. – Steve Feb 13 '16 at 22:19