如果产品在购物车中,则更改 "add to cart" 按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面
Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce
我正在寻找用户单击 'add to cart' 按钮时的解决方案
- 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面 url
- 如果产品已存在于购物车中,将“添加到购物车”按钮文本更改为“已在购物车中”并重定向到购物车页面 url
我已经有了一些代码,但我在我的 WordPress 块中收到一个严重错误,只是需要一些关于它可能是什么的指导。
该代码在前端运行良好,但当我尝试在 wordpress 中编辑页面时,精选产品块指出存在错误。
当我调试时,错误状态为:
[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
我做了一些调查,正是这两段代码导致了这个问题。如果产品已经在购物车中,基本上将添加到购物车文本更改为已经在购物车中。如果产品已在购物车中,另一个代码段会将用户定向到购物车页面。
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;
}
更新
1 期已下,仅剩另一期与购物车按钮文本挂钩相关。下面是堆栈跟踪:
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
下面的代码包含
- 如果用户点击 'add to cart' 按钮,如果购物车中已经存在产品,则重定向到购物车页面 url。
- 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面 url。
但是,代码运行有一个条件,您首先需要在后端:
WooCommerce
> Settings
> Products
> Display
> Disable the checkbox: “Enable AJAX add to cart buttons on archives” option
(见图)
然后你得到
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 );
我正在寻找用户单击 'add to cart' 按钮时的解决方案
- 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面 url
- 如果产品已存在于购物车中,将“添加到购物车”按钮文本更改为“已在购物车中”并重定向到购物车页面 url
我已经有了一些代码,但我在我的 WordPress 块中收到一个严重错误,只是需要一些关于它可能是什么的指导。
该代码在前端运行良好,但当我尝试在 wordpress 中编辑页面时,精选产品块指出存在错误。
当我调试时,错误状态为:
[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
我做了一些调查,正是这两段代码导致了这个问题。如果产品已经在购物车中,基本上将添加到购物车文本更改为已经在购物车中。如果产品已在购物车中,另一个代码段会将用户定向到购物车页面。
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;
}
更新
1 期已下,仅剩另一期与购物车按钮文本挂钩相关。下面是堆栈跟踪:
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
下面的代码包含
- 如果用户点击 'add to cart' 按钮,如果购物车中已经存在产品,则重定向到购物车页面 url。
- 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面 url。
但是,代码运行有一个条件,您首先需要在后端:
WooCommerce
> Settings
> Products
> Display
> Disable the checkbox: “Enable AJAX add to cart buttons on archives” option
(见图)
然后你得到
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 );