Uncaught Error: Call to a member function generate_cart_id() on null
Uncaught Error: Call to a member function generate_cart_id() on null
我有以下代码,在将产品添加到购物车时重定向到 WooCommerce 结帐页面,而不是重定向到购物车页面。效果很好。
// Immediately go to checkout when adding to cart.
add_filter( 'woocommerce_add_to_cart_redirect', function( $url ) {
return wc_get_checkout_url();
});
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if(
$product->get_sold_individually() // if individual product
&& !is_null($product_cart_id) // Added to try and understand the error I'm getting
// The next line causes a server error, but only in the edit view of the Homepage after clicking on "Update".
&& WC()->cart->find_product_in_cart( $product_cart_id ) // if already in the cart.
&& $product->is_purchasable()
) {
$add_to_cart_url = wc_get_checkout_url(); // Instead of adding it to cart, go to checkout.
}
return $add_to_cart_url;
}
但是在后端,当我编辑主页并单击“更新”时,出现指向第二个功能代码的服务器错误。
这是我在服务器日志中得到的:
PHP Fatal error: Uncaught Error: Call to a member function generate_cart_id() on null in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:11
Stack trace:
#0 /home/xxx/public_html/wp-includes/class-wp-hook.php(287): misha_fix_for_individual_products()
#1 /home/xxx/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters()
#2 /home/xxx/public_html/wp-content/plugins/learndash-woocommerce/learndash_woocommerce.php(1285): apply_filters()
#3 /home/xxx/public_html/wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php(398): WC_Product_Course->add_to_cart_url()
#4 /home/xxx/public_html/wp-includes/shortcodes.php(343): WC_Shortcodes::product_add_to_cart_url()
#5 [internal function]: do_shortcode_tag()
#6 /home/xxx/public_html/wp-includes/shortcodes.php in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 11
知道如何解决这个问题吗?
您可以使用以下方法来避免后端产生的错误:
add_filter( 'woocommerce_add_to_cart_redirect', 'wc_get_checkout_url' );
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
if( is_admin() || current_user_can('edit_pages') ) {
return $add_to_cart_url; // Exit if it's on admin
}
$cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if( $product->get_sold_individually() && $product->is_purchasable()
&& WC()->cart->find_product_in_cart( $cart_id ) ) {
$add_to_cart_url = wc_get_checkout_url();
}
return $add_to_cart_url;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该可以解决错误。
我有以下代码,在将产品添加到购物车时重定向到 WooCommerce 结帐页面,而不是重定向到购物车页面。效果很好。
// Immediately go to checkout when adding to cart.
add_filter( 'woocommerce_add_to_cart_redirect', function( $url ) {
return wc_get_checkout_url();
});
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if(
$product->get_sold_individually() // if individual product
&& !is_null($product_cart_id) // Added to try and understand the error I'm getting
// The next line causes a server error, but only in the edit view of the Homepage after clicking on "Update".
&& WC()->cart->find_product_in_cart( $product_cart_id ) // if already in the cart.
&& $product->is_purchasable()
) {
$add_to_cart_url = wc_get_checkout_url(); // Instead of adding it to cart, go to checkout.
}
return $add_to_cart_url;
}
但是在后端,当我编辑主页并单击“更新”时,出现指向第二个功能代码的服务器错误。
这是我在服务器日志中得到的:
PHP Fatal error: Uncaught Error: Call to a member function generate_cart_id() on null in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:11
Stack trace:
#0 /home/xxx/public_html/wp-includes/class-wp-hook.php(287): misha_fix_for_individual_products()
#1 /home/xxx/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters()
#2 /home/xxx/public_html/wp-content/plugins/learndash-woocommerce/learndash_woocommerce.php(1285): apply_filters()
#3 /home/xxx/public_html/wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php(398): WC_Product_Course->add_to_cart_url()
#4 /home/xxx/public_html/wp-includes/shortcodes.php(343): WC_Shortcodes::product_add_to_cart_url()
#5 [internal function]: do_shortcode_tag()
#6 /home/xxx/public_html/wp-includes/shortcodes.php in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 11
知道如何解决这个问题吗?
您可以使用以下方法来避免后端产生的错误:
add_filter( 'woocommerce_add_to_cart_redirect', 'wc_get_checkout_url' );
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
if( is_admin() || current_user_can('edit_pages') ) {
return $add_to_cart_url; // Exit if it's on admin
}
$cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if( $product->get_sold_individually() && $product->is_purchasable()
&& WC()->cart->find_product_in_cart( $cart_id ) ) {
$add_to_cart_url = wc_get_checkout_url();
}
return $add_to_cart_url;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该可以解决错误。