Skip to content

Instantly share code, notes, and snippets.

@kreamweb
Created September 23, 2020 11:13
Show Gist options
  • Save kreamweb/6c81d1a1deba40a3fb5afff6f65876b8 to your computer and use it in GitHub Desktop.
Save kreamweb/6c81d1a1deba40a3fb5afff6f65876b8 to your computer and use it in GitHub Desktop.
<?php
add_action( 'woocommerce_applied_coupon', 'change_subscription_next_payment_due_date' );
add_action( 'woocommerce_removed_coupon', 'change_subscription_next_payment_due_date_on_remove_coupon' );
function change_subscription_next_payment_due_date( $coupon_code ) {
if ( 'famille' === strtolower( $coupon_code ) ) {
replace_recurring_price( $coupon_code, 'add');
}
}
function change_subscription_next_payment_due_date_on_remove_coupon( $coupon_code ) {
if ( 'famille' === strtolower( $coupon_code ) ) {
replace_recurring_price( $coupon_code, 'remove' );
}
}
function replace_recurring_price( $coupon_code, $what = 'add' ) {
if ( did_action( 'wp_loaded' ) && isset( WC()->cart ) ) {
$contents = WC()->cart->get_cart();
$coupon = new WC_Coupon( $coupon_code );
$amount = $coupon->get_amount();
if ( ! empty( $contents ) ) {
foreach ( $contents as $item_key => $item ) {
$product = $item['data'];
error_log( print_r( $product , 1) );
if ( ywsbs_is_subscription_product( $product ) ) {
$subscription_info = $item['ywsbs-subscription-info'];
if ( $subscription_info ) {
$price = $what === 'add' ? $product->get_price() + $amount : $product->get_price() ;
WC()->cart->cart_contents[ $item_key ]['data']->set_price( $price );
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment