duanraotun1674 2018-10-25 20:37 采纳率: 100%
浏览 35

更改add_filters数组中的函数

Deep within the WooCommerce Composite Products plugin, there is the below add_filters array:

        /**
     * Filter front-end params.
     *
     * @param  array  $params
     */
    $params = apply_filters( 'woocommerce_composite_front_end_params', array(
        'small_width_threshold'                 => 450,
        'full_width_threshold'                  => 450,
        'legacy_width_threshold'                => 450,
        'i18n_qty_string'                       => _x( ' × %s', 'qty string', 'woocommerce-composite-products' ),
        'i18n_price_string'                     => _x( ' – %s', 'price suffix', 'woocommerce-composite-products' ),
        'i18n_title_string'                     => sprintf( _x( '%1$s%2$s%3$s', 'title quantity price', 'woocommerce-composite-products' ), '%t', '%q', '%p' ),
        'i18n_selected_product_string'          => sprintf( _x( '%1$s%2$s', 'product title followed by details', 'woocommerce-composite-products' ), '%t', '%m' ),
        'i18n_free'                             => __( 'Free!', 'woocommerce' ),
        'i18n_total'                            => __( 'Total', 'woocommerce-composite-products' ) . ': ',
        'i18n_no_options'                       => __( 'No options available…', 'woocommerce-composite-products' ),
        'i18n_no_selection'                     => __( 'No selection', 'woocommerce-composite-products' ),
        'i18n_no_option'                        => _x( 'No %s', 'dropdown empty-value option: optional selection (%s replaced by component title)','woocommerce-composite-products' ),

etc, etc...

What I'm trying to do is a remove_filter on two of the functions (in this case, i18n_clear_selection and i18n_strikeout_price_string), and change the first variable of i18n_no_option.

I've tried doing remove_filter( 'woocommerce_composite_front_end_params', 'i18n_strikeout_price_string' ); which did nothing, and tried using the below snippet (from this StackOverflow page):

function change_composite_product_functions($arr) {
$arr = array(
        'i18n_stirkeout_price_string'           => __(),
        'i18n_clear_selection'                  => __(),
        'i18n_no_option'                        => _x( 'Blah blah this is a test', 'dropdown empty-value options: optional selection (%s replaced by component title)','woocommerce-composite-products' ),
    );
return $arr;
}
 add_filter( 'woocommerce_composite_front_end_params', 'change_composite_product_functions', 10, 1 );

But that just returned a white page - no errors or anything. Is there a way to isolate functions within an array and remove / change just those?

  • 写回答

1条回答 默认 最新

  • dream198731 2019-01-02 17:00
    关注

    Note that '$arr' variable will be populated with data that is being passed to the 'change_composite_product_functions' function.

    Your section of code is literally overriding it with your new array.

    You are looking to locate the array key/item and change/remove it. Quick untested/rough snippet below:

    function change_composite_product_functions($arr) {
    if (array_key_exists('i18n_stirkeout_price_string', $arr)) unset($arr['i18n_stirkeout_price_string']); // Remove
    if (array_key_exists('i18n_no_option', $arr)) $arr['i18n_no_option'] = 'Blah blah this is a test'; // Change
    return $arr;
    }
    add_filter( 'woocommerce_composite_front_end_params', 'change_composite_product_functions', 10, 1 );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大