I am implementing a woocommerce website. In here I am providing a search bar to search only the products.
<form action="<?php echo get_site_url() ?>" method="GET">
<div class="input-group">
<input type="search" name="s" class="form-control" placeholder="Search Products" required>
<input type="hidden" name="post_type" value="products" />
<div class="input-group-btn">
<button class="btn btn-default" type="submit">
<i class="fa fa-search" aria-hidden="true"></i>
</button>
</div>
</div>
</form>
The above search code result in single-product
page. In the single product page's sidebar I have a section that shows the product attributes
and attribute terms
.
The following code is for getting the available product sizes in the current page (product-single
)
// get available sizes for woocommerce sidebar
function getSizes(){
$sizes = array();
$codes = array();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo get_the_title();
$product = wc_get_product();
$attributes = $product -> get_attributes('size');
foreach ($attributes as $key => $attribute) {
foreach ($attribute['options'] as $key => $meta) {
$meta_array = get_term_meta($meta);
if($meta_array['label']){
$temp = array();
$term = get_term($meta);
foreach ($term as $key => $value) {
if($key == 'term_id'){
$temp['term_id'] = $value;
}
if($key == 'name'){
$temp['name'] = $value;
}
$temp['attribute'] = 'size';
}
foreach ($meta_array['label'] as $key => $value) {
if (!in_array($value, $codes)){
$temp['size'] = $value;
array_push($sizes,$temp);
array_push($codes,$value);
}
}
}
}
}
endwhile; endif; return $sizes;
}
This is the html
where I print them in HTML-DOM
,
<ul class="w-woo-inner-box">
<?php $sizes = getaSizes();
if(!empty($sizes)):
foreach ($sizes as $key => $size) { ?>
<li>
<label class="checkbox inline-block <?php echo checkurlactive($size['attribute'], $size['term_id']); ?>">
<a href="<?php echo generate_filter_url($size['attribute'], $size['term_id']) ?>">
<input type="checkbox" class="">
<label> </label>
<span><?php echo $size['size']; ?></span>
</a>
</label>
</li>
<?php }
else: ?>
<li class="no-val-found"><span>No sizes found!</span> </li>
<?php endif; ?>
</ul>
PROBLEM :
That was a huge description. Ok now let's look at the problem, from the above code, when I search a existing product name
it works fine. But when I search some values from other post types
There is an php error
on the page mentioning
Fatal error: Call to a member function get_attributes() on boolean in /..../functions.php on line 482
And I tried to debug it and I found that the following loop in getSizes()
returning the other post type values
,
if ( have_posts() ) :
while ( have_posts() ) : the_post();
What is the problem here? How can I avoid searching the other post type values
?
In your function code you need to check the post type before inside the loop like:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Check for "product" post type or continue
if( 'product' !== get_post_type() ) {
continue; // jump to next post
the_title();
// Get an instance of the WC_Product Object
$product = wc_get_product( get_the_id() );
This will avoid the error you get as
$product
will always be aWC_Product
Object.
Bonus addition:
Now the WC_Product
method get_attributes()
has one argument $context
that can have 2 values:
'view'
(the default value)'edit'
(to be used in backend)But NOT:
$attributes = $product -> get_attributes('size');
So if you want to get a specific attribute, you will use WC_Product
method get_attribute()
intead that will gives a coma separated string of term names like:
// Get an instance of the WC_Product Object
$product = wc_get_product( get_the_id() );
// Loop through 'size' product attribute
$size_values = $product->get_attribute('size');
// Convert to an array of term names
$term_names = (array) explode(', ', $size_values);
// The product attribute taxonomy (always start with "pa_" + the slug)
$taxonomy = 'pa_size';
// Loop through term names
foreach( $term_names as $term_name ) {
// Get the WP_Term object (if needed)
$term = get_term_by( 'name', $term_name, $taxonomy );
$term_id = $term->term_id;
$term_slug = $term->slug;
}