给在 Woocommerce 上购买产品的用户的通知消息

问题描述

我能够向使用此挂钩在 woocommerce 上获得产品的用户显示通知消息。但我认为隐藏添加到购物车按钮会更好,因为有些用户可能仍然继续点击添加到购物车。在用户获得产品后,我尝试隐藏“添加到购物车”按钮。

请问我该如何实现?谢谢

// Woocommerce Product bought Once
add_filter('woocommerce_add_to_cart_validation','sd_bought_before_woocommerce_add_to_cart_validation',20,2);
function sd_bought_before_woocommerce_add_to_cart_validation($valid,$product_id){
 $current_user = wp_get_current_user();
 if ( wc_customer_bought_product( $current_user->user_email,$current_user->ID,$product_id)) {
 wc_add_notice( __( 'You can only get this free product once.Thanks','woocommerce' ),'error' );
 $valid = false;
 }
 return $valid;
}

解决方法

尝试使用 woocommerce_is_purchasable 过滤器挂钩隐藏添加到购物车按钮:

示例:

function sd_bought_before_woocommerce_add_to_cart_validation( $is_purchasable,$instance ){
    
    $current_user = wp_get_current_user();
    if ( wc_customer_bought_product( $current_user->user_email,$current_user->ID,$instance->get_id()) ) {
        $is_purchasable = false;
    }
    return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable','sd_bought_before_woocommerce_add_to_cart_validation',10,2 );

更多信息:http://hookr.io/filters/woocommerce_is_purchasable/