-1

In woocommerce plugin file class-wc-booking-cart-manager.php there is this code

/**
     * 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;
    }

I want to change add_cart_item function's code into my child theme functions.php file. I want to know how to override this plugin function.

So I did this :

remove_all_filters('woocommerce_add_cart_item');
add_filter('woocommerce_add_cart_item', 'custom_add_cart_item');

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;
}

As you can see, I'm setting price to 2000.

But it does not work.. Thanks for your help !

2
  • it is possible that you are trying to remove the filter before it was set. Commented Jan 29, 2016 at 14:05
  • Here $cart_item is array OR object? Commented Jan 30, 2016 at 12:00

2 Answers 2

0

Here is the code for overriding price of product in cart

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
    }
}
0

You are using woocommerce_add_cart_item, Please use woocommerce_before_calculate_totals.

add_action( 'woocommerce_before_calculate_totals', 'custom_add_cart_item' );

function custom_add_cart_item( $cart_object ) {
    foreach ( $cart_object->cart_contents as $key => $value ) {
        if ( empty( $value['booking'] ) && empty( $value['booking']['_cost'] ) {
            $value['data']->price = 2000;
        }
    }
}

Make sure that this code is always executable. After adding this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.