【问题标题】:Include dimension letters L/W/H to WooCommerce formatted product dimensions output包括尺寸字母 L/W/H 到 WooCommerce 格式的产品尺寸输出
【发布时间】:2018-01-06 00:56:15
【问题描述】:

我正在尝试修改调用 get_dimensions() 的所有地方的尺寸输出,而不是默认情况下它会包含尺寸字母。

所以它将是 1 L x 1 W x 1 H 而不是 1 x 1 x 1

我可以覆盖 get_dimensions() 函数并在数组中的每个值旁边包含字母,或者我可以以某种方式使用 wc_format_dimensions() 来做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce product dimensions


    【解决方案1】:

    更新:你不能覆盖get_dimensions() WC_Product方法。

    但是您可以使用位于wc_format_dimensions() 函数中的woocommerce_format_dimensions 过滤钩子,为每个维度产品属性添加自定义标签,这样(随心所欲)

    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' );
    
        $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
        $label_with_dimensions = []; // Initialising
    
        // Loop Though dimensions array
        foreach( $dimensions as $key => $dimention )
            $label_with_dimensions[$key] = strtoupper( substr($key, 0, 1) ) . ' ' . $dimention;
    
        return implode( ' x ',  $label_with_dimensions) . ' ' . get_option( 'woocommerce_dimension_unit' );
    }
    

    代码进入您的活动子主题(或主题)的functions.php 文件或任何插件文件中。

    此代码在 WooCommerce 版本 3+ 上经过测试并且可以正常工作

    【讨论】:

    • 完美运行。不确定是否应该创建单独的问题,但我如何将“长度”键重命名为“直径”。我认为可以使用 array_map 替换 $dimensions 数组?尝试了几件事,但收到“非法字符串偏移”警告。
    • @Aivaras 最好在一个新问题中提出这个问题,可能会在其中添加这段代码,解释你现在得到什么,然后你想得到什么……或类似的东西。我会回答(也可能是其他人),因为不可能将我的回答作为评论包含在内。还要尝试明确解释您要在哪里重命名(将出现在哪里)......
    【解决方案2】:
    $dimensions = $product->get_dimensions();
    
    if ( !empty( $dimensions) ) {
    
    echo 'Height: ' . $product->get_height() . get_option( 'woocommerce_dimension_unit' );
    
    echo 'Width: ' . $product->get_width() . get_option( 'woocommerce_dimension_unit' );
    
    echo 'Length: ' . $product->get_length() . get_option( 'woocommerce_dimension_unit' );
    
    }
    

    【讨论】:

    • 请在您的答案中添加一些进一步的解释,以便其他人可以从中学习。此代码如何更改调用get_dimensions所有地方 中的输出?
    【解决方案3】:

    我已将Ed 代码添加到您的活动子主题的functions.php 文件中,现在它工作正常:

    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' );
    
        $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
        $label_with_dimensions = []; // Initialising
    
        // Loop Though dimensions array
        foreach( $dimensions as $key => $dimention )
            $label_with_dimensions[$key] = strtoupper( substr($key, 0, 1) ) . ' ' . $dimention;
    
        return implode( ' x ',  $label_with_dimensions) . ' ' . get_option( 'woocommerce_dimension_unit' );
    }
    

    【讨论】:

    • 您介意注意一下您所做的更改以及为什么会在这里有所作为吗?
    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 2017-02-01
    • 2020-01-01
    • 2018-07-08
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多