如何在自定义简码中添加woocommerce分页和woocommerce排序下拉菜单

时间:2019-01-24 23:43:43

标签: woocommerce pagination shortcode

我已经创建了一个自定义的简码,以显示具有最小库存量的产品,并希望在结果中添加分页并调用woocommerce-ordering下拉菜单以显示在页面上。

这是简码:

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

任何帮助将不胜感激!

亲切的问候, JP

1 个答案:

答案 0 :(得分:0)

好的,所以我已经进行了分页工作并整理了一些东西(它在调试日志中引发了一些错误),现在的代码如下所示:

// 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-notices-wrapper”,但是没有迹象表明“ woocommerce_catalog_ordering”的迹象,我认为也应该用“ woocommerce_before_shop_loop”来调用。 / p>

任何帮助都将不胜感激:)

更新:

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

这是我更新的代码:

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

我希望有人会对此有所帮助:)

亲切的问候, JP