如何在WooCoommerce购物车、结账和食谱邮件中的总计字段中再添加一行?

时间:2018-09-25 作者:Johnny97

我搜索了很多,想知道如何在WooCommerce商店的总成本中添加自定义行,并查看了挂钩,但找不到解决方案。这就是我所尝试的:

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;
}
但这只会将该行添加到配方电子邮件的总行中。我需要更改我的总行,添加一个新行,该行的名称和值由cart subtotal字段计算得出。

这就是我需要它的方式:

enter image description here

你现在该怎么做?我没有计划。谢谢你的帮助!

2 个回复
SO网友:Prateek Tyagi

虽然为时已晚,但它仍然可以帮助其他正在寻找相同产品的人:

将“plugins\\woomerce\\templates\\cart\\cart totals.php”复制到“your theme\\woomerce\\templates\\cart\\cart totals.php”

打开“your theme\\woocommerce\\templates\\cart\\cart totals.php”,并在表标记中添加以下行

<?php do_action( \'woocommerce_cart_totals_custom_text\' ); ?>
然后打开函数。php并添加以下内容:

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>";
}
结果如下

enter image description here

SO网友:Praveen

尝试以下操作:

        // 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;
}
代码进入function.php 文件enter image description here

结束