Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_cart_calculate_fees', 'wc_payment_based_discount', 10, 1 );
- function wc_payment_based_discount( $cart ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- // Check if a coupon code has been applied
- if ( WC()->cart->has_discount() )
- return;
- // Check if the product "Wallet Topup" is in the cart
- $wallet_topup_found = false;
- foreach ( $cart->get_cart() as $cart_item ) {
- if ( $cart_item['data']->get_name() == 'Wallet Topup' ) {
- $wallet_topup_found = true;
- break;
- }
- }
- if ( $wallet_topup_found )
- return;
- // Check if the product is on sale
- $product_on_sale = false;
- foreach ( $cart->get_cart() as $cart_item ) {
- if ( $cart_item['data']->is_on_sale() ) {
- $product_on_sale = true;
- break;
- }
- }
- if ( $product_on_sale )
- return;
- // Get chosen payment method
- $payment_method = WC()->session->get( 'chosen_payment_method' );
- if ( $payment_method == 'wallet' ) {
- $discount_percent = 2;
- } elseif ( $payment_method == 'cod' ) {
- $discount_percent = 2;
- } else {
- $discount_percent = 0;
- }
- // Calculate discount amount
- $discount_amount = $cart->get_subtotal() * ( $discount_percent / 100 );
- $cart->add_fee( 'Discount', - $discount_amount, true );
- }
Advertisement
Add Comment
Please, Sign In to add comment