【问题标题】:Insert a custom total row on cart and checkout totals in Woocommerce在 Woocommerce 中的购物车和结帐总计上插入自定义总计行
【发布时间】:2018-09-10 03:23:35
【问题描述】:

在 woocommerce 中,我成功地向 Woocommerce 简单产品添加了一个自定义字段,并将该值显示在产品页面的附加信息选项卡上。

现在我完全迷失了,我的拼图的最后一块试图在购物车和结帐页面中插入一个总行以计算总计算量。

自定义字段用于存储家具项目的体积(以 m3 为单位)。当客户将商品添加到购物篮时,我想通过将所有自定义字段的值相加并在页面上向客户显示总数来计算装运的总 m3。谁能指出我如何添加这些自定义字段并显示它们的正确方向。

到目前为止,我的代码是这样的

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

【问题讨论】:

    标签: php wordpress woocommerce volume shipping


    【解决方案1】:

    以下将显示购物车和结帐页面中的购物车总量:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 可以根据产品尺寸值在产品级别自动计算体积自定义字段...如果您对此感兴趣,您可以提出新的相关问题并在此处通知我...然后我会回答的。
    • 您好 - 我可以使用代码在小部件区域或页面上插入总体积的表格吗?非常感谢 - 你的知识给我留下了深刻的印象 - 大弓
    猜你喜欢
    • 2021-08-14
    • 2019-08-14
    • 2015-07-29
    • 2013-12-28
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多