Dokan 阻止商店关闭时添加到购物车

问题描述

插件开发人员给了我几行代码,但我似乎无法修复显示错误。我们试图通过完全移除按钮来防止用户在商店关闭时将商品添加到购物车。

add_filter( 'woocommerce_is_purchasable',function($show_cart) {
global $product;
$seller_id = get_post_field('post_author',$product->get_id());

//print_r($seller);

if(dokan_is_store_open( $seller_id )) {
    $show_cart = true;
} else {
    $show_cart = false;
}
return $show_cart;
},1000);

代码添加到子主题-wordpress 中的functions.PHP 中。出现的错误警告是

Fatal error: Uncaught Error: Call to a member function get_id() on null in /home/customer/www/usku.com.au/public_html/wp-content/themes/hestia-pro-child/functions.PHP:819 Stack trace: #0 /home/customer/www/usku.com.au/public_html/wp-includes/class-wp-hook.PHP(294): {closure}(true) #1 /home/customer/www/usku.com.au/public_html/wp-includes/plugin.PHP(212): WP_Hook->apply_filters(true,Array) #2 /home/customer/www/usku.com.au/public_html/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.PHP(1543): apply_filters('woocommerce_is_...',true,Object(WC_Product_Simple)) #3 /home/customer/www/usku.com.au/public_html/wp-content/plugins/woocommerce/includes/class-wc-cart-session.PHP(131): WC_Product->is_purchasable() #4 /home/customer/www/usku.com.au/public_html/wp-content/plugins/woocommerce/includes/class-wc-cart.PHP(602): WC_Cart_Session->get_cart_from_session() #5 /home/customer/www/usku.com.au/public_html/wp-content/plugins/woocommerce/includes/class-wc-cart.PHP(1478): WC_Cart->get_cart() #6 /home/customer/w in /home/customer/www/usku.com.au/public_html/wp-content/themes/hestia-pro-child/functions.PHP on line 819

819 行特指这一行: $seller_id = get_post_field('post_author',$product->get_id());

解决方法

woocommerce_is_purchasable 动作挂钩具有 WC_Product 对象作为您可以使用的第二个参数。

add_filter( 'woocommerce_is_purchasable',function( $show_cart,$product ) {

    $seller_id = get_post_field('post_author',$product->get_id());

    if(dokan_is_store_open( $seller_id )) {
        $show_cart = true;
    } else {
        $show_cart = false;
    }
    return $show_cart;
},10,2 );