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

时间:2021-05-20 02:23:53

标签: wordpress woocommerce 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());

1 个答案:

答案 0 :(得分:0)

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 );