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

WooCommerce按变体属性添加下载文件的条件判断问题

问题核心原因定位

你当前代码出现Basic版本也输出WAV链接的异常,主要来自3个具体逻辑错误:

  • 条件判断语法错误:所有if分支里使用了单等号=做判断,这是PHP的赋值操作,并非相等比较。代码执行时会强行把$attribute_value赋值为对应字符串,导致两个分支的判断条件永远为真,所有输出行都会被追加。
  • 属性遍历逻辑冗余:代码会遍历变体的所有自定义属性,哪怕是和版本判断无关的颜色、尺寸类属性,也会进入判断流程,容易触发误判。
  • 重复代码逻辑:MP3下载链接的输出逻辑在两个分支里重复编写,没有做逻辑抽取,容易出现修改疏漏。
修正后的完整实现代码
add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 );
function add_new_rows( $total_rows, $order, $tax_display  ) {
    // 可根据实际属性名修改为你的网站版本属性系统标识
    $target_version_attribute = 'pa_download_version';
    $basic_value = 'Basic';
    $premium_value = 'Premium';

    foreach( $order->get_items() as $item_id => $item ){
        $product = $item->get_product();
        // 非变体产品直接跳过
        if ( ! $product->is_type('variation') ) continue;

        $title = $product->get_name();
        $parent_id = $product->get_parent_id();
        // 获取ACF字段,后续增加空值校验避免无效输出
        $mp3 = get_field('mp3', $parent_id);
        $wav = get_field('wav', $parent_id);

        // 获取变体已选属性列表
        $variation_attributes = $product->get_variation_attributes();
        foreach( $variation_attributes as $attribute_taxonomy => $term_slug ){
            $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
            // 跳过和版本判断无关的属性
            if ( $taxonomy !== $target_version_attribute ) continue;

            // 获取属性值,增加空值校验避免PHP报错
            $term_object = get_term_by( 'slug', $term_slug, $taxonomy );
            if ( ! $term_object ) continue;
            $attribute_value = $term_object->name;

            // 严格相等比较,避免赋值误触发
            if ( $attribute_value === $basic_value ) {
                // Basic版本仅输出MP3链接
                if ( $mp3 ) {
                    $total_rows['download_mp3_' . $item_id]['label'] = sprintf( __( '%s MP3 下载', 'woocommerce' ), $title );
                    $total_rows['download_mp3_' . $item_id]['value'] = sprintf( '<a href="%s" download>下载MP3文件</a>', esc_url( $mp3 ) );
                }
            }
            if ( $attribute_value === $premium_value ) {
                // Premium版本同时输出MP3 + WAV链接
                if ( $mp3 ) {
                    $total_rows['download_mp3_' . $item_id]['label'] = sprintf( __( '%s MP3 下载', 'woocommerce' ), $title );
                    $total_rows['download_mp3_' . $item_id]['value'] = sprintf( '<a href="%s" download>下载MP3文件</a>', esc_url( $mp3 ) );
                }
                if ( $wav ) {
                    $total_rows['download_wav_' . $item_id]['label'] = sprintf( __( '%s WAV 下载', 'woocommerce' ), $title );
                    $total_rows['download_wav_' . $item_id]['value'] = sprintf( '<a href="%s" download>下载WAV文件</a>', esc_url( $wav ) );
                }
            }
        }
    }
    return $total_rows;
}
额外优化说明
  • 代码里的$target_version_attribute需要替换成你网站实际用来区分版本的属性分类名,可在WooCommerce产品属性设置页查看对应属性的系统名称(一般是pa_开头的字符串)
  • 所有输出的链接都加了esc_url转义,避免XSS安全风险
  • 行键使用唯一的订单条目ID做标识,不会出现重复覆盖的问题
  • 增加了字段空值判断,当产品没有在ACF里上传对应文件时,不会输出空的无效链接

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.25 20:54:03