WP Bakery woocommerce show product subcategory (FREE) - wordpress

I wanted to share this work with you since I did not find any solution on the internet, so I had to build mine.
The problem was that the client wanted to show the subcategories within the main category using a shortcode. Only the name of the subcategories, without thumbnail or number of products.

So create a file called "products-subcategory.php" in plugins/bezel-addons/vc/shortcodes/.
And I incorporated it in plugins/bezel-addons/vc/shortcodes.php
In my case my template is the Bezzel https://themeforest.net/item/bezel-creative-multipurpose-wordpress-theme/20014332
But I think you can implement it in anyone using Visual Composer or WP Bakery.
products-subcategory.php.
<?php
/* Product subcategory */
vc_map(
array(
'name' => 'Product subcategory',
'base' => 'bezel_products_subcategory',
'icon' => 'ti-align-left',
'description' => 'Product subcategories',
'category' => __( 'Bezel', 'bezel-addons'),
'params' => array(
array(
'type' => 'dropdown',
'param_name' => 'orderby',
'heading' => 'Order BY',
'value' => array(
'Name' => 'name',
'ID' => 'term_id'
),
),
array(
'type' => 'dropdown',
'param_name' => 'order',
'heading' => 'Order',
'value' => array(
'Upward' => 'ASC',
'Falling' => 'DESC'
),
),
array(
'type' => 'dropdown',
'param_name' => 'empty',
'heading' => 'Show empty subcategories',
'value' => array(
'Yes' => 0,
'No' => 1
),
)
)
)
);
add_shortcode( 'bezel_products_subcategory', 'bezel_products_subcategory' );
function bezel_products_subcategory( $atts ) {
global $wp_query;
extract( shortcode_atts( array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'order' => 'ASC',
'empty' => 0,
'hierarchical' => 1
), $atts ) );
$cat = get_queried_object();
$category_id = ($cat->parent) ? $cat->parent : $cat->term_id;
$args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty);
$categories = get_categories( $args2 );
$categories_cnt = count(get_categories( $args2 ));
$selcat[$cat->term_id] = 'current-cat';
if ($categories_cnt != 0){
$sub_cats = get_categories( $args2 );
if($sub_cats) {
$output = '<div class="vc_wp_categories wpb_content_element">';
$output .= '<div class="widget widget_categories">';
$output .= '<ul>';
foreach($sub_cats as $sub_category) {
$output .= '<li class="cat-item cat-item-'.$sub_category->term_id.' '.$selcat[$sub_category->term_id].'">'.$sub_category->cat_name.'</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</div>';
}
}
return $output;
}
?>

Related

Shortcode with multiple meta_key attributes

I added two custom fields: day (event_day) & month (event_month) (both of type radio) for CPT Events. Now i want to be able to get posts by meta_key day and month.
The shortcode works except the part with $meta_query.
Here is how shorŠµcode should look like :
[tribe_custom_events_list_mm_wed cat="Rodrigo" num="6" day="Monday" month="October"]
Bellow is the code responsible for the shortcode, added in functions.php
function tribe_custom_events_shortcode($atts, $content = null)
{
global $post;
extract(shortcode_atts(array(
'cat' => '',
'num' => '',
'order' => 'ASC',
'orderby' => 'post_date',
'taxonomy' => 'tribe_events_cat',
'field' => 'name',
'day' => '',
'month' => '',
), $atts));
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
$day = $day;
$month = $month;
$meta_query = array(
array(
'key' => 'event_day',
'value' => '$day',
'compare' => '='
),
array(
'key' => 'event_month',
'value' => '$month',
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query),
'meta_query' => array($meta_query),
);
$output = '';
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$output .= '<div class="tribe-mini-calendar-event event-0 first last">';
$output .= '<h4 class="tribe-events-title">' . get_the_title() . '</h4>';
$output .= '</div>';
}
echo '<pre>' , var_dump($meta_query) , '</pre>';
wp_reset_postdata();
return '<div>' . $output . '</br>' . '</div>';
}
add_shortcode('tribe_custom_events_list_mm_wed', 'tribe_custom_events_shortcode');
This should work for you. There were a few errors in your code... Noted in the comments below.
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
/* This is unnecessary since $day already = $day
$day = $day;
$month = $month;
*/
$meta_query = array(
array(
'key' => 'event_day',
'value' => $day, // Don't put quotes around variables
'compare' => '='
),
array(
'key' => 'event_month',
'value' => $month,
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => $tax_query, // This is already an array defined above
'meta_query' => $meta_query,
);

How do i show only 1 post from latest 10 post?

my english language is not good. sorry!
this code is for checkbox in advanced custom field plugin.
i want show only 1 post(randomly) from latest 10 post.
please help me. thanks
('posts_per_page' => 10) and ('numberposts' => 10) is not working.
<?php
$gallery = array(
"offset" => "0",
'showposts' => '1',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
// query
$qgallery = new WP_Query( $gallery );
?>
<?php if( $qgallery->have_posts() ): ?>
<?php while( $qgallery->have_posts() ) : $qgallery->the_post(); ?>
<div class="fromgallery">
<a href="0" class="frgall">
<span class="frgdesc"><?php the_title() ?></span>
</a></div>
<?php endwhile; ?><?php endif; ?>
Try this
$gallery = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
$qgallery = new \WP_Query( $gallery );
I only do change in your code. For randomly one post you need to use '_shuffle_and_pick' => 1 , 'posts_per_page' => 10 is from 10 post and 'order' => 'DESC' is for latest posts. for custom '_shuffle_and_pick' you need to add
add_filter( 'the_posts', function( $posts, \WP_Query $qgallery )
{
if( $pick = $qgallery->get( '_shuffle_and_pick' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
To show Random post , Please use below script:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
//check is post found
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
I hope it will help you :)

Woo-Commerce Product Query with Multiple Arguments

I am trying to Query Woo-Commerce products based on the following
Featured
Popular
Promotional
Free
I was working to show them in isotop so I have to get all products and give specifiq tag based on my query to make filter. I was confused if I can make 4 Query as I have 4 different arguments / parameter.
I made 4 different query and got result in 4 different array. But there have problems.
I had done 4 Queries and Save result on 4 array. So there same products containing. I have to filter all products and if same product in more than one category I have to input tag like features free . as Isotop works.
If I unique marge array it will not show products in multiple category. But same products can be in two categories or can be in all categories so it should add tag in duplicate product and clear duplicate products list so that in result there will be unique product with multi tag.
Any idea how to do that ?
Below is my code which providing all products.
<?php
foreach ($filter_attr as $item) {
if ($item == 'featured') {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'orderby' => 'date',
'order' => 'desc',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$featured_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'popular'){
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$popular_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($popular_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'promotional'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => 'product',
'meta_key' => '_sale_price',
'meta_value' => '0',
'meta_compare' => '>='
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$promotional_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($promotional_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'freebies'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$freebies_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($freebies_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
}
}
?>

Moving WordPress php code for ordering events by date and grouping by category into Timber Twig templates

I'm moving a standard WordPress theme into Twig templates using the Timber plugin.
My goal is to list a custom post type called cpt_shows (events) by date but have them listed and grouped by artist. For example:
Artist A
event - April 1
event - May 1
event - June 1
Artist B
event - April 1
event - May 1
event - June 1
Artist C
event - April 1
event - May 1
event - June 1
I had this working without using twig with the following code in my original template:
$today = current_time('Ymd');
$args = array(
'orderby' => 'post_title',
'category_name' => 'events',
'exclude' => 28
);
$cats = get_categories( $args );
foreach( $cats as $cat ) :
$args = array(
'post_type' => 'cpt_shows',
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'meta_key' => 'date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'category__in' => array( $cat->term_id ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
while( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?><br>
<?php
endwhile;
wp_reset_postdata();
}
endforeach;
What I can't wrap my head around is how to move this into my Twig template because i have templating in my logic code, specifically the 'category__in' => array( $cat->term_id ), is being set in the loop. I've tried things in Twig like
{% for cat in categories %}
{% for post in loopSetupinContext %}
without success. Is there a better way to do this intially? In shortL i have a solution for my output but i'm unsure how to move it into Timber/Twig.
Any help is greatly appreciated.
Here is an example of what I meant in the comments.
I did not test the code, so it could contain some syntax errors. I changed the Twig_SimpleFilter to Twig_SimpleFuction.
function add_to_twig($twig) {
/* this is where you can add your own fuctions to twig */
$twig->addExtension(new Twig_Extension_StringLoader());
$twig->addFunction(new Twig_SimpleFunction('events', 'events_listing'));
return $twig;
}
function events_listing() {
$today = current_time('Ymd');
$args = array(
'orderby' => 'post_title',
'category_name' => 'events',
'exclude' => 28
);
$cats = get_categories( $args );
//init the array
$data = array();
foreach( $cats as $cat ) {
$args = array(
'post_type' => 'cpt_shows',
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'meta_key' => 'date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page'=> -1,
'category__in' => array( $cat->term_id ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
//Prepare the array to keep track of the category
//And init an extra array for keeping the posts together
$data[$cat->term_id] = array(
'category' => array(
'url' => get_category_link( $cat->term_id ),
'title' => sprintf( __( "View all posts in %s" ), $cat->name),
'text' => $cat->name,
),
'posts' => array(),
);
while( $query->have_posts() ){
$query->the_post();
//append the post to the array
$data[$cat->term_id]['posts'][] = array(
'url' => the_permalink(),
'text' => the_title(),
);
}
wp_reset_postdata();
}
}
return $data;
}
twig
{% for event in events() %}
<h2>{{ event.category.text }}</h2>
{% for post in event.posts %}
{{ post.text }}<br />
{% endfor %}
{% endfor %}
Ok. Looking into Timber more I figured this out, however, I'm still not sure this is the best approach so please comment if not.
Instead of trying to put this in my php template page (archive.php), I made it a function and added a filter in functions.php.
function add_to_twig($twig) {
/* this is where you can add your own fuctions to twig */
$twig->addExtension(new Twig_Extension_StringLoader());
$twig->addFilter(new Twig_SimpleFilter('events', 'events_listing'));
return $twig;
}
function events_listing() {
$today = current_time('Ymd');
$args = array(
'orderby' => 'post_title',
'category_name' => 'events',
'exclude' => 28
);
$cats = get_categories( $args );
foreach( $cats as $cat ) :
$args = array(
'post_type' => 'cpt_shows',
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'meta_key' => 'date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'category__in' => array( $cat->term_id ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
while( $query->have_posts() ) : $query->the_post();
?>
<?php the_title(); ?><br>
<?php
endwhile;
wp_reset_postdata();
}
endforeach;
}
And then call it in my Twig template like this:
{{ post.content|events }}

How to get category listing of most popular products in woocommerce

How can I list out the top 5 most popular category(or category of most popular products) on my wordpress site home page.
I have used woocommerce plugin for products.
Thanks in advance for any suggestion or solution.
Since none of the answers is a solution to the author's question, here is what I came up with. This is a shortcode snippet that lists popular products by categories. By popular I mean most sold products ( as in total sales).
function bestselling_products_by_categories( $atts ){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cats' => '',
'tax' => 'product_cat',
'per_cat' => '5',
'columns' => '5',
'include_children' => false,
'title' => 'Popular Products',
'link_text' => 'See all',
), $atts));
if(empty($cats)){
$terms = get_terms( 'product_cat', array('hide_empty' => true, 'fields' => 'ids'));
$cats = implode(',', $terms);
}
$cats = explode(',', $cats);
if( empty($cats) )
return '';
ob_start();
foreach($cats as $cat){
// get the product category
$term = get_term( $cat, $tax);
// setup query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_cat,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $cat,
'include_children' => $include_children,
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN'
)
)
);
// set woocommerce columns
$woocommerce_loop['columns'] = $columns;
// query database
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php if ( shortcode_exists('title') ) : ?>
<?php echo do_shortcode('[title text="'. $title .'" link="' . get_term_link( $cat, 'product_cat' ) . '" link_text="' . $link_text . '"]'); ?>
<?php else : ?>
<?php echo '<h2>'. $title .'</h2>'; ?>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
}
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
} add_shortcode( 'custom_bestselling_product_by_categories', 'bestselling_products_by_categories' );
You can use this by calling it as:
<?php echo do_shortcode('[custom_bestselling_product_by_categories cats="' . $term->term_id . '"]'); ?>
This shortcode has some options:
cats : the category ID or comma-separated IDs to retrieve the products from.
tax : the taxonomy to get the products from, default is product_cat
per_cat : number of products to retrieve
columns : number of columns to display
include_children : if false only direct children of the category will be displayed, if true then children of children will be displayed
title : title to display
link_text : the link text linked to the store
Notice that this snippet assumes you have a shortcode named title and it takes a few other parameters such as link and link_text arguments. You can always change this according to your theme.
Hope it helps.
I recommend you to check this page.
http://docs.woothemes.com/document/woocommerce-shortcodes/
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => ''
)
[product_category category="appliances"]
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
)
[top_rated_products per_page="12"]
Or you can use this plugin : https://wordpress.org/plugins/sp-woocommerce-best-selling-products-by-category/
Popular might be in many cases like most viewing, top selling. So i listed products by top selling. This way you can get top selling products and by this you can get category listing.
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '10',
'columns' => '4',
'fields' => 'ids',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'meta_query' => WC()->query->get_meta_query()
);
$best_sell_products_query = query_posts($query_args);
return $best_sell_products_query;

Resources