WordPress.org

Support

Support » Plugins and Hacks » [Plugin: WooCommerce – excelling eCommerce] Featured Products by Category?

[Plugin: WooCommerce – excelling eCommerce] Featured Products by Category?

  • Hi. I’m trying to modify the widget-featured_products.php file to only show the featured products for the current pages category (i only turn the plugin featured products on my category pages)

    Here is my modified code for this PHP file:

    $title = apply_filters('widget_title', empty($instance['title']) ? __('Featured Products', 'woocommerce') : $instance['title'], $instance, $this->id_base);
    		if ( !$number = (int) $instance['number'] )
    			$number = 10;
    		else if ( $number < 1 )
    			$number = 1;
    		else if ( $number > 15 )
    			$number = 15;
    ?>
    
       		<?php
    		$taxonomy = get_taxonomy (get_query_var('taxonomy'));
    		$category_object = get_term_by('slug', get_query_var('term'), 'product_cat');
    		$catname = get_query_var('term');
    		echo 'Cat: '.$catname;
    
    		$query_args = array('posts_per_page' => $number,
    							'no_found_rows' => 1,
    							'post_status' => 'publish',
    							'post_type' => 'product',
    							'tax_query', array(array(
    								'taxonomy' => 'product_cat',
    								'field' => 'slug',
    								'terms' => array($catname),
    								'operator' => 'IN'
    								))
    							);
    
    		$query_args['meta_query'] = array();
    
    		$query_args['meta_query'][] = array(
    			'key' => '_featured',
    			'value' => 'yes'
    		);
    
    	    $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
    	    $query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();
    
    		$r = new WP_Query($query_args);
    
    		if ($r->have_posts()) : ?>

    I cut out the un-neccesary parts. As you can see, I’m trying to limit the page to only show the featured products which have the same category slug (preferably should be id) of the page it’s on.

    The code above will show the featured products, but not for the page’s category. It will show all the products on any page it’s on. I need it to be specific.

    Can anyone assist me here? I feel like I am close, but I do not have a great understanding of WordPress PHP. Thanks

    http://wordpress.org/extend/plugins/woocommerce/

  • The topic ‘[Plugin: WooCommerce – excelling eCommerce] Featured Products by Category?’ is closed to new replies.