Tom
Tom

Reputation: 459

How do you write conditional PHP based on WooCommerce attribute

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.

Upvotes: 3

Views: 1818

Answers (2)

Tom
Tom

Reputation: 459

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>'; } ?> 

Upvotes: 1

Steve
Steve

Reputation: 818

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']);

Upvotes: 3

Related Questions