Cart Item Sub Total
-
add_action('woocommerce_cart_calculate_fees', 'add_handling_fee_on_payment_gateway_selection'); function add_handling_fee_on_payment_gateway_selection() { // Check if this function is being called in an admin page or an AJAX call, if yes, return if (is_admin() && !defined('DOING_AJAX')) { return; } // Check if a payment method has been selected, if not, return $payment_method = WC()->session->get('chosen_payment_method'); if ( empty( $payment_method ) ) { return; } // Define the handling fees for different payment methods $flatFee = 1; // Payment Gateway Flat 1 Fee $stripePercentFee = 0.029; // Stripe Payment Gateway 2.9% Fee $tabbyPercentFee = 0.065; // Tabby Payment Gateway 6.5% Fee // Check the payment gateway ID and calculate the handling fee accordingly if ( $payment_method === 'stripe' ) { // Calculate the discounted subtotal of the cart with shipping $DiscountSubTotal = (WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total()); $totalFee = ($DiscountSubTotal * $stripePercentFee) + $flatFee; } elseif ( $payment_method === 'tabby_installments' ) { // Set the regular price as the product price $regular_prices_total = 0; foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $product = $cart_item['data']; $regular_price = (float) $product->get_regular_price(); $cart_item['data']->set_price($regular_price); $regular_prices_total += ($regular_price * $cart_item['quantity']); } // Update cart item subtotal to regular prices total WC()->cart->set_cart_contents_total($regular_prices_total); // Calculate the regular subtotal of the cart with shipping $regular_prices_total += WC()->cart->get_shipping_total(); // Disable coupons WC()->cart->remove_coupons(); // Set a standard shipping rate WC()->session->set('chosen_shipping_methods', array('flat_rate:1')); WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages()); // Calculate the handling fee $totalFee = ($regular_prices_total * $tabbyPercentFee) + $flatFee; } else { $totalFee = 0; // No handling fee for other payment methods } // Add the handling fee to the cart WC()->cart->add_fee(__('Handling Fee', 'txtdomain'), $totalFee); }
I added this code to assign some extra fee depends on the payment gateway selected. everything is working well so far but I’m facing an issue with
WC()->cart->set_cart_contents_total($regular_prices_total);
I want to display the items sub total regular prices not the discounted price for particular gateway.$regular_prices_total
this variable I want to show to the customer as sub total of the items when that particular payment gateway is selected. I passes the variable to the setting subtotal amount function but I don’t know why it is now showing there at the checkout page.The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Cart Item Sub Total’ is closed to new replies.