代码之家  ›  专栏  ›  技术社区  ›  James Donald

在购物车上插入自定义总计行,并在Woocommerce中签出总计

  •  4
  • James Donald  · 技术社区  · 7 年前

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

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

    自定义字段用于存储家具项目的体积(以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>';
    }
    
    1 回复  |  直到 6 年前
        1
  •  8
  •   LoicTheAztec    7 年前

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

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

    代码进入功能。活动子主题(或活动主题)的php文件。 已测试并正常工作。

    enter image description here