Last active
December 10, 2022 05:08
-
-
Save kartikparmar/5e6de5d541fcfa9751078e63bb3386c7 to your computer and use it in GitHub Desktop.
Dave Changes - Ticket 46398
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function custom_change_cart_meta( $cart ) { | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) | |
return; | |
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) | |
return; | |
// Only for a DEFINED coupon code ( to be set below ) | |
$coupon_code = ''; | |
$discount_type = ''; | |
$coupon_amount = 1; | |
$is_coupon = 0; | |
$update_cart = false; | |
$cart_content = $cart->cart_contents; | |
if ( count( $cart->get_applied_coupons() ) > 0 ) { | |
$is_coupon = 1; | |
$coupon = $cart->get_applied_coupons(); | |
$coupon_code = new WC_Coupon($coupon[0]); //percent, fixed_product, fixed_cart | |
$discount_type = $coupon_code->get_discount_type(); | |
$coupon_amount = $coupon_code->get_amount(); | |
} | |
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) { | |
$booking_settings = get_post_meta( $cart_item['product_id'], 'woocommerce_booking_settings', true ); // Adjust price if addons are set | |
if ( isset( $cart_item['bkap_booking'] ) ) { | |
foreach ( $cart_item['bkap_booking'] as $key => $addon ) { | |
if ( isset( $booking_settings['booking_partial_payment_radio'] ) && 'percent' === $booking_settings[ 'booking_partial_payment_radio' ] ) { | |
if ( 'percent' === $discount_type /* && $cart_item['line_total'] != $addon[ 'Deposit' ] */ && $is_coupon > 0 ) { | |
$addon['Deposit'] = $addon['default_Deposit'] - ( $addon['default_Deposit'] * ( $coupon_amount / 100 ) ); | |
$addon['Total'] = $addon['default_Total'] - ( $addon['default_Deposit'] - $addon['Deposit'] ); | |
$addon['bkap_coupon_applied'] = true; | |
$cart_item['bkap_booking'][$key] = $addon; | |
$update_cart = true; | |
} elseif ( 0 === $is_coupon && isset($addon['bkap_coupon_applied'] ) && true === $addon['bkap_coupon_applied'] ) { | |
$addon['Deposit'] = $addon['default_Deposit']; | |
$addon['Total'] = $addon['default_Total']; | |
$addon['Remaining'] = $addon['default_Remaining']; | |
$addon['bkap_coupon_applied'] = false; | |
$cart_item['bkap_booking'][$key] = $addon; | |
$update_cart = true; | |
} | |
} | |
} | |
$cart_content[$cart_item_key] = $cart_item; | |
} | |
} | |
if ( $update_cart ) { | |
$cart->set_cart_contents($cart_content); | |
$cart->calculate_totals(); | |
} | |
} | |
add_action( 'woocommerce_before_calculate_totals', 'custom_change_cart_meta', 110, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment