簡體   English   中英

在 WooCommerce 管理手動訂單上添加產品重量和尺寸作為訂單項元數據

[英]Add product weight and dimensions as order item meta data on WooCommerce admin manual orders

基於將產品自定義字段保存為 WooCommerce 管理員手動訂單的自定義訂單項元數據

我有這些代碼顯示從前端下的訂單的產品的重量和尺寸,但我無法讓它們為從后端手動下的訂單工作。 當我添加產品時,我看不到重量和尺寸,它們也不會顯示在支付訂單的頁面上,收到訂單和電子郵件。

那是我的代碼:

隨處顯示和保存產品的重量。

// Display the cart item weight in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
    if ( $cart_item['data']->get_weight() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Weight', 'woocommerce' ),
            'value' =>  $cart_item['data']->get_weight()  . ' ' . get_option('woocommerce_weight_unit')
        );
    }
    return $cart_item_data;
}

// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'Weight', 'woocommerce' ), $values['data']->get_weight()  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

隨處顯示和保存產品尺寸。

// Display the cart item weight in dimensions and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data1', 10, 2 );
function display_custom_item_data1( $cart_item_data, $cart_item ) {
    if ( $cart_item['data']->has_dimensions() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Dimensions', 'woocommerce' ),
            'value' =>  $cart_item['data']->get_dimensions()  . ' ' . get_option('woocommerce_dimensions_unit')
        );
    }
    return $cart_item_data;
}

// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data1', 20, 4 );
function display_order_item_data1( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->has_dimensions() > 0 ){
        $item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $values['data']->get_dimensions()  . ' ' . get_option('woocommerce_dimensions_unit') );
    }
}

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
    $text_domain    = 'woocommerce';
    $dimension_unit = get_option('woocommerce_dimensions_unit');

    if ( empty( $dimension_string ) )
        return __( 'N/A', $text_domain );

    // Set here your new array of dimensions based on existing keys/values
    $new_dimentions = array(
        __('Largo:', $text_domain)    => $dimensions['length'],
        __('Ancho:', $text_domain) => $dimensions['width'],
        __('Alto:', $text_domain)     => $dimensions['height'],
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );

    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . get_option( 'woocommerce_dimension_unit' );
    }

    return implode( ' × ',  $dimensions);
}

要將產品重量和尺寸顯示為手動后端訂單上的自定義訂單項元數據,您將使用woocommerce_before_save_order_item專用操作掛鈎,如下所示:

add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
    // 1. WEIGHT
    $weight = $item->get_meta('Weight');
    // If custom meta data is not saved as order item
    if ( empty($weight) ) {
        // Get the WC_Product Object
        $product = $item->get_product();
        
        // Get custom meta data from the product
        $weight      = $product->get_weight();
        $weight_unit = get_option('woocommerce_weight_unit');

        // Save it as custom order item (if defined for the product)        
        if ( $weight ) {
            $item->update_meta_data( 'Weight', $weight . ' ' . $weight_unit );
        }
    }
    
    // 2. DIMENSIONS
    $dimensions = $item->get_meta('Dimensions');
    // If custom meta data is not saved as order item
    if ( empty($dimensions) ) {
        // Get the WC_Product Object
        $product = $item->get_product();
        
        // Get custom meta data from the product
        $dimensions = $product->get_dimensions();
        $dim_unit   = get_option('woocommerce_dimensions_unit');

        // Save it as custom order item (if defined for the product)        
        if ( $dimensions ) {
            $item->update_meta_data( 'Dimensions', $dimensions . ' ' . $dim_unit );
        }
    }
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM