0
votes

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 :)

1

1 Answers

0
votes

Finally assembled the code myself, here for everyone else who may need it:

/*David is adding the code so that a minimum order of 25 dollars is in the cart. || All your processing fee are belong to us now*/
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
add_action( 'woocommerce_checkout_before_order_review' , 'wc_minimum_order_amount');
function wc_minimum_order_amount() {
global $woocommerce;
$prod_to_check = 36324; //Processing Fee line item ID
$found = false; // Default value

/* Check for the Processing Fee Line Item in Woocommerce Cart */
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        if ($cart_item['product_id'] == $prod_to_check || $cart_item['variation_id'] == $prod_to_check) {
            $found = true;
        }
    }

/* If the Processing Fee Line Item is in the cart, remove it. This is required to update the cost of the line item. Or keep it off, in case the minimum order amount has been reached.*/    
    if( $found == true) {
            $prod_to_remove = 36324; // Set the product ID to remove
            foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
                if ($cart_item['product_id'] == $prod_to_remove || $cart_item['variation_id'] == $prod_to_remove) { //remove single product
                        $woocommerce->cart->remove_cart_item($cart_item_key);
                }
            }
    }

/* Calculate the difference up to the minimum order amount required, then change Processing Fee line item price accordingly. */
    if ( WC()->cart->total < 34.95) {
        $minimum = 34.95; // Set this variable to specify a minimum order value
        $surcharge_product = ($minimum - WC()->cart->total);
        update_post_meta( '36324', '_regular_price', str_replace("$","", $surcharge_product) );
        update_post_meta( '36324', '_price', str_replace("$","", $surcharge_product) );
    }

/* If the cart total is below minimum order amount, add the Processing Fee Line item with the new price. */
    if ( WC()->cart->total < 34.95) {
            if( is_cart() || is_checkout() ) {
                    $woocommerce->cart->add_to_cart(36324);
                wc_add_notice( 
                            sprintf( 'A Processing Fee has been added to make your order total match our minimum order of 34.95.'
                            ), 'error' 
                        );
            }
    }
}