更改 WooCommerce 中长度的产品尺寸标签

时间:2021-04-07 09:09:13

标签: php wordpress woocommerce

我有一些自定义代码可以在 Wordpress/Woocommerce 中创建一个短代码,用于显示产品尺寸(长度宽度高度 >) - 输出工作正常,但是我想将“长度”的标签更改为“深度”(仅前端很好)并重新排序输出显示为高度宽度深度(按此顺序)

这是我创建短代码的代码 - 但是,我不确定我需要做什么来更改输出标签和重新排序它们。

add_shortcode( 'product_taxonomies', 'product_taxonomies_shortcode' );
function product_taxonomies_shortcode( $atts ){
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_taxonomies' );

    $product_id = ! empty($atts['id']) ? $atts['id'] : 0;

    if( $product_id > 0 ) {
        $product = wc_get_product( $product_id );
    } else {
        global $product;
    }

    if( ! is_a($product, 'WC_Product' ) ) {
        $product = wc_get_product( get_the_id() );
    }

    if ( is_a($product, 'WC_Product' ) ) {

        ob_start();

        // Weight
        if ( $product->has_weight() ) {
            $weight_unit = get_option('woocommerce_weight_unit');
            $weight_html = '<strong>'.__("Weight").':</strong> ' . $value . $weight_unit;
            echo '<div class="dimensions">' . $weight_html . '</div>';
        }

        // Dimensions
        if ( $product->has_dimensions() ) {
            $dimension_unit = get_option('woocommerce_dimension_unit');
            $dimensions     = array();
            foreach( $product->get_dimensions( false ) as $key => $value ) {
                if( ! empty($value) )
                    $dimensions[] = '<strong>'.ucfirst($key).':</strong> ' . $value . $dimension_unit;
            }
            echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';
        }

        
        return ob_get_clean();
    }
}

3 个答案:

答案 0 :(得分:2)

问题空间

首先,让我们确定您需要执行更改的代码范围:

        $dimensions     = array();
        foreach( $product->get_dimensions( false ) as $key => $value ) {
            if( ! empty($value) )
                $dimensions[] = '<strong>'.ucfirst($key).':</strong> ' . $value . $dimension_unit;
        }
        echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';

执行更改

        $keys = [
            'Height' => 0,
            'Width' => 0,
            'Depth' => 0
        ];
        $dimensions     = array();
        foreach( $product->get_dimensions( false ) as $key => $value ) {
            if( ! empty($value) ) {
                $currentKey = (($key === 'Length') ? 'Depth' : $key);
                $dimensions[$keys[$currentKey]] = '<strong>'.ucfirst($currentKey).':</strong> ' . $value . $dimension_unit;
            }
        }
        echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';

说明

  • 我们将矩阵映射到他们想要的数组索引,因此数组将以正确的顺序构建,而不是重新排序
  • 如果是 $key,我们将 'Length' 替换为 'Depth',否则使用 $key,任何替换都在表示级别进行

潜在问题

我假设了一些具体的关键值。如果我对它们的理解是错误的,那么一些不正确的值可能会引起麻烦。如果是这种情况,让我们讨论一下您可能会遇到的情况。

答案 1 :(得分:0)

我想我已经用另一种方式解决了

function display_product_dimensions( $atts ){
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_dimensions' ) );

    if( $id == '' ) {
        global $product;

        if( ! is_a($product, 'WC_Product') ) {
            $product = wc_get_product( get_the_id() );
        }
    }

    return method_exists( $product, 'get_dimensions' ) ? wc_format_dimensions($product->get_dimensions(false)) : '';
}


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

    // Set here your new array of dimensions based on existing keys/values
    $new_dimentions = array(
        '<b>Height:</b>' => $dimensions['height'],
        '<b>Width:</b>'  => $dimensions['width'],
        '<b>Depth:</b>' => $dimensions['length']
    );

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

    $label_with_dimensions = array();

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

    return implode( ' <br> ',  $dimensions) . '';
}

这似乎可以正常工作并正确显示...

答案 2 :(得分:-3)

我知道这不是您直接要求的,甚至可能与开发者论坛上的主题无关。但我想提供第二个选择:一个插件,允许您在不编辑 WordPress 核心的情况下更改网站上的字符串。

我想有很多这样的插件,但我喜欢的是一个叫做 Say What

祝你好运!