【问题标题】:Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面
【发布时间】:2021-02-04 09:17:37
【问题描述】:

当用户点击“添加到购物车”按钮时,我正在寻找解决方案

  • 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面网址
  • 如果产品已存在于购物车中,请将“添加到购物车”按钮文本更改为“已在购物车中”并重定向到购物车页面网址

我已经有一些代码,但我在我的 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

我做了一些调查,正是这两个 sn-ps 代码导致了问题。基本上,如果产品已经在购物车中,则将添加到购物车的文本更改为已经在购物车中。如果产品已经在购物车中,另一个 sn-p 会将用户引导到购物车页面。

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

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:

    下面的代码包含

    • 如果用户点击“添加到购物车”按钮,如果产品已经存在于购物车中,则重定向到购物车页面网址。
    • 如果产品尚未在购物车中,则将产品添加到购物车并转到购物车页面网址。

    但是,代码工作有一个条件,你需要先在后端:

    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 );
    

    【讨论】:

    • 我的问题是解决错误,但修复错误后不能更改现有功能。上面的代码,如果我用我当前的代码替换它,这意味着“添加到购物车”功能发生变化,新产品无法添加到购物车。
    • 我现在已经调整了我的答案。您的要求的“问题”是最好在将产品添加到购物车之前检查是否满足条件。这可以通过woocommerce_add_to_cart_validation 过滤钩子实现。但是重定向,例如您可以使用woocommerce_add_to_cart_redirect 过滤钩子,仅在添加产品后才会发生。有点棘手。我的答案有效,但与此同时,我正在寻找更合适的解决方案,如果存在的话
    • 好的,代码运行良好,所以部分代码没问题。现在唯一的问题是添加到购物车文本更改的代码,如果产品在购物车中,按钮文本更改为已经在购物车中。狙击手的那部分正在抛出 php 错误,所以我认为只需要稍作调整。代码在做。功能很好,但是当我在仪表板中编辑课程页面时,它带有为产品显示的 php 严重错误消息
    • @BruceyBandit 我再次更新了我的答案,另一个与购物车按钮文本挂钩相关。如果在我的答案更改后仍然出现错误,请尝试在我更新的答案中替换:if ( WC()->cart ) { 与 if ( ! is_admin() && WC()->cart ) { 或 if ( ! is_null( WC()->cart ) ) {(已附加在我的答案中,但在评论标签中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2021-01-31
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多