代码之家  ›  专栏  ›  技术社区  ›  JapeNZ

如何将WooCommerce分页和WooCommerce订购下拉列表添加到自定义短代码中

  •  0
  • JapeNZ  · 技术社区  · 6 年前

    我已经创建了一个自定义的短代码来显示具有最小库存量的产品,并希望在结果中添加分页,并调用将在页面上显示的WooCommerce订购下拉列表。

    以下是短代码:

    // Minimum Stock Shortcode
    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>';
    }
    

    任何帮助都将非常感谢!

    亲切的问候, 日本药典

    1 回复  |  直到 6 年前
        1
  •  0
  •   JapeNZ    6 年前

    好的,我已经开始分页并整理了一些东西(它在调试日志中抛出了一些错误),代码现在看起来如下:

    // Minimum Stock Shortcode
    add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
    
    function minimum_stock_shortcode( $atts ) {
    
    global $product, $woocommerce, $woocommerce_loop;
    
    // Attributes 
            $atts = shortcode_atts(
                array(
                'limit'                 => '40',
                'columns'               => '5',
                'orderby'               => 'title',
                'order'                 => 'asc',
                'category'              => '',
                'cat_operator'          => 'IN',
                'stock'                 => '',
                ),
                $atts, 'minimum_stock'
            );
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                'post_type'             => 'product',
                'post_status'           => 'publish',
                'ignore_sticky_posts'   => 1,
                'posts_per_page'        => $atts['limit'],
                'orderby'               => $atts['orderby'], 
                'order'                 => $atts['order'], 
                'paged'                 => $paged, 
                'meta_query'            => array(
                    array(
                        'key'           => '_stock',
                        'value'         => $atts['stock'],
                        'compare'       => '>='
                    )
                ),
                'tax_query'             => array(
                    array(
                        'taxonomy'      => 'product_cat',
                        'field'         => 'slug',
                        'terms'         => $atts['category'],
                    )   
                )
            );
    
            if ( isset( $ordering_args['meta_key'] ) ) { 
    $args['meta_key'] = $ordering_args['meta_key']; 
    }
    
    
    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 wc_get_template_part( 'content', 'product' ); ?>
    
            <?php endwhile; // end of the loop. ?>
    
        <?php woocommerce_product_loop_end(); ?>
    
    <?php endif;
    
    if($products->max_num_pages>1){
    ?>
    
    <nav class="woocommerce-pagination"> 
    
    <?php echo paginate_links( apply_filters(
            'woocommerce_pagination_args', array( 
                'base'      => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ), 
                'format'    => '', 
                'current'   => max( 1, get_query_var( 'paged' ) ), 
                'total'     => $products->max_num_pages, 
                'prev_text' => '&larr;', 
                'next_text' => '&rarr;', 
                'type'      => 'list', 
                'end_size'  => 3, 
                'mid_size'  => 3 
            )
        )       
    );
    
    ?>
    
    </nav>
    
    <?php }
    
    woocommerce_reset_loop(); 
    wp_reset_postdata();
    
    $return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
    
    // Remove ordering query arguments 
    WC()->query->remove_ordering_args();
    
    return $return; 
    }
    

    有人知道我现在怎么称呼WooCommerce订购下拉列表吗?

    我已经尝试添加:

    <?php do_action( 'woocommerce_before_shop_loop' ); ?>
    

    但这似乎不起作用,当我检查页面时,它确实有“WooCommerce通知包装纸”,但没有迹象表明“WooCommerce目录订购”,我认为也应与“WooCommerce”一起在“Shop-Loop”之前调用。

    我们将非常感谢您的任何帮助:)

    更新:

    我发现这个问题 Adding 'sort by' drop down on custom page using woocommerce short code 这给了我一个答案,我需要添加WooCommerce的订购下拉列表。

    这是我更新的代码:

    // Minimum Stock Shortcode
    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'               => 'date',
                'order'                 => 'desc',
                'category'              => '',
                'cat_operator'          => 'IN',
                'stock'                 => '',
                ), $atts );
    
        if ( ! $atts['category'] ) {
            return '';
        }
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    // Default ordering args
    $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'],    
    $atts['order'] );
    $orderby = 'date';
    $order = 'desc';
    if ( isset( $_GET['orderby'] ) ) {
        $getorderby = $_GET['orderby'];
    }
    if ($getorderby == 'date') {
        $orderby = 'date';
        $order = 'desc';
    } elseif ($getorderby == 'sku_desc') {
        $orderby = 'meta_value';
        $order = 'desc';
        $meta_key = '_sku';
    } elseif ($getorderby == 'sku_asc') {
        $orderby = 'meta_value';
        $order = 'asc';
        $meta_key = '_sku';
    }
    
            $args = array(
                'post_type'             =>  array( 'product', 'product_variation' ),
                'post_status'           => 'publish',
                'ignore_sticky_posts'   => 1,
                'posts_per_page'        => $atts['limit'],
                'orderby'               => $orderby, // $ordering_args['orderby'],
                'order'                 => $order, // $ordering_args['order'],
                'paged'                 => $paged, 
                'meta_query'            => array(
                    array(
                        'key'           => '_stock',
                        'value'         => $atts['stock'],
                        'compare'       => '>='
                    )
                ),
                'tax_query'             => array(
                    array(
                        'taxonomy'      => 'product_cat',
                        'field'         => 'slug',
                        'terms'         => $atts['category'],
                    )   
                )
            );
    
            if ( isset( $ordering_args['meta_key'] ) ) { 
                $args['meta_key'] = $ordering_args['meta_key']; 
    }
    
    
    ob_start();
    
    $products = new WP_Query( $args );
    
    $woocommerce_loop['columns'] = $atts['columns'];
    
    if ( $products->have_posts() ) : ?>
    
    <div style="width:100%;">
        <div style="float:right">
            <form class="woocommerce-ordering" method="get">
                <select name="orderby" class="orderby">
                    <?php
                        $catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
                            'date'      => __( 'Sort by latest', 'woocommerce' ),
                            'sku_asc'   => __( 'A-Z / Low to High Numbers', 'woocommerce' ),
                            'sku_desc'  => __( 'Z-A / High to Low Numbers', 'woocommerce' )
                        ) );
    
                        foreach ( $catalog_orderby as $id => $name )
                            echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
                    ?>
                </select>
                <?php
                    // Keep query string vars intact
                    foreach ( $_GET as $key => $val ) {
                        if ( 'orderby' === $key || 'submit' === $key )
                            continue;
    
                        if ( is_array( $val ) ) {
                            foreach( $val as $innerVal ) {
                                echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
                            }
    
                        } else {
                            echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
                        }
                    }
                ?>
            </form>
        </div>
    </div>
    <div style="clear:both;"></div>
    
        <?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 endif;
    
    if($products->max_num_pages>1){
    ?>
    
    <nav class="woocommerce-pagination"> 
    
    <?php echo paginate_links( apply_filters(
            'woocommerce_pagination_args', array( 
                'base'      => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ), 
                'format'    => '', 
                'current'   => max( 1, get_query_var( 'paged' ) ), 
                'total'     => $products->max_num_pages, 
                'prev_text' => '&larr;', 
                'next_text' => '&rarr;', 
                'type'      => 'list', 
                'end_size'  => 3, 
                'mid_size'  => 3 
            )
        )       
    );
    
    ?>
    
    </nav>
    
    <?php }
    
    woocommerce_reset_loop(); 
    wp_reset_postdata();
    
    $return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
    
    // Remove ordering query arguments 
    WC()->query->remove_ordering_args();
    
    return $return; 
    }
    

    我希望有人会觉得这有帮助:)

    亲切的问候, 日本药典