admin管理员组文章数量:1334671
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:
Do you now how to to this? I have no plan. Thanks for your help!
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:
Do you now how to to this? I have no plan. Thanks for your help!
Share Improve this question edited Sep 25, 2018 at 11:51 Johnny97 asked Sep 25, 2018 at 11:21 Johnny97Johnny97 2147 silver badges18 bronze badges 6- do you want to add extra price in that custom row? – Praveen Commented Sep 25, 2018 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 Commented Sep 25, 2018 at 12:00
- Exactly below the Subtotal is not possible. try to edit this file 'cart-totals.php' – Praveen Commented Sep 25, 2018 at 12:06
- @PullataPraveen but this would be a problem because of plugin updates etc. – Johnny97 Commented Sep 25, 2018 at 12:45
- @jojnny97 you can add woocommerce folder and that page inside the woocommerce in your child theme and then you can edit – Praveen Commented Sep 26, 2018 at 8:49
2 Answers
Reset to default 1Its too late but it can still help to others who are looking for the same :
Copy "plugins\woocommerce\templates\cart\cart-totals.php" to
"your-theme\woocommerce\templates\cart\cart-totals.php"
Open "your-theme\woocommerce\templates\cart\cart-totals.php" and add the following line inside the table tag
<?php do_action( 'woocommerce_cart_totals_custom_text' ); ?>
Then open your function.php and add the following:
add_action( 'woocommerce_cart_totals_custom_text', 'action_woocommerce_cart_totals_before_shipping', 10, 0 );
function action_woocommerce_cart_totals_before_shipping( ) {
echo "<tr class='cart-subtotal'><th>Title</th><td>Text</td></tr>";
}
Here is the result
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'] ).' ';
$product_price .= __("Extra Charge", "woocommerce" );
}
return $product_price;
}
Code goes in function.php
file
本文标签: phpHow can I add another row to the total field in WooCoommerce cartcheckout and recipe mail
版权声明:本文标题:php - How can I add another row to the total field in WooCoommerce cart, checkout and recipe mail? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742377137a2463383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论