Woocommerce 如果选择了特定属性,则禁止用户添加到购物车

Woocommerce 如果选择了特定属性,则禁止用户添加到购物车,woocommerce,Woocommerce,在Woocommerce中,如果选择了特定属性,我希望阻止用户添加到购物车 属性名为“生产时间”,我们要从采购中禁用的特定值为“紧急” 以下代码来自此链接- 然而,它只适用于数字。当属性是文本时,它不起作用 add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 ); function conditional_variation_is_purchasabl

在Woocommerce中,如果选择了特定属性,我希望阻止用户添加到购物车

属性名为“生产时间”,我们要从采购中禁用的特定值为“紧急”

以下代码来自此链接-

然而,它只适用于数字。当属性是文本时,它不起作用

add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );

function conditional_variation_is_purchasable( $purchasable, $product ) {

    ## ---- Your settings ---- ##

    $taxonomy  = 'production-time';
    $term_name =  'urgent';

    ## ---- The active code ---- ##

    $found = false;

    // Loop through all product attributes in the variation
    foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
        $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
        $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
        // Searching for attribute 'pa_size' with value 'XL'
        if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
            $found = true;
            break;
        }
    }

    if( $found )
        $purchasable = false;

    return $purchasable;
}