php - 在单独的行中显示 woocommerce 产品尺寸

标签 php wordpress woocommerce product dimensions

我找到了通过将 product-attributes.php 文件复制到我的子主题并替换来在单独的行中显示尺寸的方法:

<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
    <tr>
        <th><?php _e( 'Dimensions', 'woocommerce' ) ?></th>
        <td class="product_dimensions"><?php echo esc_html( wc_format_dimensions( $product->get_dimensions( false ) ) ); ?></td>
    </tr>
<?php endif; ?>

与:

<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
    <tr>
        <th>Length</th>
        <td class="product_dimensions"><?php echo $product->get_length() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
    <tr>
        <th>Width</th>
        <td class="product_dimensions"><?php echo $product->get_width() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
    <tr>
        <th>Height</th>
        <td class="product_dimensions"><?php echo $product->get_height() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
<?php endif; ?>

问题在于产品并不总是具有 3 个维度...... 然后它看起来像这样:

Screen Shot

如何只显示相关行?

最佳答案

更新:您可以通过以下方式为每个维度类型添加条件:

<?php

    // For non variable products (separated dimensions)
    if ( $display_dimensions && $product->has_dimensions() && ! $product->is_type('variable') ) :
        if ( ! empty( $product->get_length() ) ) { ?>
    <tr>
        <th>Length</th>
        <td class="product_dimensions"><?php echo $product->get_length() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
<?php   }
        if ( ! empty( $product->get_width() ) ) { ?>
    <tr>
        <th>Width</th>
        <td class="product_dimensions"><?php echo $product->get_width() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
<?php   }
        if ( ! empty( $product->get_height() ) ) { ?>
    <tr>
        <th>Height</th>
        <td class="product_dimensions"><?php echo $product->get_height() . get_option( 'woocommerce_dimension_unit' ); ?></td>
    </tr>
<?php

    // For variable products (we keep the default formatted dimensions)
    elseif ( $display_dimensions && $product->has_dimensions() && $product->is_type('variable') ) : ?>
    <tr>
        <th><?php _e( 'Dimensions', 'woocommerce' ) ?></th>
        <td class="product_dimensions"><?php echo esc_html( wc_format_dimensions( $product->get_dimensions( false ) ) ); ?></td>
    </tr>
<?php endif; ?>

经过测试并有效。

Note: This doesn't handle variable products that uses Javascript to update the formatted dimensions on selected product variations.

关于php - 在单独的行中显示 woocommerce 产品尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46317041/

相关文章:

php - 不存在的行上的 SQL 语法错误

php - 运费取决于卖家和客户之间的距离

php - 将生日字段添加到 WooCommerce 我的帐户和管理员用户页面

wordpress - WooCommerce PDF 发票和装箱单无法使用中文

php - 在 Woocommerce REST API 中按类别获取产品

php - 我如何在 PHP 中运行检查以查看我的 MYSQL 存储过程是否正确运行?

php - 如何查询 mysql 数据库中的变量,如果存在则创建另一个变量,如果不插入?

php - 如何在 php 中将重音字符转换为 html?

html - 3 列结构,其中 2 个外部列被拖动到屏幕底部

jquery - 尝试将多个 div 倾斜到不同的随机 Angular