• Resolved sgallery

    (@sgallery)


    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)
  • Hi @sgallery

    Thanks for reaching out!

    I understand that have implemented the custom code above to add handling fees based on the selected payment gateway. However, you are facing an issue with updating the cart subtotal to display the regular prices instead of discounted prices for a particular payment gateway, correct?

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Cart Item Sub Total’ is closed to new replies.