php - 在 WooCommerce 管理员手动订单上添加产品重量和尺寸作为订单项元数据

标签 php wordpress woocommerce hook-woocommerce orders

基于 Save product custom-field as custom order item metadata for WooCommerce admin manual orders

我有这些代码显示从前端下达的订单的产品重量和尺寸,但我无法让它们用于从后端手动下达的订单。当我添加产品时,我没有看到重量和尺寸,它们也没有显示在支付订单、收到订单和电子邮件的页面上。

这是我的代码:

到处显示和保存产品的重量。

// 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 专用操作 Hook ,如下所示:

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 文件。经过测试并有效。

关于php - 在 WooCommerce 管理员手动订单上添加产品重量和尺寸作为订单项元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64033925/

相关文章:

php - MySQL - 设置字符串到数字的隐式转换?

wordpress - 有没有办法在将商品添加到购物车之前连接到 WooCommerce

php - Wordpress-Woocommerce

php - Woocommerce 优惠券添加自定义复选框

php - 有没有办法在 Opencart 中显示 youtube 视频而不是产品图片?

php - 将值作为函数的参数?

php - 'field list' php/mySQL 中的未知列,但该字段确实存在。

javascript - 一个部分或 div 的 Wordpress block 主题 css

php - 无法连接MySQL : (1045) Access denied for user 'root' @'localhost' (using password: YES)

php - 如何在wordpress中实现分页