提问者:小点点

如果产品在购物车中,则更改“添加到购物车”按钮文本,并在满足条件时重定向到购物车页面


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

  • 如果产品不在购物车中,则将产品添加到购物车并转到购物车页面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;
 
}

更新

下一个问题,仅剩下与购物车按钮文本挂钩相关的另一个问题。下面是此的堆栈跟踪:

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

共1个答案

匿名用户

下面的代码包含

  • 如果用户点击添加到购物车按钮,如果产品已经存在于购物车,然后重定向到购物车页面url.
  • 如果产品还没有在购物车中,那么将产品添加到购物车中并转到购物车页面URL。

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

WooCommerce

然后你得到

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