【问题标题】:Customizing product dimensions output on front end + change dimensions title在前端自定义产品尺寸输出+更改尺寸标题
【发布时间】:2020-01-01 15:25:23
【问题描述】:

使用 "Reorder and customize product dimensions formatted output in WooCommerce" 回答代码我想让输出显示为:

尺寸:D40 x W45 x H60(厘米)

感谢任何帮助。

【问题讨论】:

  • 不,只是在你想要的描述前面加上“问题”,仍然不能使这成为一个正确的问题描述。再说一遍:您尝试过的代码发生了什么
  • @misorude,已更新问题。使它更简单。希望这会有所帮助
  • @misorude,它确实说如果你使用代码你会得到这个尺寸:L 40 x W 45 x H 60 cm 但我想要这个尺寸:D40 x W45 x H60 (cm) 对不起,我不知道怎么写更清楚。
  • 为什么不使用 str_replace ?

标签: php wordpress woocommerce product dimensions


【解决方案1】:

欢迎来到 StackOverflow! 尝试更简单的代码(经过测试):

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 ) );

    return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' ('.get_option( 'woocommerce_dimension_unit' ).')';
}

【讨论】:

  • @FurnitureSuppliesUK 太好了。如果某个解决方案对您有用,请选择它作为选择的答案(左侧的 V 标记)thx。 :)
【解决方案2】:

只需在您的活动主题的functions.php中添加以下代码sn-p即可实现上述 -

function woocommerce_display_product_attributes( $product_attributes, $product ){
    if( !isset( $product_attributes['dimensions'] ) ) return $product_attributes;

    $modified_dimensions = array();
    foreach ( $product->get_dimensions( false ) as $key => $value ) {
        if( $key == 'length' )
            $modified_dimensions[$key] = 'D'.$value;
        if( $key == 'width' )
            $modified_dimensions[$key] = 'W'.$value;
        if( $key == 'height' )
            $modified_dimensions[$key] = 'H'.$value;
    }
    $dimension_string = implode( ' × ', array_filter( array_map( 'wc_format_localized_decimal', $modified_dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        $dimension_string .= ' (' . get_option( 'woocommerce_dimension_unit' ) . ')';
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }
    // change dimensions label & value.
    $product_attributes['dimensions']['label']  = __( 'Size', 'text-domain' );
    $product_attributes['dimensions']['value']  = $dimension_string;
    return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'woocommerce_display_product_attributes', 99, 2 );

【讨论】:

  • 谢谢,但它对我不起作用。从那以后,我意识到我在functions.php 中还有一些代码可以让Dimensions 显示在产品页面和存档页面中。我现在在这段代码中看到我可以更改维度标题。也许此代码与您的代码冲突。如果可以的话,我会更新上面的原始代码。
  • 这是经过测试的代码。只需确保删除所有以前的相关代码即可。或者,也许您的主题已经对其进行了一些艰难的定制。请确定。
猜你喜欢
  • 1970-01-01
  • 2019-08-30
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
相关资源
最近更新 更多