Woocommerce,基于短码产品列表的排序下拉列表 [英] Woocommerce, sort dropdown on shortcode based product lists

查看:55
本文介绍了Woocommerce,基于短码产品列表的排序下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的商店中,我们有许多标准的WP页面.在这些页面上,我们显示了约40种使用标准Woocommerce简码的产品.

In our shop, we have a number of standard WP pages. On these pages we show ~40 products using the standard Woocommerce shortcodes.

例如:

[product_category category="boots" per_page="20" columns="4" orderby="price" order="desc"]

产品出现了,但是缺少两件事:

The products appears, but there are two things missing:

  1. 在产品列表上方没有排序下拉列表,因此我们的访客无法对产品进行排序.
  2. 我们看不到任何分页按钮,因此每页上看到的前20个产品都不多.

有什么想法可以解决这两件事吗?

Any ideas how we can fix those two things?

推荐答案

关于您的第一个问题,除了破解WC中的短代码外,我没有找到任何好的解决方案.不完全建议,因为它将在WC的每次升级/修补程序中覆盖.如果绝对必要,您可以在每次升级时通过重写hack来维护代码.

Regarding your first issue, I have not found any good solutions beside hacking the shortcode within WC. Not entirely advisable as it will be overwritten each upgrade/patch of WC. If it's absolutely necessary, you could maintain the code by rewriting the hack each time you upgrade.

好的,首先在woocommerce文件夹中下载 includes \ class-wc-shortcodes.php 的副本.在编辑原始文件之前先对其进行备份,我通常使用-o重命名文件或将文件类型更改为.bak.

Alright, first download a copy of includes\class-wc-shortcodes.php in your woocommerce folder. Make a backup of the original before editing it, I usually rename the file with a -o or change the file type to .bak.

假设您想要购物页面上WC随附的下拉菜单进行原始排序.

Assuming you'd want the original sort by dropdown that comes with WC on the Shop Page.

第1步 删除简码上的orderby和order参数:

Step 1 Remove orderby and order arguments on the shortcode:

[product_category category="boots" per_page="20" columns="4"]

第2步 class-wc-shortcodes.php 上编辑产品类别的简码,如下所示:

Step 2 Edit the Product Category shortcode on class-wc-shortcodes.php as such:

/**
 * List products in a category shortcode
 *
 * @access public
 * @param array $atts
 * @return string
 */
public static function product_category( $atts ) {
    global $woocommerce_loop, $wpdb;

    if ( empty( $atts ) ) return '';

    extract( shortcode_atts( array(
        'per_page'      => '12',
        'columns'       => '4',
        'orderby'       => 'title',
        'order'         => 'asc',
        'category'      => '',
        'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        ), $atts ) );

    if ( ! $category ) return '';

    // Default ordering args
    // $ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order ); // COMMENT THIS OUT
    $orderby = 'title';
    $order = 'asc';
    if ( isset( $_GET['orderby'] ) ) {
        $getorderby = $_GET['orderby'];
    }
    if ($getorderby == 'popularity') {
        $orderby = 'meta_value_num';
        $order = 'desc';
        $meta_key = 'total_sales';
    } elseif ($getorderby == 'rating') {

        $fields .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";

        $where .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";

        $join .= "
            LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
            LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
        ";

        $orderby = "average_rating DESC, $wpdb->posts.post_date DESC";

        $groupby = "$wpdb->posts.ID";   

    } elseif ($getorderby == 'date') {
        $orderby = 'date';
        $order = 'desc';
    } elseif ($getorderby == 'price') {
        $orderby = 'meta_value_num';
        $order = 'asc';
        $meta_key = '_price';
    } elseif ($getorderby == 'price-desc') {
        $orderby = 'meta_value_num';
        $order = 'desc';
        $meta_key = '_price';
    }
    $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'orderby'               => $orderby, // $ordering_args['orderby'],
        'order'                 => $order, // $ordering_args['order'],
        'meta_key'              => $meta_key,
        'fields'                => $fields,
        'where'                 => $where,
        'join'                  => $join,
        'groupby'               => $groupby,
        'posts_per_page'        => $per_page,
        'meta_query'            => array(
            array(
                'key'           => '_visibility',
                'value'         => array('catalog', 'visible'),
                'compare'       => 'IN'
            )
        ),
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'terms'         => array( esc_attr( $category ) ),
                'field'         => 'slug',
                'operator'      => $operator
            )
        )
    );

    if ( isset( $ordering_args['meta_key'] ) ) {
        $args['meta_key'] = $ordering_args['meta_key'];
    }

    ob_start();

    $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

    $woocommerce_loop['columns'] = $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(
                            'menu_order' => __( 'Default sorting', 'woocommerce' ),
                            'popularity' => __( 'Sort by popularity', 'woocommerce' ),
                            'rating'     => __( 'Sort by average rating', 'woocommerce' ),
                            'date'       => __( 'Sort by newness', 'woocommerce' ),
                            'price'      => __( 'Sort by price: low to high', 'woocommerce' ),
                            'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
                        ) );

                        if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
                            unset( $catalog_orderby['rating'] );

                        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;

    woocommerce_reset_loop();
    wp_reset_postdata();

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

将文件上传回include文件夹,就可以完成!您的简码产品页面现在将具有按下拉列表的有效排序方式,该下拉列表与WC商店页面上显示的页面相同.

Upload the file back into the includes folder and you're done! Your shortcode product pages will now have a working sort by dropdown same as the one that appears on the WC Shop Page.

根据您的喜好编辑排序选项!希望对您有帮助!

Edit the sort options to your liking! Hope it helps!

这篇关于Woocommerce,基于短码产品列表的排序下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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