MarketPress eCommerce. Charge a handling per order, not per product.

I want to charge for the order per weight plus $3 per order. I see that I can add a handling charge per product, but I only want to charge the extra for each order.

  • Lindeni Mahlalela

    Hello Sj

    I hope you are doing great today. Thank you for the question, and for your patience while we were looking into this.

    This is not possible out of the box and there are no specific hooks or actions specific to achieving this in MarketPress but I was able to have a look at the code and came up with a basic code snippet that should help you get started.

    I am not sure if I understand what you mean by this:

    charge for the order per weight

    But I think the Shipping module can do the calculations for charging the order based on the items' weight. After that has been calculated you can use the following code to add the extra $3 to the cart total and thus achieving the extra $3 charge in addition to the order total.

    Here is the code:

    add_filter( "mp_cart/cart_meta/shipping_total", "add_additional_handling_line", 99, 2);
    add_filter( "mp_cart/total", "add_additional_handling_fee", 10, 3);
    //add_filter( "mp_cart/cart_meta/product_total", "add_additional_handling_line", 99, 2); //add before product total
    function add_additional_handling_line($shipping_line, $cart_object){
        $additional_fee =  mp_format_currency( '', 3 );
        return $shipping_line . '<div class="mp_cart_resume_item mp_cart_resume_item-shipping-total">
            <span class="mp_cart_resume_item_label">' . __( 'Additional Handling', 'mp' ) . '</span>
            <span class="mp_cart_resume_item_amount"> ' . $additional_fee . '</span>
        </div>';
    }
    
    //modify the cart total by adding 3 to the total
    function add_additional_handling_fee($total, $current_total, $cart_object ) {
        return (float) $total + 3;
    }

    You can add the above code to your active theme's functions.php file, but I suggest you create a Must Use Plugin, which is basically a plugin that will be automatically executed by WordPress each time it loads. To create the MU Plugin, start by creating an empty PHP file, maybe name it 'additional-handling-fee.php' then add the above code including the opening PHP tag like so:

    <?php
    //paste the above code

    Then save the file and then upload it to your website in the folder 'wp-content/mu-plugins/additional-handling-fee.php' and after that the if you visit the cart page it should look like this:

    Please note the highlighted line and the total has changed to be the Products Cost + Shipping Cost + The Taxes + Additional Handling Fee that was added. Never mind the currency on the screenshot, it should adapt to the currency of your store.

    Please note that this code is not final but it serves as a guide for you to achieve what you want to achieve. In addition to the above, you may still want to add some hooks to update the email notifications or order emails to include this you may want to check the filters and hooks defined in the following files:

    marketpress/includes/common/class-mp-order.php
    marketpress/includes/common/class-mp-cart.php
    marketpress/includes/common/class-mp-mailer.php
    marketpress/includes/common/template-functions.php

    If you are not comfortable doing this yourself then you may find our Jobs Board useful, it is where you can hire an independent professional developer to do this for you at a price. Please note that the developers you can find from the Jobs and Pros page are independent of WPMU DEV and none of our Staff is allowed to take jobs there, so you will be hiring a completely independent developer.

    I hope this helps. Please let us know if you need any further assistance with regards to this and we will be happy to help wherever possible.

    Have a nice day.
    Lindeni

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.