简体   繁体   English

如果产品已在购物车中,我正在尝试更改单个页面和产品上的“添加到购物车”按钮中的链接和文本

[英]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.

相关问题 当产品已经在购物车中并且产品标签为Buynow时,将“添加到购物车”按钮文本更改为“添加” - Change Add to cart button text to Added when product already in cart and product tag is buynow woocommerce 添加到单个产品页面上的购物车按钮 - woocommerce add to cart button on single product page 如果我单击已在购物车中的产品的“添加到购物车”按钮,如何重定向到购物车页面 - How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart WooCommerce-产品已经在购物车中,更改“添加到购物车”文本为ajax - WooCommerce - Product already in cart, change “add to cart” text ajax 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce 如何在单个产品页面上添加 target=&quot;_blank&quot; 以添加到购物车按钮? - How to add target=“_blank” to add to cart button on Single Product page? 在单个产品页面/ Wordpress / Woocommerce中禁用Ajax添加到购物车按钮 - Disable ajax for add to cart button in single product page / Wordpress / Woocommerce Ajax 带有自定义字段的单个产品页面上的“添加到购物车”按钮 - Ajax add to cart button on single product page with custom field 添加到购物车按钮在 Woocommerce 的单个产品页面上不起作用 - Add to cart button not working on single product page in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM