duanfengshang1088 2018-03-31 07:29
浏览 85
已采纳

在购物车中插入自定义总计行并在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.

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>';
}
  • 写回答

1条回答 默认 最新

  • dtio35880438 2018-03-31 11:04
    关注

    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>';
        }
    }
    

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

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端