在Woocommerce中覆盖Avada Catalog排序挂钩以恢复默认值 [英] Override Avada Catalog sorting hook back to default in Woocommerce

查看:89
本文介绍了在Woocommerce中覆盖Avada Catalog排序挂钩以恢复默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将以下代码添加到Avada子function.php文件中来修改Woocommerce排序选项以获取自定义选项:

I am trying to modify Woocommerce sorting option to get a customized one by adding the following code to Avada child function.php file:

    // add custom sorting option 
   add_filter( 'woocommerce_get_catalog_ordering_args', 
  'custom_woocommerce_get_catalog_ordering_args' );

  function custom_woocommerce_get_catalog_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( 
  $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', 
  get_option( 'woocommerce_default_catalog_orderby' ) );

  if ( 'random_list' == $orderby_value ) {
 $args['orderby'] = 'menu_order Date';
    $args['order'] = 'ASC';
    $args['meta_key'] = '';
    }

return $args;
}

add_filter( 'woocommerce_default_catalog_orderby_options', 
'custom_woocommerce_catalog_orderby' );
 add_filter( 'woocommerce_catalog_orderby', 
 'custom_woocommerce_catalog_orderby' );

 function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['random_list'] = 'Menu_order_date';
return $sortby;
}
//end custom ordering code 

如果我没有启用WooCommerce商店页面订购框,并且实际上我希望将订购框显示在我的商店页面和产品类别上,则此代码可以正常工作,因此我就此问题与Avada支持小组联系,结果证明了Avada的使用他们自己的WooCommerce商店页面订购盒挂钩,带有以下代码:

this code work fine if i didn't enable WooCommerce Shop Page Ordering Boxes and actually i wanted the ordering box to be displayed on my shop page and product category, so i contact Avada Support for this issue and it turn out Avada use thier own hook for WooCommerce Shop Page Ordering Boxes with the following code:

/**
 * Controls the actions adding the ordering boxes.
 *
 * @access public
 * @since 5.0.4
 * @param object $query The main query.
 * @return void
 */
public function product_ordering( $query ) {

    // We only want to affect the main query.
    if ( ! $query->is_main_query() || $query->is_search() ) {
        return;
    }

    if ( $query->get( 'page_id' ) ) {
        $page_id = absint( $query->get( 'page_id' ) );
    } else {
        $page_id = absint( Avada()->fusion_library->get_page_id() );
    }

    if ( wc_get_page_id( 'shop' ) === $page_id || $query->is_post_type_archive( 'product' ) || $query->is_tax( get_object_taxonomies( 'product' ) ) ) {

        if ( Avada()->settings->get( 'woocommerce_avada_ordering' ) ) {
            remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
            add_action( 'woocommerce_before_shop_loop', array( $this, 'catalog_ordering' ), 30 );

            add_action( 'woocommerce_get_catalog_ordering_args', array( $this, 'get_catalog_ordering_args' ), 20 );
        }
    }
}

Avada支持人员说我可以从此挂钩中删除Avada功能,并使用子主题添加自己的功能.

Avada Support said I can remove Avada function from this hook and add my own using child theme.

 add_action( 'woocommerce_get_catalog_ordering_args', array( $this, 
'get_catalog_ordering_args' ), 20 );

我到处搜寻了所有寻求帮助的人,我发现许多人都问了几乎相同的问题,但没有答案.

I searched all over the interent seeking help and I found many pepole asked about almost same question but without answer.

如果有人可以帮助删除avada功能,这将对我和其他正在寻找该功能的人非常有用.

if anyone could help removing avada function that will be very helpful for me and other pepole who's looking for that as well.

如何从woocommerce_get_catalog_ordering_args动作挂钩中删除Avada挂钩功能?

How I can remove Avada hooked function from woocommerce_get_catalog_ordering_args action hook?

经过几次测试,我发现了Avada团队添加的以下代码如下:

after several test i disocoverd the following code which added by Avada team as follow:

/**
 * Modified the ordering of products.
 *
 * @access public
 * @since 5.1.0
 */
public function catalog_ordering() {
    get_template_part( 'templates/wc-catalog-ordering' );
}

/**
 * Gets the catalogue ordering arguments.
 *
 * @access public
 * @since 5.1.0
 * @param array $args The arguments.
 * @return array
 */
function get_catalog_ordering_args( $args ) {
    global $woocommerce;
    $woo_default_catalog_orderby = get_option( 'woocommerce_default_catalog_orderby' );

    // Get the query args.
    if ( isset( $_SERVER['QUERY_STRING'] ) ) {
        parse_str( sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) ), $params );
    }

    // Get order by.
    $pob = ( ! empty( $params['product_orderby'] ) ) ? $params['product_orderby'] : $woo_default_catalog_orderby;

    // Get order.
    $po = 'asc';
    if ( isset( $params['product_order'] ) ) {
        // Dedicated ordering.
        $po = $params['product_order'];
    } else {
        // Get the correct default order.
        $po = 'asc';
        if ( 'date' === $pob || 'popularity' === $pob || 'rating' === $pob || 'price-desc' === $pob ) {
            $po = 'desc';
        }
    }

    // Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
    if ( 'default' !== $pob ) {
        if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
            WC()->query->remove_ordering_args();
        }
    }

    $orderby  = 'date';
    $order    = strtoupper( $po );
    $meta_key = '';

    switch ( $pob ) {
        case 'menu_order':
        case 'default':
            $orderby  = $args['orderby'];
            break;
        case 'date':
            $orderby  = 'date';
            break;
        case 'price':
        case 'price-desc':
            add_filter( 'posts_clauses', array( $this, 'order_by_price_post_clauses' ) );
            add_action( 'wp', array( $this, 'remove_ordering_args_filters' ) );
            break;
        case 'popularity':
            $meta_key = 'total_sales';
            add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
            add_action( 'wp', array( $this, 'remove_ordering_args_filters' ) );
            break;
        case 'rating':
            $meta_key = '_wc_average_rating';
            $orderby  = array(
                'meta_value_num' => strtoupper( $po ),
                'ID'             => 'ASC',
            );
            break;
        case 'name':
            $orderby  = 'title';
            break;
    }

    $args['orderby']  = $orderby;
    $args['order']    = $order;
    $args['meta_key'] = $meta_key;

    return $args;
}

如果我只是修改以下内容:

if i just modify the following:

    // Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
    if ( 'default' !== $pob ) {
        if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
            WC()->query->remove_ordering_args();
        }
    }

    $orderby  = 'date';
    $order    = strtoupper( $po );
    $meta_key = '';

    // Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
    if ( 'default' !== $pob ) {
        if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
            WC()->query->remove_ordering_args();
        }
    }

    $orderby  = 'menu_order Date';
    $order    = strtoupper( $po );
    $meta_key = '';

$ orderby ='menu_order日期';而不是$ orderby ='date';

$orderby = 'menu_order Date'; instead of $orderby = 'date';

这只是在强迫avada使用我自己的自定义顺序而不是日期.

it's simply am forcing the avada to use my own custom ordering instead of date.

但是当我修改class-avada-woocommerce.php

but that not really practical solution as i modified class-avada-woocommerce.php

有没有建议通过向avada子代添加一些代码来找到解决方案

is there any suggestion to do find solution by add some code to avada child them instead

推荐答案

更新2:

我认为,如果您添加的优先级高于Avada的主题挂钩,它将起作用(此处我将其设置为100),并且此功能中唯一要做的更改是$args['orderby'] = 'menu_order date';以获得类似的更改,已经完成了Avada的文件.

I think that if you add a highest priority than Avada's theme hook it will work (here I have set it to 100), and the only change to do in this function is $args['orderby'] = 'menu_order date'; to get the similar change you have done on Avada's files.

代码:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 100 );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $args['orderby'] = 'menu_order date';
    return $args;
}

此代码进入您的活动子主题的function.php文件中.

This code goes in function.php file of your active child theme.

应该可以

非常奇怪的是, 过滤器钩子 ,但不是操作钩子(因此,我更改了代码)…

The very strange thing is that woocommerce_get_catalog_ordering_args is a filter hook, but NOT an action hook (so I have changed my code)…

然后,Avada的主题代码有问题,您应将其报告给支持部门.

您只需要增加钩子函数,即可覆盖Avada的函数:

You just need to increase your hooked function, so it will override Avada's:

add_action('after_setup_theme', 'remove_avada_function' );
function remove_avada_function(){
    remove_action( 'woocommerce_before_shop_loop', 'catalog_ordering', 30 );
    add_action( 'woocommerce_before_shop_loop', 'custom_catalog_ordering', 30 );
}

这篇关于在Woocommerce中覆盖Avada Catalog排序挂钩以恢复默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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