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

Add variable product downloadable files based on variation attributes in WooCommerce

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

动态内容将通过在管理端添加到产品的自定义文件输入字段 (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;
}

导致...

如您所见,它目前显示了所有正确的 link 和标签。但是测试项目 1 应该只提供 MP3 link,因为它是基本产品,而带有'Premium' 像第 2 项一样选择将提供 MP3 和 WAV。

我认为我的问题是 if 语句仅检查 attributes/variables 是否存在,而不检查它们是否实际上是活动的。

如有任何帮助,我们将不胜感激。

主要问题来自您使用 = (set a value) 而不是 ===== (compare a value).[=16= 的 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;
}

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