I'm looking for a solution when a user clicks on 'add to cart' button
- If product is not already in cart, then add product into cart and go to cart page url
- If product already exists in cart, change "add to cart" button text to "already in cart" and redirect to the cart page url
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
