【问题标题】:Woocommerce override plugin actionWoocommerce 覆盖插件操作
【发布时间】:2014-11-19 06:07:51
【问题描述】:

文件 email-order-items.php 有以下代码行:

echo "\n" . sprintf( __( 'Cost: %s', 'woocommerce' ), $order->get_formatted_line_subtotal( $item ) );

以下操作挂钩已添加到我正在使用的插件(Woocommerce 复合产品)中:

add_action( 'woocommerce_order_formatted_line_subtotal', array( $this, 'wc_cp_order_item_subtotal' ), 10, 3 );

我想重写函数 wc_cp_order_item_subtotal 以更改显示项目小计的方式。我尝试将以下内容添加到我的子主题functions.php,但它没有做任何事情。

remove_action( 'woocommerce_order_formatted_line_subtotal', 'wc_cp_order_item_subtotal', 10);
add_action( 'woocommerce_order_formatted_line_subtotal', 'child_wc_cp_order_item_subtotal', 10,  3);

function child_wc_cp_add_order_item_meta( $order_item_id, $cart_item_values, $cart_item_key = '' ) {
    return 'xxxxxxx';
}

任何帮助我完成这项工作的提示将不胜感激。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    Codex 中没有提及,但我通常从挂钩中调用 remove_action() 函数。

    此外,正如Codex example 中所涵盖的从类添加的操作,您需要访问类实例或变量。

    我在 Composite 插件的任何地方都没有看到wc_cp_order_item_subtotal,所以我认为您没有使用 Woo 的版本。在这种情况下,我无法访问代码,也无法准确告诉您如何访问类变量。

    但如果您使用的是 Woo 的复合产品,情况如下:

    针对 Composites 2.4 编辑

    function so_remove_action(){
        global $woocommerce_composite_products;
        remove_action( 'woocommerce_order_formatted_line_subtotal', array( $woocommerce_composite_products->order, 'wc_cp_order_item_subtotal' ), 10, 3 ); //not sure why it isn't a filter, but also not sure if there is a huge difference
    }
    add_action('init', 'so_remove_action');
    

    【讨论】:

    • 谢谢。我正在使用官方的 Woocommerce Composite Product 插件,但看起来在 2.4.0 版中他们重构了代码并删除了对 BTO 的引用。我如何计算出相当于 $woocommerce_bto->order->woocommerce_bto 的是什么?
    • 看起来所有的“bto”都变成了“cp”,“woo”变成了“wc”。所以整个插件的全局变量是$woocommerce_composite_products,而WC_CP_Order()类被初始化为主类的order变量,所以$woocommerce_composite_product->order。我不知道我从哪里得到额外的woocommerce_bto,可能复制/粘贴失败。
    猜你喜欢
    • 2013-02-26
    • 2022-10-14
    • 2016-12-28
    • 1970-01-01
    • 2015-11-16
    • 2018-06-03
    • 2015-01-06
    • 2018-02-16
    • 2014-12-04
    相关资源
    最近更新 更多