Woocommerce product shortcode with offset attribute - php

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!

Related

Woocommerce single product select field not able to perform ajax function

HI i am adding select product field for a single product i tried a code for multiple select but now i need it for single product select field.
here is the code for adding field which works fine.
woocommerce_wp_text_input( array(
'id' => "seed-bundle-seeds-0-seed",
'name' => $this->options_name."[seed-bundle][seeds][0][seed]",
'label' => __( 'Seed', $this->slug ),
'placeholder' => __( 'Search for a sseed…', $this->slug ),
'class' => "wc-product-search repeatable",
'wrapper_class' => "no-label",
'style' => "width: 100%;",
'type' => "hidden",
'custom_attributes' => array(
'data-action' => "woocommerce_json_search_seeds",
'data-multiple' => "false",
'data-val' => 0
)
) );
And This is the ajax function, this was working fine on an older version of woocommerce on php 5.6 which is why i am updating it.
Somehow its not working .. i saw on multiple select field it runs an ajax call of Type GET but in this case its not working.
Not adding the Multiple select field code because its different from that one.
Here is the AJAX Code
add_action( 'wp_ajax_woocommerce_json_search_seeds', array( $this, 'json_search_cs' ) );
/** =============================
*
* AJAX - Search for seed based products from a bundle
*
============================= */
/**
* Search for products and echo json
*
* #param string $x (default: '')
* #param string $post_types (default: array('product'))
*/
public function json_search_cs( $x = '', $post_types = array( 'product' ) ) {
ob_start();
check_ajax_referer( 'search-products', 'security' );
$term = (string) wc_clean( stripslashes( $_GET['term'] ) );
if ( empty( $term ) ) {
die();
}
$taxQuery = array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'seed',
)
);
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $term,
'fields' => 'ids',
'tax_query' => $taxQuery
);
if ( is_numeric( $term ) ) {
$args2 = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => array( 0, $term ),
'fields' => 'ids',
'tax_query' => $taxQuery
);
$args3 = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'post_parent' => $term,
'fields' => 'ids',
'tax_query' => $taxQuery
);
$args4 = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $term,
'compare' => 'LIKE'
)
),
'fields' => 'ids',
'tax_query' => $taxQuery
);
$posts = array_unique( array_merge( get_posts( $args ), get_posts( $args2 ), get_posts( $args3 ), get_posts( $args4 ) ) );
} else {
$args2 = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $term,
'compare' => 'LIKE'
)
),
'fields' => 'ids',
'tax_query' => $taxQuery
);
$posts = array_unique( array_merge( get_posts( $args ), get_posts( $args2 ) ) );
}
$found_products = array();
if ( $posts ) {
foreach ( $posts as $post ) {
$product = wc_get_product( $post );
$found_products[ $post ] = rawurldecode( $product->get_formatted_name() );
}
}
$found_products = apply_filters( 'woocommerce_json_search_found_products', $found_products );
wp_send_json( $found_products );
}
Do we need to run the call through jQuery function Or it should work with Custom Attribute Name Data-action which i used.
Thanks In advance.

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 );
}

Woocommerce Products Offset Shortcode

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' );

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

Categories