You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce商品目录页:简单/可变产品数据展示函数开发需求

WooCommerce 目录版商品/变体信息展示函数

我刚好做过类似的需求,给你整理了一个完整的函数,完全适配你要的“目录版”页面场景——没有加购按钮和变体下拉,只清晰展示商品/变体的核心信息:

function wc_display_product_directory_info() {
    global $product;
    
    // 确保当前是WooCommerce产品对象
    if ( ! is_a( $product, 'WC_Product' ) ) {
        return '';
    }
    
    $output = '<div class="product-directory-info">';
    
    // 处理可变产品
    if ( $product->is_type( 'variable' ) ) {
        $output .= '<h3>' . esc_html__( '产品变体信息', 'woocommerce' ) . '</h3>';
        $output .= '<ul class="variation-list">';
        
        // 获取所有可用变体(排除缺货或不可见的)
        $variations = $product->get_available_variations();
        
        foreach ( $variations as $variation ) {
            $variation_obj = wc_get_product( $variation['variation_id'] );
            
            // 获取尺寸属性值(这里假设尺寸属性的slug是'pa_size',根据你的实际属性修改)
            $size = isset( $variation['attributes']['attribute_pa_size'] ) ? wc_attribute_label( 'pa_size' ) . ': ' . esc_html( $variation['attributes']['attribute_pa_size'] ) : '';
            
            // 获取常规价格
            $regular_price = $variation_obj->get_regular_price() ? wc_price( $variation_obj->get_regular_price() ) : esc_html__( '无定价', 'woocommerce' );
            
            // 获取促销价格
            $sale_price = $variation_obj->get_sale_price() ? wc_price( $variation_obj->get_sale_price() ) : '';
            
            // 获取库存信息
            $stock_status = $variation_obj->get_stock_status();
            if ( $stock_status === 'instock' ) {
                $stock = esc_html__( '有货', 'woocommerce' ) . ( $variation_obj->managing_stock() ? ' (' . $variation_obj->get_stock_quantity() . '件)' : '' );
            } else {
                $stock = esc_html__( '缺货', 'woocommerce' );
            }
            
            // 组装单条变体信息
            $output .= '<li class="variation-item">';
            if ( $size ) {
                $output .= '<div class="variation-size">' . $size . '</div>';
            }
            $output .= '<div class="variation-price">';
            $output .= '<span class="regular-price">' . $regular_price . '</span>';
            if ( $sale_price ) {
                $output .= '<span class="sale-price">' . $sale_price . '</span>';
            }
            $output .= '</div>';
            $output .= '<div class="variation-stock">' . $stock . '</div>';
            $output .= '</li>';
        }
        
        $output .= '</ul>';
    } 
    // 处理简单产品
    else if ( $product->is_type( 'simple' ) ) {
        $output .= '<h3>' . esc_html__( '产品详情', 'woocommerce' ) . '</h3>';
        $output .= '<div class="simple-product-info">';
        
        // 获取常规价格
        $regular_price = $product->get_regular_price() ? wc_price( $product->get_regular_price() ) : esc_html__( '无定价', 'woocommerce' );
        
        // 获取促销价格
        $sale_price = $product->get_sale_price() ? wc_price( $product->get_sale_price() ) : '';
        
        // 获取库存信息
        $stock_status = $product->get_stock_status();
        if ( $stock_status === 'instock' ) {
            $stock = esc_html__( '有货', 'woocommerce' ) . ( $product->managing_stock() ? ' (' . $product->get_stock_quantity() . '件)' : '' );
        } else {
            $stock = esc_html__( '缺货', 'woocommerce' );
        }
        
        $output .= '<div class="product-price">';
        $output .= '<span class="regular-price">' . $regular_price . '</span>';
        if ( $sale_price ) {
            $output .= '<span class="sale-price">' . $sale_price . '</span>';
        }
        $output .= '</div>';
        $output .= '<div class="product-stock">' . $stock . '</div>';
        $output .= '</div>';
    }
    
    $output .= '</div>';
    
    return $output;
}

关键说明:

  1. 属性适配:代码里默认尺寸属性的slug是pa_size,如果你的店铺里尺寸属性的slug不一样(比如size),记得修改两处attribute_pa_size和wc_attribute_label('pa_size')的部分。
  2. 样式自定义:你可以通过.product-directory-info、.variation-list、.regular-price这些类名添加CSS样式,让展示更贴合你的目录版页面设计。
  3. 使用方法:
    • 把这段代码放到你的主题functions.php文件里,或者自定义插件中。
    • 在你自定义的“目录版”商品页面模板里,找到需要展示信息的位置,调用函数:<?php echo wc_display_product_directory_info(); ?>
  4. 边缘情况处理:函数已经处理了无定价、无促销价、缺货、不管理库存等情况,确保展示不会出错。

如果你需要调整展示的字段或者样式,直接修改代码里的HTML结构和对应的逻辑就行,非常灵活。

内容的提问来源于stack exchange,提问作者Garconis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:04:32