I have this function:
function test_q($atts){
$args = shortcode_atts(array(
'post_type' => 'product',
'columns' => 4,
'posts_per_page' => 12,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'mugs' ),
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => array( 'football' ),
),
),
), $atts);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
} else {
echo __( 'No products found' );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode('testasd', 'test_q');
I want to pass dynamic parameters to 'mugs' and 'football' with the shortcode. I am filtering products by category and tag. This function is working fine but I want to pass those two parameters through shortcode to make it dynamic.
I need to call this shortcode each time with different 'mugs' and 'football' term. For example 't-shirts' and 'basketball'. How will i do that?
You can do it easily via extracting the shortcode and passing the values via $atts from the shortcode.
After your arguments , you can pass it like the following ,
if($args['tax_query']['taxonomy'] == 'product_cat'){
$args['tax_query']['terms'] = $atts['terms_cat'];
}
if($args['tax_query']['taxonomy'] == 'product_tag'){
$args['tax_query']['terms'] = $atts['terms_tag'];
}
Then add 2 parameters in the shortcode,
[testasd terms_cat='t-shirts' terms_tag='basketball']
Related
I have a custom taxonomy called Specialties for Custom post type Candidates.
And entered items in Specialties A, B
I have created shortcode with attributes for looping A and B separately
like [candidates show="a"] for A candidates
and [candidates show="b"] for B candidates
I need to create a loop condition if any cpt item has both A & B Selected
then It will show that item in both shortcodes.
Here is the code
function show_all_custom_post_shortcode($attr) {
ob_start();
$a = shortcode_atts( array(
'show' => 'a',
), $attr );
$args = array(
'post_type' => 'candidates',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'candidate_speciality',
'field' => 'slug',
'terms' => $a['show'],
),
)
);
$q = new WP_Query( $args );
while ( $q->have_posts() ) : $q->the_post();
$imgurl = wp_get_attachment_url( get_post_thumbnail_id($q->ID) );
include('candidates/candidates-loop.php');
endwhile;
wp_reset_query();
return ob_get_clean();
}
add_shortcode('all-cpt', 'show_all_custom_post_shortcode'); // [all-cpt show='a'], [all-cpt show='b']
As I know
[products limit=”4″ columns=”4″ on_sale=”true” ] will show all on sale products,
[products limit=”3″ columns=”3″ best_selling=”true” ] will show best selling products,
If I were not wrong, both of the shortcodes would call the content-product.php file to display the products.
My question is how can I make different product shortcodes call different php template files to show the related products instead of calling the same file that is content-product.php?
For example,
on sale shortcodes call content-product-1.php
best selling shortcodes call content-product-2.php
Thank you!
Create our own shortcode to display the specific products and our our own template as below,
for example, wc_get_template_part( 'content', 'product-test' );
add_shortcode( 'sale_products_test', 'sale_products' );
function sale_products( $atts ){
global $woocommerce_loop, $woocommerce;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '3',
'orderby' => 'title',
'category' => 'clearance',
'order' => 'asc'
), $atts ) );
// Get products on sale
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$args = array(
'posts_per_page'=> $per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => $meta_query,
'post__in' => $product_ids_on_sale,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
)),
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product-test' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return ob_get_clean();
}
Not Possible. This type of shortcode require to use wp_query and fetch data your requirement wise
I am trying to print all products of a brand on from category page.
I find the wp_query but I can't print the current brand name of the brand to show all products in this page.
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
For example, i need to get all product of brand name (XXX) that fount in category name (YYY)
This is answer after search :
$args = array(
'post_type' => array('post','product'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 82,
'field' => 'id'
),
array(
'taxonomy' => 'brand',
'terms' => 81,
'field' => 'id'
),
)
);
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
}
}
I am trying to make a page, (default woocommerce archive style), that it will show only the products that are in Sale. Here to mention, that i have only, variable products and not simple.
I tried to make my custom shortcode
global $woocommerce_loop;
$atts = shortcode_atts( array(
'per_page' => '-1',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
), $atts );
// Get products on sale
$product_ids_on_sale = wc_get_product_ids_on_sale();
$meta_query = WC()->query->get_meta_query();
$args = array(
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => $meta_query,
'post__in' => array_merge( array( 0 ), $product_ids_on_sale )
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$columns = absint( $atts['columns'] );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) :
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; // end of the loop.
woocommerce_product_loop_end();
endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
but as a result i get only 2 products.
Any help or ideas?
Why are you trying to create a new shortcode? Woocommerce has provided its own shortcode to show the products on sale in its own archival style :
[sale_products per_page="12"]
You can see the whole list here
I found out a temporary solution in this by creating a custom shortcode. I don't know why i don't get all the sale products with the default woocommerce shortcode.
This worked for me:
function variable_sale_products( $atts ) {
global $woocommerce, $product;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1
);
ob_start();
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
$id = get_the_ID();
$_product = wc_get_product( $id );
if($_product->is_on_sale()){
wc_get_template_part( 'content', 'product' );
}
endwhile;
woocommerce_product_loop_end();
}
wp_reset_postdata();
return '<div class="woocommerce columns-4">' . ob_get_clean() . '</div>';
}
add_shortcode( 'variation_sale_product', 'variable_sale_products' );
If you have any other suggestion i'd like to hear from you
Try to expand $args (this code adds sale simple and variable products):
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
I tried [sale_products per_page="12"] , but it was showing only only one product.
Below is the steps I did to show all the products on sale :
1- go to WooCommerce -> Status -> Tools tab and click the button next to generate lookup tables and give it some time to complete before checking again. If this didn't work so proceed with the next step.
2- Update the shortode to be [sale_products per_page="32" paginate="true" columns="4" orderby="random"]. Again if this didn't work implement the next step.
3- This is not logical but what I did is that I removed the "sale price" of the item which appears in the on sale page. After that all other items appeared !!
Hope this helps anybody
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;