在woocommerce插件文件class-wc-booking-cart-manager.php
中有这段代码
/**
* Constructor
*/
public function __construct() {
add_filter( 'woocommerce_add_cart_item', array( $this, 'add_cart_item' ), 10, 1 );
}
/**
* Adjust the price of the booking product based on booking properties
*
* @param mixed $cart_item
* @return array cart item
*/
public function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['booking'] ) && ! empty( $cart_item['booking']['_cost'] ) ) {
$cart_item['data']->set_price( $cart_item['booking']['_cost'] );
}
return $cart_item;
}
我想将add_cart_item
函数的代码更改为我的子主题functions.php
文件
所以我这样做了:
function custom_add_cart_item($cart_item) {
if (empty( $cart_item['booking'] ) && empty( $cart_item['booking']['_cost'] ) ) {
$cart_item['data']->set_price(2000);
}
return $cart_item;
}
function setup_add_cart_item_filter(){
remove_filter( 'woocommerce_add_cart_item', array('WC_Booking_Cart_Manager', 'add_cart_item' ), 10, 1 );
add_filter('woocommerce_add_cart_item', 'custom_add_cart_item');
}
add_action( 'after_setup_theme', 'setup_add_cart_item_filter' );
但它不起作用。谢谢你的帮助!
答案 0 :(得分:1)
您可以致电remove_all_filters('woocommerce_add_cart_item');
删除现有的挂钩,然后在functions.php中调用add_filter( 'woocommerce_add_cart_item', 'your_new_add_cart_item' );
编辑:我错过了在主题主题之前加载了childtheme functions.php
的一点,所以直接在functions.php中运行remove_all_filters()
实际上是无用的......
我更新的答案是将这些调用包装在另一个函数中,并在主题设置短语后调用它们:
function setup_add_cart_item_filter(){
remove_all_filters('woocommerce_add_cart_item');
add_filter('woocommerce_add_cart_item', 'custom_add_cart_item');
}
add_action( 'after_setup_theme', 'setup_add_cart_item_filter' );