John Davidson

php - Custom shortcode displaying a loop of WooCommerce product variations

0 comments
Message:


Im trying to make a custom shortcode in order to display some product variations that have no stock quantity with stock <= 0.


Here's what I did so far:


if( ! function_exists('preorable_products') ) {

// Add Shortcode
function preorable_products( $atts ) {
global $woocommerce_loop;

// Attributes
$atts = shortcode_atts(
array(
'columns' => '4',
'limit' => '20',
'preordable' => "yes",
'stock' => 0,
),
$atts, 'preorable_products'
);


$woocommerce_loop['columns'] = $atts['columns'];

// The WP_Query
$products_variation = new WP_Query( array (
'post_type' => 'product_variation',
'post_status' => 'publish',
'fields' => 'id=>parent',
'posts_per_page' => $atts['limit'],
'meta_query' => array(
'relation' => 'AND',
'preordable' => array(
'key' =>'_ab_preorder_checkbox',
'value' => "yes",
'compare' => '=='
),
'stock' => array(
'key' =>'_stock',
'value' => 0,
'compare' => '<='
),
)
));
$products = $products_variation;
ob_start();

if ( $products->have_posts() ) { ?>

<?php woocommerce_product_loop_start(); ?>

<?php while ( $products->have_posts() ) : $products->the_post(); ?>

<?php wc_get_template_part( 'content', 'product' ); ?>

<?php endwhile; // end of the loop. ?>

<?php woocommerce_product_loop_end(); ?>

<?php
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>Aucun article disponible à la précommande.</p>";
}

woocommerce_reset_loop();
wp_reset_postdata();

return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
}

add_shortcode( 'preorable_products', 'preorable_products' );
}

The WP Query part seems to work perfectly, but the code part to display the product seems wrong. I feel like $product variable doesn't have have_post method :


if ( $products->have_posts() ) { ?>
<?php woocommerce_product_loop_start(); ?>

<?php while ( $products->have_posts() ) : $products->the_post(); ?>

<?php wc_get_template_part( 'content', 'product' ); ?>

<?php endwhile; // end of the loop. ?>

<?php woocommerce_product_loop_end(); ?>

<?php
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>Aucun article disponible à la précommande.</p>";
}

woocommerce_reset_loop();
wp_reset_postdata();

return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';

Any help is welcome.



My Answer:
It looks like the issue might be with the way you are setting the $products variable. Instead of setting it to $products_variation, you should set it to $products_variation->posts. This will give you an array of posts that you can loop through.

Here's the updated code:

php
if( ! function_exists('preorable_products') ) {

// Add Shortcode
function preorable_products( $atts ) {
global $woocommerce_loop;

// Attributes
$atts = shortcode_atts(
array(
'columns' => '4',
'limit' => '20',
'preordable' => "yes",
'stock' => 0,
),
$atts, 'preorable_products'
);


$woocommerce_loop['columns'] = $atts['columns'];

// The WP_Query
$products_variation = new WP_Query( array (
'post_type' => 'product_variation',
'post_status' => 'publish',
'fields' => 'id=>parent',
'posts_per_page' => $atts['limit'],
'meta_query' => array(
'relation' => 'AND',
'preordable' => array(
'key' =>'_ab_preorder_checkbox',
'value' => "yes",
'compare' => '=='
),
'stock' => array(
'key' =>'_stock',
'value' => 0,
'compare' => '<='
),
)
));
$products = $products_variation->posts;
ob_start();

if ( $products ) { ?>











} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "

Aucun article disponible à la précommande.

";
}

woocommerce_reset_loop();
wp_reset_postdata();

return '
' . ob_get_clean() . '
';
}

add_shortcode( 'preorable_products', 'preorable_products' );
}


This should loop through the array of posts and display the product variations correctly. Let me know if you have any other questions or issues!

Rate this post

5 of 5 based on 5045 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us