if statement - How do you write conditional PHP based on WooCommerce attribute -
i've search interwebs high , low looking way conditionally display woocommerce product attribute data on pages.
ex: if attribute somethingcool has value , it's on /?filtering=1&filter_manufacturer=1648 page, display value
i'd display attribute differently if on specific filtered page
ex: http://www.colorselectcoil.com/color-match-tool/?filtering=1&filter_manufacturer=1648
based on filtered page, display product attribute 'somethingcool'
<?php if ( is_product_attribute('somethingcool') ) { echo 'hi! cool?';} else { echo ''; } ?>
if normal wordpress page, not filtered page hide , show based on body class tag unfortunately body class doesn't change based on query string urls.
you use url search string , extract whatever part wanted. example, if wanted know if on filtered page wouldn't bother checking manufacturer.
<?php if ( $product->get_attribute('certainteed') && intval($_get['filtered']) == 1 && intval($_get['filter_manufacturer']) == 1648 ) { echo 'hi! cool?'; } ?>
the intval()
bit should enough prevent injects.
if wanted find part of uri use like:
if( stristr( htmlspecialchars($_server['request_uri']), 'color-match-tool') && is_product_attribute('somethingcool') ){ echo 'hi! cool!'; }
check see echo is_product_attribute('everlast');
per example returns.
$_get...
gets variables in search string names, go in 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! cool!'; } ?>
try 1 item @ time , build hang of it.
loads finding strings in strings how check if string contains specific word in php?
other ways , echo product attribute if need be: woocommerce getting custom attributes
i not sure if work, might adaptable enough.
you pass filter value $ff
filter integer value want $ffv
(might different 1) $fm
$filter_manufacturer
integer value $fmv
- value looking - example 1648 product array $product
, $collection
variable , $aiw
"attribute want" in text form might pass in variable.
put function in 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 little lot passed it
<?php attribute_i_want( intval($_get['filtering']), 1, intval($_get['filter_manufacturer']), 1648, $product, $coll, 'certainteed'); ?>
i have assumed code posted working stands.
looking @ page can't see how getting value dropdown list has no name or id - ideally in onchange event onchange="setmanufacturer()";
- can't find believe event listner script - use set hidden variable gets dropdown text value , uses slot in function call have enter text manually, 'certainteed' in example:
<script language=javascript> function setmanufacturer(){ var manufacturername = manufacturerdropdown.options[manufacturername.selectedindex].innerhtml; documentgetelementbyid('submittedmanufacturername').value = manufacturername; } </script>
this text of selected select dropdown - need id "manufacturername" , insert text value hidden input's value, php able pick , use in php function call.
the onchange event trigger javascript , submit page, php pick text value hidden input , put 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 other way can @ value manufacturer name in php variable - may possible manufacturer name appear elsewhere on page in link above dropdown, may exist php variable use is. way function automatic without need additional javascript. value appears in a href
called "remove filter" on existing page.
$manufacturername
submitted value dropdown's actual value collected $manufacturername = htmlspecialchars($_get['manufacturerdropdown']);
Comments
Post a Comment