【问题标题】:Change the Product dimensions order in WooCommerce single product pages在 WooCommerce 单个产品页面中更改产品尺寸顺序
【发布时间】:2017-08-21 10:55:19
【问题描述】:

我正在寻找一种过滤显示顺序或 woocommerce 产品尺寸(长度、宽度、高度)的方法。我想在宽度之前显示高度。

目前我使用自定义函数来过滤尺寸显示的位置:

function wsa_show_product_dimensions() {
    global $product;
    echo $product->list_attributes();
}

add_action( 'woocommerce_single_product_summary', 'wsa_show_product_dimensions', 25 );

我已修改 product-attributes.php 模板以格式化返回的 html:

<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
    <div class="c-details-row">
        <div class="c-details-col c-details-col_1">
          <span class="c-label-middle"><?php _e( 'Size', 'woocommerce' ) ?></span>
        </div>
        <div class="c-details-col c-details-col_2">
          <span class="c-label-middle"><?php echo esc_html( wc_format_dimensions( $product->get_dimensions( false ) ) ); ?></span>
        </div>
    </div>
<?php endif; ?>

我可以看到我需要过滤get_dimensions();函数但不知道如何对数组重新排序?

我看到 list_attributes(); 已被弃用,因此任何有关如何执行此操作的帮助 - 如果可能的话,使用新的 wc_display_product_attributes 方法会很棒。

【问题讨论】:

    标签: php wordpress woocommerce product dimensions


    【解决方案1】:

    首先,由于 WooCommerce 3 WC_Product list_attributes() 方法已被弃用,它已被 wc_display_product_attributes( $product ) 取代。

    要更改产品尺寸顺序,您需要先制作自己的函数:

    function get_product_dimensions_custom(){
        global $product;
    
        return  wc_format_dimensions( array(
            'length' => $product->get_length(),
            'height' => $product->get_height(),
            'width'  => $product->get_width(),
        ));
    }
    

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

    然后就可以在模板定制中使用了:

    <?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
        <div class="c-details-row">
            <div class="c-details-col c-details-col_1">
              <span class="c-label-middle"><?php _e( 'Size', 'woocommerce' ) ?></span>
            </div>
            <div class="c-details-col c-details-col_2">
              <span class="c-label-middle"><?php echo esc_html( get_product_dimensions_custom() ); ?></span>
            </div>
        </div>
    <?php endif; ?>
    

    woocommerce_single_product_summary 中完成您的自定义挂钩函数将是:

    add_action( 'woocommerce_single_product_summary', 'wsa_show_product_dimensions', 25 );
    function wsa_show_product_dimensions() {
        global $product;
    
        echo wc_display_product_attributes( $product );
    
    }
    

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

    此代码在 WooCommerce 3+ 上经过测试并且有效;

    【讨论】:

    • 太棒了@LoicTheAztec - 非常感谢。模板自定义 sn-p 有一个小错字('dimentions' 应该是 'dimensions')——除此之外,这绝对是完美的。非常感谢
    猜你喜欢
    • 2019-02-11
    • 2015-01-04
    • 1970-01-01
    • 2015-09-11
    • 2015-08-08
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多