Woocommerce Products Offset Shortcode - php

I'm trying to create a custom Woocommerce products shortcode that allows me to offset a certain number of products when adding the shortcode to a page. I've been able to get the offset part working by looking at WooCommerce - Recent Products Offset and https://ericwijaya.com/load-woocommerce-loop-custom-arguments/
The problem I am having is that the Woocommerce products "category" attribute doesn't work with my new shortcode. My shorcode looks like this: [products_offset limit="8" category="fiction" offset="10" orderby="menu_order"]
Instead of displaying products using the specified category slug (in this case "fiction"), it displays all products across all categories.
This is the code I've added to my function.php:
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );`
Any help would be greatly appreciated!

Figured it out. I wasn't passing the category slug into the WP_Query. Added:
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
So code all together looks like this:
//Shortcode for Offset Products
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );

Related

Woocommerce product shortcode with offset attribute

I've worked with a code extracted from an old question (Woocommerce Products Offset Shortcode), so I created a new shortcode to display products with offset but I think the "orderby" and "column" parameters are not working properly.
//Shortcode for Offset Products
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );
I've tried a lot of things but nothing worked fine for me :/
I would appreciate any help or code improvements!

woocommerce shortcode with stock quantity

I would like a shortcode like the following example that shows the stock of the products:
[products limit="8" columns="4" category="pantalones" cat_operator="IN"]
I would need something like this:
[products limit="8" columns="4" category="pantalones" cat_operator="IN" showstock="yes"]
I found this code a few days ago, I found it here on stackoverflow, but now I don't remember the link, sorry.:
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?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">' . ob_get_clean() . '</div>';
}
This code works perfect, but not show stock in homepage
I think the solution is not to modify the shortcode, but to modify the template that it uses to visualize data, but I don't know how to do it, if someone knows where it is or how it's done, I deeply appreciate it
Very thanks for your time.
I found, solution, thanks, very thanks
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_show_stock_shop', 10 );
function bbloomer_show_stock_shop() {
global $product;
echo wc_get_stock_html( $product );
}

Get WooCommerce featured products with a custom shortcode

I tried to get array of featured products to use theme in my own plugin with jquery slider
I have made this function and get attrs from class-wc-shortcodes.php
but no results
add_shortcode('soqopslider', 'wps_soqopslider');
function wps_soqopslider() {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'featured_products' );
$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' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
// The Query
$the_query = new WP_Query( query_args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo "No featured products found :(";
}
return "<span style='background:green;color:white;' >nothing</span>";
}
what I must add or change to get it works
I use it now as shortcode in welcome page just for testing
There was some errors in your code. So I have made the necessary changes.
Also the Shortcode data has to be returned not echoed.
Here is the functional code:
add_shortcode('soqopslider', 'wps_soqopslider');
function wps_soqopslider( $atts) {
$atts = shortcode_atts(
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'soqopslider'
);
$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' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
// The Query
$the_query = new WP_Query( $query_args );
$html = '</ul>';
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$html .= '<li>' . get_the_title() . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
// Output
return $html . '</ul>';
} else {
return "No featured products found :(";
}
}
## BASIC USAGE: [soqopslider]
# ---- #
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and will output a list of feature products titles.

WooCommerce:Custom product template - Pagination isn't working

all products are displayed. "posts_per_page" is not working. I try to limit products to 12 by page, but it shows all products.
Looks like my code is fine, but it isn't working.
Whats wrong with my code?
Can someone enlighten me, please?
Here's my code:
<?php
$meta_query = array();
$meta_query[] = array('key' => '_visibility','value' => array('visible', 'catalog'),'compare' => 'IN');
$meta_query[] = array('key' => '_stock_status','value' => 'instock','compare' => '=');
if($min_price !='' && $max_price !=''){
$meta_query[] = array('key' => '_price','value' => array($min_price, $max_price),'compare' => 'BETWEEN','type' => 'NUMERIC');
}
if($orderbym != '')
{
$mkey = '_price';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'ignore_sticky_posts' => 1,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => -1,
'meta_query' => $meta_query,
'meta_key' => $mkey,
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'bundle',
),
$product_catar
),
);
global $woocommerce_loop;
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args));
$columns = '2';
$woocommerce_loop['columns'] = $columns;
ob_start();
if($products->have_posts()){
woocommerce_product_loop_start();
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
}else{
_e( 'No product matching your criteria.' );
}
woocommerce_reset_loop();
wp_reset_postdata();
echo '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
?>
What comes to my mind is that the filter is maybe overwriting the query args. See what you get by doing
var_dump(apply_filters( 'woocommerce_shortcode_products_query', $query_args)) ;
My guess is you'll get different query args

how to search product of woocommerce with product name or title or category

I have an used YITH WooCommerce AJAX Search plugin but it shows or search keyword from product content also but I want to search product with only the title or product name & category of that product. I have tried & Google but not getting any proper solution.
Please help me.
IN YITH WooCommerce AJAX Search plugin following code is used.
public function ajax_search_products() {
global $woocommerce;
$search_keyword = $_REQUEST['query'];
$ordering_args = $woocommerce->query->get_catalog_ordering_args( 'title', 'asc' );
$suggestions = array();
$args = array(
's' => apply_filters( 'yith_wcas_ajax_search_products_search_query', $search_keyword ),
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters( 'yith_wcas_ajax_search_products_posts_per_page', get_option( 'yith_wcas_posts_per_page' ) ),
'suppress_filters' => false,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'search', 'visible' ),
'compare' => 'LIKE'
)
)
);
if ( isset( $_REQUEST['product_cat'] ) ) {
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_REQUEST['product_cat']
) );
}
$products = get_posts( $args );
if ( !empty( $products ) ) {
foreach ( $products as $post ) {
$product = wc_get_product( $post );
$suggestions[] = apply_filters( 'yith_wcas_suggestion', array(
'id' => $product->id,
'value' => strip_tags($product->get_title()),
'url' => $product->get_permalink()
), $product );
}
}
else {
$suggestions[] = array(
'id' => - 1,
'value' => __( 'No results', 'yith-woocommerce-ajax-search' ),
'url' => '',
);
}
wp_reset_postdata();
$suggestions = array(
'suggestions' => $suggestions
);

Categories