Reputation: 152
In WooCommerce I am changing the price of products in cart using the following hooked function:
add_action('woocommerce_before_calculate_totals', 'set_custom_price',1000,1);
function set_custom_price($cart_obj) {
foreach ($cart_obj->get_cart() as $key => $value) {
if($value['alredy_have_number'] == true) {
$value['data']->set_price(0.90);
}
}
}
it works fine for recurring price but I wants to change signup fee for subscription products. what function or hook can I use for that ?
Upvotes: 1
Views: 1006
Reputation: 253784
To change some cart item specific metadata like the subscription signup fee, you will use the WC_Data
method update_meta_data()
that can be used on any WC_Product
Object like Subscription products.
The related subscription signup fee meta_key
to be used is _subscription_sign_up_fee
so in your hooked function code, you will use it this way to change its price:
add_action( 'woocommerce_before_calculate_totals', 'change_subscription_data', 1000, 1 );
function change_subscription_data( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Check that product custom cart item data "alredy_have_number" exist and is true
if( isset($cart_item['alredy_have_number']) && $cart_item['alredy_have_number'] ) {
// Change subscription price
$cart_item['data']->set_price(10.60);
// Only for subscription products
if ( in_array( $cart_item['data']->get_type(), ['subscription', 'subscription_variation']) ) {
// Change subscription Sign up fee
$cart_item['data']->update_meta_data('_subscription_sign_up_fee', 3.40);
}
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Upvotes: 2