[英]I am trying to change links & text from the Add to Cart button on a single page and product if the product is already in the basket
I am trying to change links & text from the Add to Cart button on a single page and product if the product is already in the basket.如果产品已在购物车中,我正在尝试更改单个页面和产品上的“添加到购物车”按钮中的链接和文本。 I added this code in my theme
我在我的主题中添加了此代码
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "Click View Cart";
}
return $text;
}
add_action( 'woocommerce_simple_add_to_cart', 'wc_simple_add_to_cart' );
function wc_simple_add_to_cart(){
global $product;
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
echo sprintf( '<form class="cart"><a href="%s" class="single_add_to_cart_button button alt">%s</a></form>', wc_get_cart_url(), __( 'Click View Cart', 'woocommerce' ) );
}
}
add_filter('woocommerce_product_add_to_cart_url', 'wc_product_add_to_cart_url', 10, 2 );
function wc_product_add_to_cart_url( $url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && is_shop() ) {
$url = wc_get_cart_url();
}
return $url;
}
and it works well too, So the problem is, sometimes when I publish something from wp-admin, sometimes there are site errors, but the error is only a few moments, when I refresh it to normal again, I received an email notification,它也运行良好,所以问题是,有时当我从 wp-admin 发布内容时,有时会出现站点错误,但错误只是片刻,当我再次将其刷新为正常时,我收到了电子邮件通知,
Error Details
=============
An error of type E_ERROR was caused in line 494 of the file /home/u920807414/domains/hbfonts.com/public_html/wp-content/themes/dokan-theme-develop/functions.php. Error message: Uncaught Error: Call to a member function generate_cart_id() on null in /home/u920807414/domains/hbfonts.com/public_html/wp-content/themes/dokan-theme-develop/functions.php:494
Stack trace:
#0 /home/u920807414/domains/hbfonts.com/public_html/wp-includes/class-wp-hook.php(288): wc_product_add_to_cart_url('?add-to-cart=14...', Object(WC_Product_Simple))
#1 /home/u920807414/domains/hbfonts.com/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters('?add-to-cart=14...', Array)
#2 /home/u920807414/domains/hbfonts.com/public_html/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(51): apply_filters('woocommerce_pro...', '?add-to-cart=14...', Object(WC_Product_Simple))
#3 /home/u920807414/domains/hbfonts.com/public_html/wp-content/plugins/woocommerce/templates/loop/add-to-cart.php(26): WC_Product_Simple->add_to_cart_url()
#4 /home/u920807414/domains/hbfonts.com/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(249): include('/home/u92080741...')
#5 /home/u920807414/d
can anyone help me thank you谁能帮帮我谢谢
Use this and you have to add the code to stop ajax call.使用这个,你必须添加代码来停止 ajax 调用。 After clicking button for first time it will slide to top.
第一次点击按钮后,它会滑到顶部。
add_action( 'woocommerce_simple_add_to_cart', 'wc_simple_add_to_cart' );
function wc_simple_add_to_cart(){
global $product;
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
$url = 'http://example.com'; // add your url here
if ( $in_cart ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
echo sprintf( '<form class="cart"><a href="%s" class="single_add_to_cart_button button alt">Button Title</a></form>', $url, __( 'View Cart', 'woocommerce' ) );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.