Php 根据WooCommerce中的变量属性添加变量产品可下载文件

Php 根据WooCommerce中的变量属性添加变量产品可下载文件,php,wordpress,if-statement,woocommerce,hook-woocommerce,Php,Wordpress,If Statement,Woocommerce,Hook Woocommerce,我正在尝试检查购物车中是否存在产品变体,并根据存在的可变产品向电子邮件和感谢页面添加动态内容 动态内容将通过添加到产品管理端的自定义文件输入字段(ACF)生成 我已成功地将以下代码组合在一起: add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 ); function add_new_rows( $total_rows, $order, $tax_display ) { $domain = 'wo

我正在尝试检查购物车中是否存在产品变体,并根据存在的可变产品向电子邮件和感谢页面添加动态内容

动态内容将通过添加到产品管理端的自定义文件输入字段(ACF)生成

我已成功地将以下代码组合在一起:

add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 );
function add_new_rows( $total_rows, $order, $tax_display  ) {
    $domain = 'woocommerce'; // The text domain (for translations)
    $count = 1;
    foreach( $order->get_items() as $item_id => $item ){
        
        $variation_id = false;
        $variation_id = $item->get_variation_id();

        //Product Info
        $product = $item->get_product();
        $title=$product->get_title();
        $parent_id=$product->get_parent_id();
        $mp3 = get_field('mp3', $parent_id);
        $wav = get_field('wav', $parent_id);
            
            if($product->is_type('variation')){
                // Get the variation attributes
                $variation_attributes = $product->get_variation_attributes();
                // Loop through each selected attributes
                foreach($variation_attributes as $attribute_taxonomy => $term_slug){
                    $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
                    // The name of the attribute
                    $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
                    // The term name (or value) for this attribute
                    $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                    if ($attribute_value = 'Basic' ){
                        // Return Basic output
                        $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                        $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                    }
                    if ($attribute_value = 'Premium' ){
                        // Return Basic AND Premium output
                        $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                        $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                        $total_rows['Premium '.$count]['label'] = __( "$title WAV", 'woocommerce' ); // The row shipping label
                        $total_rows['Premium '.$count]['value'] = "<a href='$wav' download>Download Wav</a>";
                    }
                }
            }
     $count += 1;
    }
    return $total_rows;
}
add_filter('woocommerce_get_order_item_total','add_new_rows',10,3);
函数add_new_rows($total_rows,$order,$tax_display){
$domain='woocommerce';//文本域(用于翻译)
$count=1;
foreach($order->get\u items()作为$item\u id=>$item){
$variation_id=false;
$variation_id=$item->get_variation_id();
//产品信息
$product=$item->get_product();
$title=$product->get_title();
$parent_id=$product->get_parent_id();
$mp3=获取字段($mp3',$parent\u id);
$wav=get_字段('wav',$parent_id);
如果($product->is_类型('VARIANCE')){
//获取变体属性
$variation_attributes=$product->get_variation_attributes();
//循环遍历每个选定属性
foreach($variation\u attributes as$attribute\u taxonomy=>$term\u slug){
$taxonomy=str_replace('attribute_',''.$attribute_taxonomy);
//属性的名称
$attribute\u name=get\u taxonomy($taxonomy)->labels->singular\u name;
//此属性的术语名称(或值)
$attribute_value=get_term_by('slug',$term_slug,$taxonomy)->name;
如果($attribute_value='Basic'){
//返回基本输出
$total_rows['Basic.$count]['label']=\uu($title MP3,'woocommerce');//行装运标签
$total_行['Basic.$count]['value']=”;
}
如果($attribute_value='Premium'){
//返回基本和高级输出
$total_rows['Basic.$count]['label']=\uu($title MP3,'woocommerce');//行装运标签
$total_行['Basic.$count]['value']=”;
$total_行['Premium'.$count]['label']=\uuu($title WAV','woocommerce');//行发货标签
$total_行['Premium'.$count]['value']=”;
}
}
}
$count+=1;
}
返回$total_行;
}
导致

正如您所看到的,它当前正在显示所有正确的链接和标签。。但是,测试项目1应仅提供MP3链接,因为它是一种基本产品,而选择“高级”的产品(如项目2)将提供MP3和WAV

我认为我的问题是if语句只检查属性/变量是否存在,而不是它们是否实际上是活动的


任何帮助都将不胜感激。

主要问题来自您的if语句,其中您使用的是
=
(设置值)
,而不是
=
===
(比较值)

因此,请使用以下重新访问和简化的代码:

add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 );
function add_new_rows( $total_rows, $order, $tax_display  ) {
    $domain = 'woocommerce'; // The text domain (for translations)
    $count  = 0;

    foreach( $order->get_items() as $item ){
        if ( $item->get_variation_id() > 0 ) {
            $product = $item->get_product();
            $title   = $product->get_name();

            $mp3 = get_field('mp3', $item->get_product_id());
            $wav = get_field('wav', $item->get_product_id());

            $count++;

            // Loop through each selected attributes
            foreach( $product->get_variation_attributes() as $attribute => $term_slug ){
                if ( 'Basic' === $term_slug ) {
                    // Return Basic output
                    $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                    $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                }
                elseif ( 'Premium' === $term_slug ) {
                    // Return Basic AND Premium output
                    $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                    $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                    $total_rows['Premium '.$count]['label'] = __( "$title WAV", 'woocommerce' ); // The row shipping label
                    $total_rows['Premium '.$count]['value'] = "<a href='$wav' download>Download Wav</a>";
                }
            }
        }
    }
    return $total_rows;
}
add_filter('woocommerce_get_order_item_total','add_new_rows',10,3);
函数add_new_rows($total_rows,$order,$tax_display){
$domain='woocommerce';//文本域(用于翻译)
$count=0;
foreach($order->get_items()作为$item){
如果($item->get\u variation\u id()>0){
$product=$item->get_product();
$title=$product->get_name();
$mp3=get_字段('mp3',$item->get_product_id());
$wav=get_字段('wav',$item->get_product_id());
$count++;
//循环遍历每个选定属性
foreach($product->get_variation_attributes()作为$attribute=>$term_slug){
如果('Basic'==$term\u slug){
//返回基本输出
$total_rows['Basic.$count]['label']=\uu($title MP3,'woocommerce');//行装运标签
$total_行['Basic.$count]['value']=”;
}
elseif('Premium'==$term\u slug){
//返回基本和高级输出
$total_rows['Basic.$count]['label']=\uu($title MP3,'woocommerce');//行装运标签
$total_行['Basic.$count]['value']=”;
$total_行['Premium'.$count]['label']=\uuu($title WAV','woocommerce');//行发货标签
$total_行['Premium'.$count]['value']=”;
}
}
}
}
返回$total_行;
}

代码进入活动子主题(或活动主题)的functions.php文件。它应该能更好地工作。

不幸的是,没有一个信息用此代码添加到订单中。它只显示产品和总计/小计。似乎任何带有多个“=”的内容都不会返回任何内容。@SBM我已经更新了答案,但它不应该解决问题,因为问题似乎与属性有关……请尝试在我的代码
echo“”中取消注释。打印($product->获取变量属性(),true)。“”用于测试并检查您的属性项是否在循环中…然后告诉我您有什么。它返回。。Array([attribute_lease]=>Basic)Array([attribute_lease]=>Premium)能够在将变体的术语首字母大写后使其工作,因为它区分大小写。非常感谢你!