简体   繁体   English

Woocommerce Widget产品标题排序

[英]Woocommerce Widget Product order by title

My initial goal is to be able to display Woocommerce Products in the sidebar, sorted by Title . 我最初的目标是能够在侧栏中, 按标题排序显示Woocommerce产品。 Currently the only sorting options in this widget are Date, Price, Random and Sales. 当前,此小部件中唯一的排序选项是日期,价格,随机和销售。

I was able to add the Title sorting option in 2 parts in class-wc-widget-products.php : 我能够在class-wc-widget-products.php中的2部分中添加Title排序选项:

'orderby' => array(
                'type'  => 'select',
                'std'   => 'date',
                'label' => __( 'Order by', 'woocommerce' ),
                'options' => array(
                    'title'   => __( 'Title', 'woocommerce' ),
                    'date'   => __( 'Date', 'woocommerce' ),
                    'price'  => __( 'Price', 'woocommerce' ),
                    'rand'   => __( 'Random', 'woocommerce' ),
                    'sales'  => __( 'Sales', 'woocommerce' ),
                ),

And here: 和这里:

switch ( $orderby ) {
    case 'title' :
        $query_args['orderby']  = 'title';
        break;
    case 'price' :
        $query_args['meta_key'] = '_price';
        $query_args['orderby']  = 'meta_value_num';
        break;
    case 'rand' :
        $query_args['orderby']  = 'rand';
        break;
    case 'sales' :
        $query_args['meta_key'] = 'total_sales';
        $query_args['orderby']  = 'meta_value_num';
        break;
    default :
        $query_args['orderby']  = 'date';
}

This customization works fine, but: 此定制工作正常,但是:

My question: where should I save this customized " class-wc-widget-products.php " file preventing it from being overwritten at the next Woocommerce update? 我的问题:我应该在哪里保存此自定义的“ class-wc-widget-products.php ”文件,以防止它在下一次Woocommerce更新时被覆盖?
OR... is there a more elegant way to accomplish this? 或者...还有一种更优雅的方式来实现这一目标吗? Thank You! 谢谢!

Kindly, making what you have done is something really to avoid. 诚然, 真正避免的事情。
It's prohibited for developers (so I had to rename the title of your question) . 开发人员禁止使用它 (因此我不得不重命名您的问题的标题)

First inconvenient (as you already know): you loose your changes when plugin is updated. 首先带来的不便 (如您所知):更新插件时,您会丢失更改。

Nobody override core files… So please remove all your changes or get the original file back. 没有人会覆盖核心文件……因此,请删除所有更改或找回原始文件。

That is why actions and filters exist all through the code 这就是为什么整个代码中都存在动作和过滤器的原因

The correct way to force orderby by "title" can be done this way: 通过“ title”强制orderby的正确方法可以通过以下方式完成:

add_filter( 'woocommerce_products_widget_query_args','title_orderby_products_widget_query_arg', 10, 1 );
function title_orderby_products_widget_query_arg( $query_args ) {
    // set 'title' for orderby 
    $query_args['orderby'] = 'title';

    return $query_args;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This should work for you. 这应该为您工作。

Then you can add some conditions to better target this... 然后,您可以添加一些条件以更好地针对此目标...


Official documentation: WooCommerce Action and Filter Hook Reference 官方文档: WooCommerce操作和筛选器挂钩参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM