Add fee using woocommerce_add_cart_item filter
-
I’ve searched online to understand if that is possible or not, but couldn’t find anything mentioning that.
In my code, I have this:
add_filter('woocommerce_add_cart_item', [$this, 'add_cart_item'], 20, 1); public function add_cart_item($cart_item) { $product_id = $cart_item['data']->get_id(); $product_type = wc_get_product($product_id)->get_type(); $insurance = $cart_item['rental_data']['rental_days_and_costs']['insurance']; if (isset($cart_item['rental_data']['quote_id']) && !empty($cart_item['rental_data']['quote_id']) && $product_type === 'rental') { $cart_item['data']->set_price($cart_item['rental_data']['rental_days_and_costs']['cost']); } else { if (isset($cart_item['rental_data']['rental_days_and_costs']['cost']) && $product_type === 'rental') { $cart_item['data']->set_price($cart_item['rental_data']['rental_days_and_costs']['cost']); } // revert if (isset($cart_item['quantity']) && $product_type === 'rental') { $cart_item['quantity'] = isset($cart_item['rental_data']['quantity']) ? $cart_item['rental_data']['quantity'] : 1; } } return $cart_item; }
The “$insurance” variable is the one I want to add to “product data” as a fee. Can I do that with this filter?
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Add fee using woocommerce_add_cart_item filter’ is closed to new replies.