随处显示和保存产品的格式化尺寸

Display and save formated dimensions of the product everywhere

Display the weight of cart and order items in WooCommerce @LoicTheAztec 的回答启发,通过用产品尺寸替换产品重量进行一些更改,我能够在购物车商品和订单商品的任何地方显示商品尺寸。

问题是我不知道如何显示标签或形容词:

西班牙站点的经度 cm x Ancho cm x Alto cm。

非常感谢任何帮助。

// Display the cart item dimensions in cart and checkout pages 
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 ); 
function display_custom_item_data_dimensions( $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_data_dimensions', 20, 4 ); 
function display_order_item_data_dimensions( $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') ); 
    } 
}

首先,方法 get_dimensions() 有一个显示格式化维度的选项,因此您可以使用以下方法更改维度格式,以根据需要添加维度标签:

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_dimensions = array(
        __('width', $text_domain)  => $dimensions['width'],
        __('length', $text_domain) => $dimensions['length'],
        __('height', $text_domain) => $dimensions['height'],
    );

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

    // Loop through new dimensions array adding spanish labels 
    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
    }

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

对于西班牙语,您可以翻译带有 Loco plugin 的字符串,或者您可以替换:

$new_dimensions = array(
    __('width', $text_domain)  => $dimensions['width'],
    __('length', $text_domain) => $dimensions['length'],
    __('height', $text_domain) => $dimensions['height'],
);

作者:

$new_dimensions = array(
    __('longitud', $text_domain) => $dimensions['width'],
    __('ancho', $text_domain)    => $dimensions['length'],
    __('alto', $text_domain)     => $dimensions['height'],
);

现在,在您的代码中,您将使用 get_dimensions( true ) 方法显示格式化维度,如下所示:

// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
    $product =  $cart_item['data'];

    if ( $product->has_dimensions() ){
        $cart_item_data[] = array(
            'name' => __( 'Dimensions', 'woocommerce' ),
            'value' => $product->get_dimensions( true ),
        );
    }
    return $cart_item_data;
}

// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
    $product =  $values['data'];

    if ( $product->has_dimensions() ){
        $item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

Note: As has_dimensions() is a conditional method returning a boolean value, it doesns't need anything else as a condition.

类似: