【问题标题】:Reorder and customize product dimensions formatted output in WooCommerce在 WooCommerce 中重新排序和自定义产品尺寸格式化输出
【发布时间】:2018-07-08 08:33:16
【问题描述】:

首先,我要感谢 LoicTheAztec 的回答 here。我正在寻找非常相似的定制,但不是 100% 相同,我想要实现的是:WxLxHmm

是的,最后是计量单位。

示例:200x600x200mm

根据提供的答案,我设法得到了 200mmx600mmx200mm

如果我只想在末尾添加单位“mm”怎么办?

以下是我编辑后的代码版本

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimensions', 10, 2 );
function custom_formated_product_dimensions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );

// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array( 
'width'  => $dimensions['width'],
'length' => $dimensions['length'],
'height' => $dimensions['height']
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );

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

return implode( 'x',  $label_with_dimensions);
}

【问题讨论】:

    标签: php wordpress woocommerce product dimensions


    【解决方案1】:

    要以不同的方式对您的代码自定义重新排序维度输出,您需要:

    add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
    function custom_formated_product_dimentions( $dimension_string, $dimensions ){
        if ( empty( $dimension_string ) )
            return __( 'N/A', 'woocommerce' );
    
        // Set here your new reordered array of dimensions based on existing keys/values
        $new_dimentions = array(
            'width'  => $dimensions['width'],
            'length' => $dimensions['length'],
            'height' => $dimensions['height']
        );
    
        $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
    
        return implode( 'x',  $dimensions) . get_option( 'woocommerce_dimension_unit' );
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    这将输出类似于200x600x200mm (最后只有一次mm

    【讨论】:

    • 以上代码给出 200 Wx600 Lx200 Hmm。我想要 200x600x200mm ,所以我在下面的答案中编辑了“foreach”行。
    • 不用担心@LoicTheAztec,在播放您提供的代码后,我得到了我正在寻找的答案。非常感谢:)
    • 对不起@Loic,我不是这个意思。我很着急,因为已经过了就寝时间。我一直试图弄清楚这几天。我只想克服它并与社区分享。我已经删除了我的答案。最后,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 2020-01-01
    • 2018-01-06
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多