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:
WooCommerce: Add product to cart with price override?
The purpose of our code is:
- Add a custom fee to reach Minimum Order amount (which is 34.95)
- Add this fee ONLY if the total order (incl. shipping & tax) is UNDER 34.95
- Add the fee as a line item to the cart.
Here is my up-to-date attempt at solving this issue:
// 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() && ! defined( 'DOING_AJAX' ) )
return;
$taxes = array_sum($woocommerce->cart->taxes);
$toasty = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes );
$surcharge = 34.95 - ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes );
if ($surcharge > 0 ) {
$custom_price = $surcharge; // This will be your custom price
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $value) {
if ( $value['product_id'] !== 36324 && $first) {
$woocommerce->cart->add_to_cart(36324);
$first = false;
}
if ( $value['product_id'] == 36324 && $second) {
$woocommerce->cart->remove_cart_item($item);
$value['data']->price = $custom_price;
$second = false;
}
if ( $value['product_id'] !== 36324 && $third) {
$woocommerce->cart->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 :)