简体   繁体   English

显示 WooCommerce 产品变体循环的自定义简码

[英]Custom shortcode displaying a loop of WooCommerce product variations

Im trying to make a custom shortcode in order to display some product variations that have no stock quantity with stock <= 0 .我试图制作一个自定义简码,以显示一些没有库存数量且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. WP Query 部分似乎运行良好,但显示产品的代码部分似乎错误。 I feel like $product variable doesn't have have_post method:我觉得$product变量没有have_post方法:

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.欢迎任何帮助。

There are some errors and missing things in your code, use instead the following revisited code:您的代码中有一些错误和遗漏的东西,请改用以下重新访问的代码:

if( ! function_exists('get_preordable_products') ) {

    function get_preordable_products( $atts ) {
        // Shortcode Attributes
        extract( shortcode_atts( array(
            'columns'    => '4',
            'limit'      => '20',
            'preordable' => "yes",
            'stock'      => 0,
        ), $atts, 'preordable_products' ) );

        // The WP_Query
        $query = new WP_Query( array (
            'post_type'         => 'product_variation',
            'post_status'       => 'publish',
            'posts_per_page'    => $limit,
            'meta_query'        => array(
                'relation'      => 'AND',
                'preordable'  => array(
                    'key'       =>'_ab_preorder_checkbox',
                    'value'     => "yes",
                    'compare'   => '=='
                ),
                array(
                    'key'       =>'_stock',
                    'value'     => 0,
                    'compare'   => '<='
                ),
            )
        ) );

        global $woocommerce_loop;

        $woocommerce_loop['columns']      = $columns;
        $woocommerce_loop['is_shortcode'] = 1;
        $woocommerce_loop['name']         = 'preordable_products';
        $woocommerce_loop['total']        = $query->post_count;
        $woocommerce_loop['total_pages']  = $query->max_num_pages;
        $woocommerce_loop['per_page']     = $limit;

        ob_start();

        if ( $query->have_posts() ) {
            woocommerce_product_loop_start();

            while ( $query->have_posts() ) {
                $query->the_post();
                wc_get_template_part( 'content', 'product' );
            }
            woocommerce_product_loop_end();
            woocommerce_reset_loop();
            wp_reset_postdata();
        } else {
            do_action( "woocommerce_shortcode_products_loop_no_results", $atts );

            echo '<p>'. __("Aucun article disponible à la précommande.") . '</p>';
        }

        $content = ob_get_clean();

        return '<div class="woocommerce columns-' . $columns . '">' . $content . '</div>';
    }

    add_shortcode( 'preordable_products', 'get_preordable_products' );
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

Usage: [preordable_products] or in Php code echo do_shortcode('[preordable_products]');用法: [preordable_products]或 Php 代码echo do_shortcode('[preordable_products]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 订单和电子邮件中显示产品变体的自定义字段 - Displaying custom field of product variations within Woocommerce orders and emails 自定义简码显示 WooCommerce 产品价格:显示零价格文本 - Custom Shortcode displaying WooCommerce product price: Display a text for zero price 自定义 Woocommerce 产品变体属性 - Custom Woocommerce product variations attributes Woocommerce - 显示循环内的变化 - Woocommerce - Displaying variations inside the loop WooCommerce 自定义产品循环短代码,带有可选的税务查询问题 - WooCommerce custom product loop shortcode with optional tax queries issue 检查 WooCommerce 产品(简单或变体)是否有库存并将标签显示为短代码 - Check if WooCommerce product (simple or variations) are in stock and display label as shortcode WooCommerce:在循环内显示产品变化 - WooCommerce: Display product variations inside of loop 按价格对 Woocommerce 产品变体自定义输出进行排序 - Sort a Woocommerce product variations custom output by their price WooCommerce:向产品变体添加自定义字段 - WooCommerce : add custom fields to product variations 在 WooCommerce 产品变体中显示 ACF 自定义字段 - Display ACF custom field in WooCommerce product variations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM