0

I've searched a lot to find out how I can add a custom row inside the total costs in my WooCommerce shop and checked out the hooks but can't find the solution. This is what I've tried:

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );

function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {

$total_rows['recurr_not'] = array(
    'label' => __( 'Rec:', 'woocommerce' ),
    'value' => 'blabla'
);

return $total_rows;
}

But this only adds the row to the total rows in the recipe email. I need to change my total row, add a new row with a name and a value calculated from the cart subtotal field.

This is how I need it:

enter image description here

Do you now how to to this? I have no plan. Thanks for your help!

  • do you want to add extra price in that custom row? – Pullata Praveen Sep 25 '18 at 11:58
  • Yes, I want to add a row with a custom label and a custom price calculated from the cart subtotal value above for example value = (getSubtotal() * 100); – Johnny97 Sep 25 '18 at 12:00
  • Exactly below the Subtotal is not possible. try to edit this file 'cart-totals.php' – Pullata Praveen Sep 25 '18 at 12:06
  • @PullataPraveen but this would be a problem because of plugin updates etc. – Johnny97 Sep 25 '18 at 12:45
  • @jojnny97 you can add woocommerce folder and that page inside the woocommerce in your child theme and then you can edit – Pullata Praveen Sep 26 '18 at 8:49
-1

Try This:

        // Add a custom field before single add to cart
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
function custom_product_price_field(){
    echo '<div class="custom-text text">
    <p>Extra Charge ('.get_woocommerce_currency_symbol().'):</p>
    <input type="text" name="custom_price" value="" placeholder="e.g. 10" title="Custom Text" class="custom_price text_custom text">
    </div>';
}

// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
function add_custom_field_data( $cart_item_data, $product_id ){
    if (! isset($_POST['custom_price']))
        return $cart_item_data;

    $custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
    if( empty($custom_price) )
        return $cart_item_data;

    $product    = wc_get_product($product_id); // The WC_Product Object
    $base_price = (float) $product->get_regular_price(); // Product reg price

    // New price calculation
    $new_price = $base_price + $custom_price;

    // Set the custom amount in cart object
    $cart_item_data['custom_data']['extra_charge'] = (float) $custom_price;
    $cart_item_data['custom_data']['new_price'] = (float) $new_price;
    $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique

    return $cart_item_data;
}

// Set the new calculated cart item price
add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1 );
function extra_price_add_custom_price( $cart ) {
    if ( is_admin() && !defined('DOING_AJAX') )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['custom_data']['new_price']) )
            $cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] );
    }
}

// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3 );
function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['custom_data']['extra_charge']) ) {
        $product = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
        $product_price .= '<br>' . wc_price( $cart_item['custom_data']['extra_charge'] ).'&nbsp;';
        $product_price .= __("Extra Charge", "woocommerce" );
    }
    return $product_price;
}

Code goes in function.php file enter image description here

  • Thanks for your idea but I need to add the new line first between subtotal and total. I already now how to change the total price. – Johnny97 Sep 25 '18 at 11:33
  • Hey! can you try the above code. Let me know if it's working I updated the code as per your requirement @Johnny97 – Pullata Praveen Sep 25 '18 at 11:39
  • Why adding an <input type="text" name="custom_price" value="" placeholder="e.g. 10" title="Custom Text" class="custom_price text_custom text"> ? This makes no sense for me because I now the name for the row and calculate the price in the backend in my functions.php – Johnny97 Sep 25 '18 at 11:42
  • Cart Totals_____________ Cart Subtotal $20 MyCustomField $2.90 Order Total $22.90 This the format you asked you have clearly mentioned custom field – Pullata Praveen Sep 25 '18 at 11:44
  • Yes but why as a HTML input field? Makes absolutely no sense for me. The format there is like <tr><label>Custom field</label</tr> – Johnny97 Sep 25 '18 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.