donglian5309 2017-11-01 21:13
浏览 51

WooCommerce添加到购物车挂起

I'm setting up a woocommerce website, and I need that a product is purchasable by customers (users) that have a custom capability that equals the product brand name (the brand name is a product attribute). In this way i'm able to decide which user can buy products of each brand.

Searching online i found some code that I adapted for my needs:

// Disable purchase based on users capabilities
function check_brand_purchase_capability( $purchasable, $product ) {

// Don't run on parents of variations,
// this will already check variations separately            
if ( $product->is_type( 'variable' ) ) {
    return $purchasable;
}

global $product;
$brand_name = strtolower ( $product->get_attribute( 'pa_brand' ) );

// Check if user has the capability corresponding to the brand name
if ( current_user_can( $brand_name ) ) {
    $purchasable = true;
} else {
    $purchasable = false;
}

// Double-check for variations: if parent is not purchasable, then variation is not
if ( $purchasable && $product->is_type( 'variation' ) ) {
        $purchasable = $product->parent->is_purchasable();
}

return $purchasable;
}
add_filter( 'woocommerce_variation_is_purchasable', 
'check_brand_purchase_capability', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'check_brand_purchase_capability', 
10, 2 );

With this code I correctly enable/disable the "add to cart" button whether the user has the correct capability or not.

The problem is that when the button is enable and I click "add to cart", the button hangs and nothing happens. Does anyone knows why this is happening? Thanks for the help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?