Woocommerce add Dynamic Processing Fee as Line Item

admin

Administrator
Staff member
This is my first question here on stackoverflow, so forgive me in case I break any community conventions or such.

My problem is with Woocommerce, in particular that I want to add a processing fee, but as line item because PayPal Advance has issues processing an additional fee properly through the '$woocommerce->cart->add_fee' command.

I found quite a couple of clues here and other individuals who had similar problems, but none of those solutions helped me as they appear to be either outdated, or I might misunderstand.

Here is one of the threads I looked at to assemble my code as it is currently:

<a href="https://stackoverflow.com/questions/12327251/woocommerce-add-product-to-cart-with-price-overrid">WooCommerce: Add product to cart with price override?</a>

The purpose of our code is:

<ul>
<li>Add a custom fee to reach Minimum Order amount (which is 34.95)</li>
<li>Add this fee ONLY if the total order (incl. shipping &amp; tax) is UNDER 34.95</li>
<li>Add the fee as a line item to the cart.</li>
</ul>

Here is my up-to-date attempt at solving this issue:

Code:
// Add cart functionality to add additional fee to reach minimum order

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
    global $woocommerce;
    if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
    return;

        $taxes = array_sum($woocommerce-&gt;cart-&gt;taxes);
    $toasty = ( $woocommerce-&gt;cart-&gt;cart_contents_total + $woocommerce-&gt;cart-&gt;shipping_total + $taxes );  
        $surcharge = 34.95 - ( $woocommerce-&gt;cart-&gt;cart_contents_total + $woocommerce-&gt;cart-&gt;shipping_total + $taxes );

    if ($surcharge &gt; 0 ) {
        $custom_price = $surcharge; // This will be your custom price  
        $items = $woocommerce-&gt;cart-&gt;get_cart();
            foreach($items as $item =&gt; $value) {
                if ( $value['product_id'] !== 36324 &amp;&amp; $first) {
                    $woocommerce-&gt;cart-&gt;add_to_cart(36324);
                    $first = false;
                }
                if ( $value['product_id'] == 36324 &amp;&amp; $second) {
                    $woocommerce-&gt;cart-&gt;remove_cart_item($item);
                    $value['data']-&gt;price = $custom_price;
                    $second = false;
                }
                if ( $value['product_id'] !== 36324 &amp;&amp; $third) {
                    $woocommerce-&gt;cart-&gt;add_to_cart(36324);
                    $third = false;
                }
        }
    }
}

Currently it times out with this wordpress error:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /wp-includes/query.php on line 1680

I believe it is due the multiple 'if' clauses, instead of calling out different functions.

The reason why we want to remove and then re-add it is because it changes the price, but it does not acknowledge it in the cart total. So for example Processing Fee may say '$21.00' in the cart, but in reality it still charges only $1.00.

Thank you very much in advance, should you need more information to help me out - I will gladly provide that :)