如何在自定义简码中添加woocommerce分页和woocommerce排序下拉菜单 [英] How to add woocommerce-pagination and woocommerce-ordering dropdown to custom shortcode

查看:87
本文介绍了如何在自定义简码中添加woocommerce分页和woocommerce排序下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的简码,以显示具有最低库存量的产品,并希望在结果中添加分页,并希望将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

解决方案

好的,所以我已经进行了分页并且整理了一些东西(它在调试日志中引发了一些错误),现在的代码看起来像这样:

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

任何人和所有帮助将不胜感激:)

更新:

我发现了这个问题添加使用woocommerce短代码在自定义页面上排序"下拉框,这给了我添加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

I've created a custom shortcode to display products with a minimum stock amount and would like to add pagination to the results as well as calling the woocommerce-ordering dropdown to be displayed on the page.

Here's the shortcode:

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

Any help would be very much appreciated!

Kind regards, JP

解决方案

Okay, so I've got pagination working and tidied things up a bit (it was throwing some errors in the debug log), the code now looks like this:

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

Does anyone know how I can now call the woocommerce-ordering dropdown?

I've tried adding:

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

But this doesn't seem to work, when I check the page it does have the 'woocommerce-notices-wrapper' but there's no sign of the 'woocommerce_catalog_ordering' that I thought should also be called with 'woocommerce_before_shop_loop'.

Any and all help will be very much appreciated :)

Update:

I found this question Adding 'sort by' drop down on custom page using woocommerce short code which gave me the answer I needed to add the woocommerce-ordering dropdown.

Here's my updated code:

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

I hope someone might find this helpful :)

Kind regards, JP

这篇关于如何在自定义简码中添加woocommerce分页和woocommerce排序下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆