Show Message in Woocommerce Cart only for specific product variation Attribute

核能气质少年 提交于 2019-12-13 00:37:31

问题


I am adding a custom message to the Cart page, but so that it only appears if an added product has a custom variation atribute selected.

After researching I came up to:

add_action( 'woocommerce_after_cart', 'wnd_after_cart' ); 
function wnd_after_cart() { 
    if($attribute_slug == 'no_review'){ 
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

But its not working. Does anyone know what am I doing wrong?

add_action( 'woocommerce_after_cart', 'wnd_after_cart' ); 
function wnd_after_cart() { 
    echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>'; 
}

Works very well, but I can't get the code to display the message only IF my custom attribute is selected.

Any help is appreciated…

Edit:

I set my product Atributes (example:Shirt size, Slug:'Shirt_Size') , and their variations within this atribute (Example: S (Slug:'Size_S'), M(Slug:'Size_M'), XL(Slug:'Size_XL') )

I'm trying to display the message when a specific Attribute Variation is selected (Example: The slug for S, 'Size_S')

I'm using clothes/shirt sizes since its a more common example to help illustrate.

In case I didn't explain very well, basically the code is searching for the attribute slug that you can see here in this video at 0:23 https://www.youtube.com/watch?v=QyMuq-WkV0o

But I'm trying to make it search for the slugs of the attribute variations that can be seen at 0:35 (the attribute attributes, or attribute variations, or attribute childs, I'm not sure what to name them)

@LoicTheAztec code seems to be working very well, but it's searching for the slug of the attribute (Shirt_Size), and not for the attribute variations shown previosly. When I set the code to find 'Shirt_size', it will disply the message, but when I set it to find 'Size_S', it stops working.

Did this make sense? Thank you for the attention and advice once again.


回答1:


There is some missing code like getting your $attribute_slug from variations in cart items:

add_action( 'woocommerce_after_cart', 'checking_variation_attributes_message' );
function checking_variation_attributes_message() {
    $found = false;
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
        if( ! $product->is_type('variation')){
            continue; // Jump to next cart item
        }
        $variation_attributes = $product->get_variation_attributes();
        foreach ( $variation_attributes as $variation_attribute => $term_slug ){
            $attribute_slug = str_replace('attribute_pa_', '', $variation_attribute);

            if( $attribute_slug == 'no_review' ){
                $found = true;
                break;
            }
        }
    }
    if($found){
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

Update: Or if you are looking for a product attribute term slug instead, use it this way:

add_action( 'woocommerce_after_cart', 'checking_variation_attributes_message' );
function checking_variation_attributes_message() {
    $found = false;
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
        if( ! $product->is_type('variation')){
            continue; // Jump to next cart item
        }
        $variation_attributes = $product->get_variation_attributes();
        foreach ( $variation_attributes as $variation_attribute => $term_slug ){
            $attribute_slug = str_replace('attribute_pa_', '', $variation_attribute);

            if( $term_slug == 'no_review' ){
                $found = true;
                break;
            }
        }
    }
    if($found){
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.



来源:https://stackoverflow.com/questions/49509774/show-message-in-woocommerce-cart-only-for-specific-product-variation-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!