php WooCommerce 产品在实际没有缺货时显示“缺货”消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24639288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:25:03  来源:igfitidea点击:

WooCommerce products showing “Out of stock” message when not actually out of stock

phpmysqlwordpresswoocommerce

提问by Ian

Having an issue with a client website where products are randomly displaying

客户网站出现问题,其中产品随机显示

"This product is currently out of stock and unavailable."

“该产品目前缺货,无法使用。”

The thing is that we don't have inventory tracking turned on, all products should be in stock all the time.

问题是我们没有打开库存跟踪,所有产品都应该一直有库存。

When I go into the WordPress admin and click the update button for the product (without changing anything) and then the message goes away and the "Add to Cart" button displays as it should. Unfortunately, the issue repeats itself on these items in the future and to fix it I have to update the product again (also clicking update in the WooCommerce settings temporarily solves the problem).

当我进入 WordPress 管理员并单击产品的更新按钮(不更改任何内容)时,该消息消失并且“添加到购物车”按钮按原样显示。不幸的是,这个问题将来会在这些项目上重复出现,为了解决这个问题,我必须再次更新产品(也点击 WooCommerce 设置中的更新暂时解决了这个问题)。

I need to find a way to fix this problem permanently so it doesn't revert itself.

我需要找到一种方法来永久解决这个问题,这样它就不会自行恢复。

Site Details:

网站详情:

  • Using WordPress 3.9.1
  • WooCommerce 2.1.12
  • PHP 5.3.10 (also tried locally w/ 5.5.10)
  • 使用 WordPress 3.9.1
  • WooCommerce 2.1.12
  • PHP 5.3.10(也在本地尝试过 5.5.10)

What I've tried

我试过的

  • Removing all plugins but WooCommerce
  • Changing themes (tried all standard WordPress themes and a few WooThemes themes to no avail)
  • Updated WooCommerce (it was v 2.1.5)
  • Manually inserted to wp_postmeta adding the meta key _manage_stockand set it to no(as observed in some of the working products we have)
  • Manually inserted to wp_postmeta adding the meta key '_stock' and set it to 0(as observed in some of the working products we have)
  • Compared the postmeta of the product before and after clicking the update button to observe if anything had changed that would cause the problem...nothing
  • No caching is turned on
  • Used XDebug to track the $available_variationsvariable in variable.phpand class-wc-ajax.phpand class-wc-product-variable.php.
  • 删除除 WooCommerce 之外的所有插件
  • 更改主题(尝试了所有标准的 WordPress 主题和一些 WooThemes 主题无济于事)
  • 更新了 WooCommerce(它是 v 2.1.5)
  • 手动插入 wp_postmeta 添加元键_manage_stock并将其设置为no(如在我们拥有的一些工作产品中所观察到的)
  • 手动插入 wp_postmeta 添加元键 '_stock' 并将其设置为0(如在我们拥有的一些工作产品中所观察到的)
  • 比较产品在点击更新按钮之前和之后的 postmeta 以观察是否有任何更改会导致问题......没有
  • 没有打开缓存
  • 使用 XDebug 来跟踪and 和 中的$available_variations变量。variable.phpclass-wc-ajax.phpclass-wc-product-variable.php

When debugging I noticed that in products that were working the $available_variationsvariable contained 2 arrays filled with product information, however on the products that didn't, it was an empty array (which results in the out of stock message being displayed as served by variable.php). Clicking the update button and then tracking the variable shows the information correctly.

在调试时,我注意到在工作的产品中,$available_variations变量包含 2 个填充了产品信息的数组,但是在没有的产品上,它是一个空数组(这导致缺货消息显示为variable.php)。单击更新按钮,然后跟踪变量会正确显示信息。

Honestly at this point I'm at a loss. I think the issue is somewhere in either of the classes, but I can't tell.

老实说,在这一点上我很茫然。我认为问题出在任何一个课程中,但我不知道。

Has anyone else had issues with this? Any way around this issue so I don't have to keep updating products for this client.

有没有其他人有这方面的问题?以任何方式解决此问题,因此我不必继续为该客户更新产品。

Link to product that is out of stock (as of posting) http://highlandsministriesonline.org/product/basement-tapes/resentment-in-the-church-135/

链接到缺货的产品(发布时)http://highlandsministriesonline.org/product/basement-tapes/resentment-in-the-church-135/

Link to product that is displaying correctly http://highlandsministriesonline.org/product/basement-tapes/bt0111-church-at-corinth-i/

链接到正确显示的产品http://highlandsministriesonline.org/product/basement-tapes/bt0111-church-at-corinth-i/

Error LogI added an error log to the product variation class. Heres the result:

错误日志我在产品变体类中添加了一个错误日志。结果如下:

For products with the "out of stock" message, nothing displays in the log.

对于带有“缺货”消息的产品,日志中不会显示任何内容。

An example of a product that function correctly:

功能正常的产品示例:

WC_Product_Variation Object
(
    [variation_id] => 1310
    [parent] => WC_Product_Variable Object
        (
            [children] => Array
                (
                    [0] => 1311
                    [1] => 1310
                )

            [total_stock] => 
            [id] => 1308
            [post] => WP_Post Object
                (
                    [ID] => 1308
                    [post_author] => 1
                    [post_date] => 2012-04-03 14:29:42
                    [post_date_gmt] => 2012-04-03 18:29:42
                    [post_content] => This audio presentation on homeschooling is a Basement Tape conversation on homeschooling.
<h2>What are The Basement Tapes?</h2>
<em>The Basement Tapes</em> are conversations between Dr. R.C. Sproul Jr., Rev. Laurence Windham, their friends and special guests, on subjects related to the “good life,” a simple, separate, and deliberate life, lived out for the glory of God and for the building of His Kingdom.

You might even say that the Basement Tapes serve to help us develop a Christian worldview. Started in 2002, we now have over #140 conversations on all areas of life that we are seeking to think biblically about so that we might take dominion over them for the King and His kingdom.

<strong>Format: Available in both CD and MP3</strong>

<strong>Length: Sixty minutes</strong>
                    [post_title] => Basement Tape #109: Homeschooling Redux
                    [post_excerpt] => It's been 108 weeks since we last talked about the subject of homeschooling our children. Our tykes have grown into teenagers. What have we learned in the past nine years? What has changed? What remains the same is that homeschooling means discipleship; passing down your most important convictions to your children and teaching them our holy religion.
                    [post_status] => publish
                    [comment_status] => open
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => bt0109-homeschooling-reducks
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2014-07-07 11:08:46
                    [post_modified_gmt] => 2014-07-07 15:08:46
                    [post_content_filtered] => 
                    [post_parent] => 0
                    [guid] => http://50.57.190.158/?post_type=product&#038;p=1308
                    [menu_order] => 0
                    [post_type] => product
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product_type] => variable
        )

    [variation_data] => Array
        (
            [attribute_pa_audio-options] => mp3-download
        )

    [variation_has_length] => 
    [variation_has_width] => 
    [variation_has_height] => 
    [variation_has_weight] => 
    [variation_has_stock] => 
    [variation_has_sku] => 1
    [variation_shipping_class] => 
    [variation_shipping_class_id] => 
    [variation_has_tax_class] => 
    [variation_has_downloadable_files] => 1
    [id] => 1308
    [post] => WP_Post Object
        (
            [ID] => 1308
            [post_author] => 1
            [post_date] => 2012-04-03 14:29:42
            [post_date_gmt] => 2012-04-03 18:29:42
            [post_content] => This audio presentation on homeschooling is a Basement Tape conversation on homeschooling.
<h2>What are The Basement Tapes?</h2>
<em>The Basement Tapes</em> are conversations between Dr. R.C. Sproul Jr., Rev. Laurence Windham, their friends and special guests, on subjects related to the “good life,” a simple, separate, and deliberate life, lived out for the glory of God and for the building of His Kingdom.

You might even say that the Basement Tapes serve to help us develop a Christian worldview. Started in 2002, we now have over #140 conversations on all areas of life that we are seeking to think biblically about so that we might take dominion over them for the King and His kingdom.

<strong>Format: Available in both CD and MP3</strong>

<strong>Length: Sixty minutes</strong>
            [post_title] => Basement Tape #109: Homeschooling Redux
            [post_excerpt] => It's been 108 weeks since we last talked about the subject of homeschooling our children. Our tykes have grown into teenagers. What have we learned in the past nine years? What has changed? What remains the same is that homeschooling means discipleship; passing down your most important convictions to your children and teaching them our holy religion.
            [post_status] => publish
            [comment_status] => open
            [ping_status] => closed
            [post_password] => 
            [post_name] => bt0109-homeschooling-reducks
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2014-07-07 11:08:46
            [post_modified_gmt] => 2014-07-07 15:08:46
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://50.57.190.158/?post_type=product&#038;p=1308
            [menu_order] => 0
            [post_type] => product
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [product_type] => variation
    [product_custom_fields] => Array
        (
            [_sku] => Array
                (
                    [0] => BT0109MP3
                )

            [_price] => Array
                (
                    [0] => 3
                )

            [_sale_price] => Array
                (
                    [0] => 
                )

            [_weight] => Array
                (
                    [0] => 
                )

            [_length] => Array
                (
                    [0] => 
                )

            [_width] => Array
                (
                    [0] => 
                )

            [_height] => Array
                (
                    [0] => 
                )

            [_stock] => Array
                (
                    [0] => 
                )

            [_thumbnail_id] => Array
                (
                    [0] => 0
                )

            [_virtual] => Array
                (
                    [0] => yes
                )

            [_downloadable] => Array
                (
                    [0] => yes
                )

            [_download_limit] => Array
                (
                    [0] => 
                )

            [_downloadable_files] => Array
                (
                    [0] => a:1:{s:32:"a0a87d43e69ed9de646ba479c252c9fb";a:2:{s:4:"name";s:29:"109-Homeschooling-Reducks.mp3";s:4:"file";s:113:"http://highlandsministriesonline.org/wp-content/uploads/woocommerce_uploads/2012/04/109-Homeschooling-Reducks.mp3";}}
                )

            [_regular_price] => Array
                (
                    [0] => 3
                )

            [_sale_price_dates_from] => Array
                (
                    [0] => 
                )

            [_sale_price_dates_to] => Array
                (
                    [0] => 
                )

            [_download_expiry] => Array
                (
                    [0] => 
                )

            [attribute_pa_audio-options] => Array
                (
                    [0] => mp3-download
                )

        )

    [sku] => BT0109MP3
    [downloadable_files] => a:1:{s:32:"a0a87d43e69ed9de646ba479c252c9fb";a:2:{s:4:"name";s:29:"109-Homeschooling-Reducks.mp3";s:4:"file";s:113:"http://highlandsministriesonline.org/wp-content/uploads/woocommerce_uploads/2012/04/109-Homeschooling-Reducks.mp3";}}
    [downloadable] => yes
    [virtual] => yes
    [sale_price_dates_from] => 
    [sale_price_dates_to] => 
    [price] => 3
    [regular_price] => 3
    [sale_price] => 
    [total_stock] => 0
    [dimensions] => 
)

Potential Next StepsMy next thought if I cant get this resolved is to write a script that will go through and update each product on the website on a nightly basis (since the temporary fix seems to work for at least 24 ours). Though if I can save some of my time I'd rather not if someone else has been able to solve this issue permanently.

潜在的后续步骤如果我不能解决这个问题,我的下一个想法是编写一个脚本,该脚本将每晚检查并更新网站上的每个产品(因为临时修复似乎对我们的至少 24 个产品有效)。虽然如果我可以节省一些时间,我宁愿不希望其他人能够永久解决这个问题。

Update as of October 23,2014

2014 年 10 月 23 日更新

This issue is still occurring despite attempting the answers below. I have created a logging plugin to hopefully help track the frequency of the out of stock messages and hopefully help correlate the message with any actions taken place on the website. It's currently customized to this specific website, but I'm working on a version to share with you all so that even if I can't find the answer, someone can.

尽管尝试了以下答案,但此问题仍然存在。我创建了一个日志插件,希望有助于跟踪缺货消息的频率,并希望有助于将消息与网站上发生的任何操作相关联。它目前是针对这个特定网站定制的,但我正在开发一个版本与大家分享,这样即使我找不到答案,也有人可以。

回答by Zach Russell

I had this same problem and I think I figured it out. I used the CSV Import/Export suite to export all of my variations. You'll notice that stock is set to 0when it should have a value of null.

我有同样的问题,我想我想通了。我使用 CSV 导入/导出套件导出我的所有变体。您会注意到 stock 设置为0它应该具有的值的时间null

You have two options here:

您在这里有两个选择:

  1. Go into each individual product, click the variations tab and expand each variation - unchecking the "manage stock" checkbox:enter image description here
  2. You can do a find-and-replace on the database substituting the 0 value for null.
  1. 进入每个单独的产品,单击变体选项卡并展开每个变体 - 取消选中“管理库存”复选框:在此处输入图片说明
  2. 您可以对数据库执行查找和替换操作,将 0 值替换为 null。

回答by pants

It's 2017 and I'm still seeing the OP's exact problem on a store running:

现在是 2017 年,我仍然在运行的商店中看到 OP 的确切问题:

  • WordPress 4.8.1
  • WooCommerce 3.1.2
  • PHP 5.6
  • tons of plugins and a janky theme
  • WordPress 4.8.1
  • WooCommerce 3.1.2
  • PHP 5.6
  • 大量的插件和简陋的主题

I don't have the time to genuinely fix the bug--whatever it is--but I made a workaround.

我没有时间真正修复错误——不管它是什么——但我做了一个解决方法。

The problem is that $productis missing everything variation related. There are no variation IDs, attributes, or prices. My workaround fixes up $productbefore it gets used in the templates.

问题是$product缺少与变化相关的所有内容。没有变体 ID、属性或价格。我的解决方法$product在模板中使用之前修复。

Step 1: Copy the price.php template into your theme. Navigate to your WooCommerce's price template at /wp-content/plugins/woocommerce/templates/single-product/price.php. Copy that into your theme at /wp-content/themes/my-theme/woocommerce/single-product/price.php. If you already have that file there, then you already have a custom price.php template and you can skip this step.

第 1 步:将 price.php 模板复制到您的主题中。导航到您的 WooCommerce 价格模板/wp-content/plugins/woocommerce/templates/single-product/price.php。将其复制到您的主题中/wp-content/themes/my-theme/woocommerce/single-product/price.php。如果您已经在那里有了那个文件,那么您已经有了一个自定义 price.php 模板,您可以跳过这一步。

Documentation on how WooCommerce custom templates work: https://docs.woocommerce.com/document/template-structure/

关于 WooCommerce 自定义模板如何工作的文档:https://docs.woocommerce.com/document/template-structure/

Step 2: Edit that price.phpyou just put in your theme. Below global $product;paste this:

第 2 步:编辑price.php您刚刚放入主题的内容。下面global $product;粘贴这个:

if ( $product->is_type( 'variable' ) ) {
  // this is a variable product, so let's ensure $product is set up correctly

  // force the product to sync with its variations
  $product->sync( $product->id );
  // update $product with the synced product
  $pf = new WC_Product_Factory();
  $product = $pf->get_product( $product->id );
  // update utility variables used in the variable.php template
  $available_variations = $product->get_available_variations();
  $attributes = $product->get_variation_attributes();
}

I had some caching issues that made it appear as though it wasn't working at first. To test if it's working you can drop

我有一些缓存问题,使它看起来好像一开始不起作用。要测试它是否有效,您可以删除

echo '<pre style="display: none;">';
print_r( $product );
echo '</pre>';

at the top and bottom of the if. Now you can inspect the page, and just above the price will be the two hidden <pre>containing $productbefore and after fixing.

在 的顶部和底部if。现在您可以检查页面,价格正上方将是修复前后的两个隐藏<pre>包含$product

This works by using the sync()function to force the product to grab all the missing variation info, and then updates variables for use in templates. I believe the price is the earliest any of the variation data gets used in the templates, but this snippet could easily go higher up in the template chain if needed.

这通过使用该sync()函数强制产品获取所有缺失的变体信息,然后更新用于模板的变量来实现。我相信价格是在模板中最早使用的任何变化数据,但如果需要,这个片段可以很容易地在模板链中上升到更高的位置。

Edit: The above fix only works for WooCommerce 3.x. I did create a fix for WC 2.x but it is so huge and gross I'm not going to post it. If you still have a 2.x store you need to look into some sort of overhaul that will allow you to get on 3.x.

编辑:上述修复仅适用于 WooCommerce 3.x。我确实为 WC 2.x 创建了一个修复程序,但它是如此庞大和粗暴,我不打算发布它。如果您仍然拥有 2.x 商店,则需要进行某种检修,以便您能够使用 3.x。

回答by Joe

I was getting this same issue with a product I had newly created. But for me the answer was more simple.

我新创建的产品也遇到了同样的问题。但对我来说,答案更简单。

I needed to enter in a “Regular Price” for every variation.
https://wordpress.org/support/topic/this-product-is-currently-out-of-stock-and-unavailable-4/

我需要为每个变化输入一个“正常价格”。
https://wordpress.org/support/topic/this-product-is-currently-out-of-stock-and-unavailable-4/

I had created my attributes previously.

我之前已经创建了我的属性。

Under Variations, for my product, I had to make sure “Add variation” was selected. Then I clicked the “Go” button.

在变体下,对于我的产品,我必须确保选择了“添加变体”。然后我点击了“开始”按钮。

I needed to create a variation for each of the different choices the attribute could have.

我需要为属性可能具有的每个不同选择创建一个变体。

So for my Color Options I had to make three different variations (one for each choice).
Then I needed to click the blue “Expand” text, and make sure each variation had a Regular price.

所以对于我的颜色选项,我必须做出三种不同的变化(每个选择一种)。
然后我需要单击蓝色的“展开”文本,并确保每个变体都有一个常规价格

Then it worked. enter image description here

然后它起作用了。 在此处输入图片说明

(This was helpful too: https://docs.woocommerce.com/document/variable-product/)

(这也很有帮助:https: //docs.woocommerce.com/document/variable-product/

回答by James Ryven Valeii

Edit: My problem recurred - items became outofstock again after a time. In searching around, I found that there are more entries in the wp_postmeta table to be concerned with thanks to this post. Below, I've added 2 additional update functions that should help make the fix last forever.

编辑:我的问题再次出现 - 一段时间后物品再次缺货。在四处搜索时,我发现 wp_postmeta 表中有更多条目需要关注,这要归功于这篇文章。下面,我添加了 2 个额外的更新功能,它们应该有助于使修复永远持续下去。

One cause of this problem is that the stock status may be recorded as "outofstock" in the database, even while manage inventory is disabled.

此问题的一个原因是库存状态可能会在数据库中记录为“缺货”,即使管理库存被禁用。

You can check if this is your problem in phpMyAdmin by running:

您可以通过运行以下命令在 phpMyAdmin 中检查这是否是您的问题:

SELECT * FROM `wp_postmeta` WHERE `meta_key` = '_stock_status'

If you see that items are "outofstock" but are expected to be "instock," you can change them manually. Or, you can run:

如果您看到商品“缺货”但预计“有货”,您可以手动更改它们。或者,您可以运行:

UPDATE `wp_postmeta` SET `meta_value` = 'instock' WHERE `meta_key` = '_stock_status';
UPDATE `wp_postmeta` SET `meta_value` = '999999999' WHERE `meta_key` = '_stock';
UPDATE `wp_postmeta` SET `meta_value` = 'no' WHERE `meta_key` = '_manage_stock';

回答by Carfire

I got the same error, and I updated max_input_vars = 30000 on php.ini and it works fine. I'm on Siteground Shared hosting,

我遇到了同样的错误,我在 php.ini 上更新了 max_input_vars = 30000 并且它工作正常。我在 Siteground 共享主机上,

回答by DPC

It could be a problem of the product. Try deleting and re-adding the product or a reinstall.

可能是产品的问题。尝试删除并重新添加产品或重新安装。

回答by Pablo S G Pacheco

I am experiencing the same problem and I think i've found the reason. It's related to the sync_stock_statusstatic function of WC_Product_VariableClass.

我遇到了同样的问题,我想我已经找到了原因。它与WC_Product_Variable类的sync_stock_status静态函数有关。

You can fix a variable product stock status running this in your functions.php: add_action('wp',function(){ $productId = 639; WC_Product_Variable::sync_stock_status(639); });

您可以修复在您的functions.php 中运行它的可变产品库存状态: add_action('wp',function(){ $productId = 639; WC_Product_Variable::sync_stock_status(639); });

Of course this isn't a good solution because it only fixes one product at one particular moment.

当然,这不是一个好的解决方案,因为它只在特定时刻修复一种产品。

So the problem seems to be running this function at the right moment.

所以问题似乎是在正确的时间运行这个函数。

The interesting thing is i've seen through the code and i found out that the quick edit savecalls this function and fixes the product also!

有趣的是我已经看透了代码,我发现快速编辑保存调用了这个函数并修复了产品!

Other interesting thing is it seems that the woocommerce team may have fixed it for good, because i've found out through this committhat they are calling this function now when the product is updated.

另一个有趣的事情是,woocommerce 团队似乎已经修复了它,因为我通过这个提交发现他们现在在产品更新时正在调用这个函数。

回答by O-mkar

Try deleting the product and create a same product and try i had a same problem and it worked for me. even try re-installing the WooCommerce 2.1.12

尝试删除产品并创建相同的产品并尝试我遇到了同样的问题并且它对我有用。甚至尝试重新安装 WooCommerce 2.1.12