在购物车中插入自定义总计行,并在Woocommerce中结帐总计 [英] Insert a custom total row on cart and checkout totals in Woocommerce

查看:76
本文介绍了在购物车中插入自定义总计行,并在Woocommerce中结帐总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我已经成功地将自定义字段添加到Woocommerce简单产品中,并将该值显示在产品页面的其他信息选项卡上.

In woocommerce, I have successfully managed to add a custom field to a Woocommerce simple product and display that value on the additional info tab on a product page.

现在,我对拼图的最后一块完全迷失了,因为我试图在购物车和结帐页面中插入总计行以获取计算出的总体积.

Now I am totally lost with the final piece of my jigsaw trying to insert a total row in cart and checkout pages with for the total calculated volume.

自定义字段用于将体积存储在m3的家具中.当客户将商品添加到购物篮中时,我想通过将所有自定义字段的值加在一起并在页面上向客户显示总计来计算装运的总m3.任何人都可以向我指出如何添加这些自定义字段并显示它们的正确方向.

The custom field is to store the volume in m3 of a furniture item. When a client adds an item to the basket I would like to calculate the total m3 of the shipment by adding the values of all the custom fields together and displaying the total to the customer on the page. Can anyone point me in the right direction of how to add these custom fields and display them please.

到目前为止,我的代码看起来像这样

So far my code looks like this

    // Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );

function woocom_general_product_data_custom_field() {
  // Create a custom text field


  // Number Field
  woocommerce_wp_text_input( 
    array( 
      'id' => '_item_volume', 
      'label' => __( 'Item Shipping Volume', 'woocommerce' ), 
      'placeholder' => '', 
      'description' => __( 'Enter the volume her in m3.', 'woocommerce' ),
      'type' => 'number', 
      'custom_attributes' => array(
         'step' => 'any',
         'min' => '0.001'
      ) 
    )
  );

}

// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );

/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {

  // Save Number Field
  $number_field = $_POST['_item_volume'];
  if( ! empty( $number_field ) ) {
     update_post_meta( $post_id, '_item_volume', esc_attr( $number_field ) );
  }

}

add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {

    //Product ID - WooCommerce compatibility
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    // Get your custom fields data
    $custom_field1 = get_post_meta( $product_id, '_item_volume', true );


    // Set your custom fields labels (or names)
    $label1 = __( 'Shipping Volume m3', 'woocommerce');


    // The Output
    echo '<h3>'. __('Item Shipping Volume', 'woocommerce') .'</h3>
    <table class="custom-fields-data">
        <tbody>
            <tr class="custom-field1">
                <th>'. $label1 .'</th>
                <td>'. $custom_field1 .'</td>
            </tr>
        </tbody>
    </table>';
}

推荐答案

以下内容将在购物车和结帐页面中显示购物车总体积:

The following will display the total cart volume in cart and checkout page:

add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
function display_cart_volume_total() {
    $total_volume = 0;

    // Loop through cart items and calculate total volume
    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
        $total_volume  += $product_volume * $cart_item['quantity'];
    }

    if( $total_volume > 0 ){

        // The Output
        echo ' <tr class="cart-total-volume">
            <th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
            <td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
        </tr>';
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在购物车中插入自定义总计行,并在Woocommerce中结帐总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆