WooCommerce在添加之前检查产品ID的库存

时间:2022-04-11 20:34:18

I have some custom code in my WooCommerce site that adds a product to the users cart. I already have it check the cart contents to make sure there is another product in the cart first, but I also want it to check that the product that is being added is in stock also...

我的WooCommerce网站上有一些自定义代码,可以将产品添加到用户购物车中。我已经检查了购物车的内容,以确保购物车中还有其他产品,但我也希望它检查正在添加的产品是否也有库存......

I can't work out the best way to do this. Would really appreciate if you can let me know what to add to the function to make it check the stock of "product_id" and if the stock is > 0. Here's my code:

我无法找到最好的方法来做到这一点。非常感谢您,如果您能告诉我要添加到该功能的内容,以便检查“product_id”的库存以及库存是否为0.这是我的代码:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 21576;
        $found = false;
        global $woocommerce;

        //check if product already in cart

    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
        // if product not found, add it
        if ( ! $found )
            WC()->cart->add_to_cart( $product_id );

        if (sizeof( WC()->cart->get_cart() ) == 1 && $found == true ) {
            $woocommerce->cart->empty_cart();
        }
    } 
}

}

}

2 个解决方案

#1


3  

You can use the WC_Product conditional method is_in_stock() directly in your if statement.

您可以直接在if语句中使用WC_Product条件方法is_in_stock()。

As $product is already a product object, we don't need anything else to make it work with WC_product methods.

由于$ product已经是产品对象,我们不需要任何其他东西来使其与WC_product方法一起使用。

Also instead of using sizeof( WC()->cart->get_cart() ) > 0 you can replace by the WC_cart method is_empty() this way ! WC()->cart->is_empty().

而不是使用sizeof(WC() - > cart-> get_cart())> 0,你可以用WC_cart方法替换is_empty()这样! WC() - > cart-> is_empty()。

Then you can also replace sizeof( WC()->cart->get_cart() ) == 1 using WC_cart get_cart_contents_count() method this way WC()->cart->get_cart_contents_count() == 1.

然后你也可以用WC_cart get_cart_contents_count()方法替换sizeof(WC() - > cart-> get_cart())== 1这样WC() - > cart-> get_cart_contents_count()== 1。

You don't need anymore the global woocommerce; declaration if you use WC()->cart instead of $woocommerce->cart with all WC_Cart methods

你不再需要全球的woocommerce;声明如果您使用WC() - >购物车而不是$ woocommerce->购物车与所有WC_Cart方法

Last thing:
May be is better to remove the concerned cart item, instead emptying the cart. So we will use remove_cart_item() method instead.
If is not convenient you can use empty_cart() method as in your original code…

最后一件事:可能更好的是删除相关的购物车项目,而不是清空购物车。所以我们将使用remove_cart_item()方法代替。如果不方便,可以使用原始代码中的empty_cart()方法...

So your code is going to be:

所以你的代码将是:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $targeted_product_id = 21576;
        $found = false;

        //check if product already in cart

        if ( ! WC()->cart->is_empty() ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product = $cart_item['data'];
                if ( $product->id == $targeted_product_id ) {
                    $found = true;
                    break; // We can break the loop
                }
            }
            // Update HERE (we generate a new product object $_product)
            $_product = wc_get_product( $targeted_product_id );
            // If product is not in the cart items AND IS IN STOCK  <===  <===  @@@@@@@
            if ( !$found && $_product->is_in_stock() ) {
                WC()->cart->add_to_cart( $targeted_product_id );
            } elseif ( $found && WC()->cart->get_cart_contents_count() == 1 ) {
                // Removing only this cart item
                WC()->cart->remove_cart_item( $cart_item_key );
                // WC()->cart->empty_cart();
            }
        }
    } 
}

This code is tested and works.

此代码经过测试并可以使用。

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

代码位于活动子主题(或主题)的function.php文件中。或者也可以在任何插件的php文件中。

#2


2  

This statement will return true if the stock quantity of the product is greater than 0.

如果产品的库存数量大于0,则此语句将返回true。

if( $product->get_stock_quantity() > 0 ) {
    WC()->cart->add_to_cart( $product->id );
}

I see you're setting the product ID yourself. If you need to get the product by the product it, in order to runt he code above, this may help you.

我看到你自己设置了产品ID。如果你需要通过产品获得产品,为了破坏上面的代码,这可能会对你有所帮助。

$product = wc_get_product( $product_id );

#1


3  

You can use the WC_Product conditional method is_in_stock() directly in your if statement.

您可以直接在if语句中使用WC_Product条件方法is_in_stock()。

As $product is already a product object, we don't need anything else to make it work with WC_product methods.

由于$ product已经是产品对象,我们不需要任何其他东西来使其与WC_product方法一起使用。

Also instead of using sizeof( WC()->cart->get_cart() ) > 0 you can replace by the WC_cart method is_empty() this way ! WC()->cart->is_empty().

而不是使用sizeof(WC() - > cart-> get_cart())> 0,你可以用WC_cart方法替换is_empty()这样! WC() - > cart-> is_empty()。

Then you can also replace sizeof( WC()->cart->get_cart() ) == 1 using WC_cart get_cart_contents_count() method this way WC()->cart->get_cart_contents_count() == 1.

然后你也可以用WC_cart get_cart_contents_count()方法替换sizeof(WC() - > cart-> get_cart())== 1这样WC() - > cart-> get_cart_contents_count()== 1。

You don't need anymore the global woocommerce; declaration if you use WC()->cart instead of $woocommerce->cart with all WC_Cart methods

你不再需要全球的woocommerce;声明如果您使用WC() - >购物车而不是$ woocommerce->购物车与所有WC_Cart方法

Last thing:
May be is better to remove the concerned cart item, instead emptying the cart. So we will use remove_cart_item() method instead.
If is not convenient you can use empty_cart() method as in your original code…

最后一件事:可能更好的是删除相关的购物车项目,而不是清空购物车。所以我们将使用remove_cart_item()方法代替。如果不方便,可以使用原始代码中的empty_cart()方法...

So your code is going to be:

所以你的代码将是:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $targeted_product_id = 21576;
        $found = false;

        //check if product already in cart

        if ( ! WC()->cart->is_empty() ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product = $cart_item['data'];
                if ( $product->id == $targeted_product_id ) {
                    $found = true;
                    break; // We can break the loop
                }
            }
            // Update HERE (we generate a new product object $_product)
            $_product = wc_get_product( $targeted_product_id );
            // If product is not in the cart items AND IS IN STOCK  <===  <===  @@@@@@@
            if ( !$found && $_product->is_in_stock() ) {
                WC()->cart->add_to_cart( $targeted_product_id );
            } elseif ( $found && WC()->cart->get_cart_contents_count() == 1 ) {
                // Removing only this cart item
                WC()->cart->remove_cart_item( $cart_item_key );
                // WC()->cart->empty_cart();
            }
        }
    } 
}

This code is tested and works.

此代码经过测试并可以使用。

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

代码位于活动子主题(或主题)的function.php文件中。或者也可以在任何插件的php文件中。

#2


2  

This statement will return true if the stock quantity of the product is greater than 0.

如果产品的库存数量大于0,则此语句将返回true。

if( $product->get_stock_quantity() > 0 ) {
    WC()->cart->add_to_cart( $product->id );
}

I see you're setting the product ID yourself. If you need to get the product by the product it, in order to runt he code above, this may help you.

我看到你自己设置了产品ID。如果你需要通过产品获得产品,为了破坏上面的代码,这可能会对你有所帮助。

$product = wc_get_product( $product_id );