【问题标题】:Custom shortcode displaying a loop of WooCommerce product variations显示 WooCommerce 产品变体循环的自定义简码
【发布时间】:2021-04-18 10:01:55
【问题描述】:

我正在尝试制作自定义简码,以显示一些没有库存数量的产品变体stock <= 0

这是我到目前为止所做的:

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

WP Query 部分似乎运行良好,但显示产品的代码部分似乎错误。我觉得$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>';     

欢迎任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce shortcode product-variations


    【解决方案1】:

    您的代码中有一些错误和遗漏的东西,请改用以下重新访问的代码:

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

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    用法: [preordable_products] 或 PHP 代码 echo do_shortcode('[preordable_products]');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2019-04-26
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多