I'm looking for a solution when a user clicks on 'add to cart' button
I already have some code but I am receiving a critical error in my WordPress block and just needed some guidance on what it could be.
The code is working great in front end, but when I try to edit a page in wordpress, the hand-picked products block is stating there is an error.
When I debug, the error states:
[21-Oct-2020 12:20:04 UTC] PHP Fatal error: Uncaught Error: Call to a member function find_product_in_cart() on null in /home4/metis/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:4
Stack trace:
#0 /home4/metis/public_html/staging/4326/wp-includes/class-wp-hook.php(287): redirect_product_exist_in_cart('?add-to-cart=43...', Object(WC_Product_Simple))
#1 /home4/metis/public_html/staging/4326/wp-includes/plugin.php(206): WP_Hook->apply_filters('?add-to-cart=43...', Array)
#2 /home4/metis/public_html/staging/4326/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(51): apply_filters('woocommerce_pro...', '?add-to-cart=43...', Object(WC_Product_Simple))
#3 /home4/metis/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(471): WC_Product_Simple->add_to_cart_url()
#4 /home4/metis/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(446): Automattic\WooCommer in /home4/metis/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 4
I did some investigation and it's these two snippets of code that is causing the issue. Basically one changes the add to cart text to already in cart if product is already in cart. The other snippet directs user to cart page if product already in cart.
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_add_to_cart_text', 10, 2 );
function woocommerce_custom_add_to_cart_text( $add_to_cart_text, $product ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id in cart
$_product_id = $cart_item['product_id'];
// Compare
if ( $product->get_id() == $_product_id ) {
// Change text
$add_to_cart_text = 'Already in cart';
break;
}
}
}
return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_url', 'redirect_product_exist_in_cart', 10, 2 );
function redirect_product_exist_in_cart( $add_to_cart_url, $product ){
if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->id ) ) // if in the cart
&& $product->is_purchasable() // we also need these two conditions
&& $product->is_in_stock() ) {
$add_to_cart_url = wc_get_cart_url();
}
return $add_to_cart_url;
}
UPDATE
1 issue down, just the other one remaining related to the cart button text hook. Below is the stack trace for this:
Stack trace:
#0 /home4/metis/public_html/staging/4326/wp-includes/class-wp-hook.php(287): woocommerce_custom_add_to_cart_text('Add to cart', Object(WC_Product_Simple))
#1 /home4/metis/public_html/staging/4326/wp-includes/plugin.php(206): WP_Hook->apply_filters('Add to cart', Array)
#2 /home4/metis/public_html/staging/4326/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', 'Add to cart', Object(WC_Product_Simple))
#3 /home4/metis/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(473): WC_Product_Simple->add_to_cart_text()
#4 /home4/metis/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(446): Automattic\WooCommerce\Blocks\BlockTypes\Abstra in /home4/metis/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 7
The code below contains
However, there is one condition for the code to work, you need first in backend:
WooCommerce
> Settings
> Products
> Display
> Disable the checkbox: “Enable AJAX add to cart buttons on archives” option
(See image)
Then you get
function woocommerce_custom_add_to_cart_text( $add_to_cart_text, $product ) {
// WC Cart
if ( WC()->cart ) {
// If this doesn't fix the error, other things you could try (replace the above if condition with 1 of the following)
//if ( ! is_admin() && WC()->cart ) {
//if ( ! is_null( WC()->cart ) ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id in cart
$_product_id = $cart_item['product_id'];
// Compare
if ( $product->get_id() == $_product_id ) {
// Change text
$add_to_cart_text = 'Already in cart';
break;
}
}
}
}
return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_add_to_cart_text', 10, 2 );
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Set variable
$in_cart = false;
// Loop
foreach( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['data']->get_id() == $product_id ) {
$in_cart = true;
break;
}
}
// Get cart url
$cart_url = wc_get_cart_url();
// True
if ( $in_cart ) {
wp_safe_redirect( $cart_url );
exit();
} else {
// Add product to cart
WC()->cart->add_to_cart( $product_id, $quantity );
wp_safe_redirect( $cart_url );
exit();
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );