【问题标题】:WooCommerce products showing “Out of stock” message when not actually out of stockWooCommerce 产品在实际上没有缺货时显示“缺货”消息
【发布时间】:2014-08-29 15:06:17
【问题描述】:

遇到产品随机显示的客户网站问题

“此产品目前缺货且不可用。”

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

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

我需要找到一种方法来永久解决此问题,以免它自行恢复。

网站详情:

  • 使用 WordPress 3.9.1
  • WooCommerce 2.1.12
  • PHP 5.3.10(也在本地尝试使用 5.5.10)

我的尝试

  • 删除除 WooCommerce 之外的所有插件
  • 更改主题(尝试了所有标准 WordPress 主题和一些 WooThemes 主题,但无济于事)
  • 更新了 WooCommerce(它是 v 2.1.5)
  • 手动插入到 wp_postmeta 添加元密钥 _manage_stock 并将其设置为 no(在我们的一些工作产品中观察到)
  • 手动插入到 wp_postmeta 添加元键 '_stock' 并将其设置为 0(在我们的一些工作产品中观察到)
  • 比较了单击更新按钮前后产品的 postmeta,以观察是否有任何更改会导致问题...没有
  • 没有开启缓存
  • 使用 XDebug 跟踪 variable.php 中的 $available_variations 变量和 class-wc-ajax.phpclass-wc-product-variable.php

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

老实说,在这一点上我很茫然。我认为问题出在任何一个类的某个地方,但我无法判断。

还有其他人对此有疑问吗?解决这个问题的任何方法,这样我就不必继续为这个客户更新产品了。

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

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

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

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

功能正常的产品示例:

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] => 
)

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

2014 年 10 月 23 日更新

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

【问题讨论】:

  • 只有可变产品有这个问题吗?或者你也有简单的产品?我的第一个猜测是这与产品变体有关。
  • 是和不是。该问题仅发生在可变产品上,特别是提供物理和数字下载的产品。然而,它并不是所有可变产品(物理/下载)。新开发的产品没有这个问题,但到目前为止,我还没有弄清楚原因。
  • 听起来很奇怪。检查这些产品是否有重复的元数据(post_metadata 中的多行具有相同的键和产品 ID。
  • 是的。根据您的建议,我检查了 postmeta 并没有发现产品 ID 的任何重复行,我注意到有问题:(
  • 好吧,$available_variations 变量来自$product-&gt;available_variations() 方法,它确实调用了$product-&gt;get_children()确实 使用瞬态来存储孩子。不知道它是否会有所帮助,但删除所有 wc_product_children_ids_$product_id 瞬态不会有什么坏处吗?

标签: php mysql wordpress woocommerce


【解决方案1】:

我遇到了同样的问题,我想我已经解决了。我使用 CSV 导入/导出套件来导出我的所有变体。您会注意到 stock 设置为 0,而它的值应该是 null

你有两个选择:

  1. 进入每个单独的产品,单击变体选项卡并展开每个变体 - 取消选中“管理库存”复选框:
  2. 您可以在数据库上执行查找和替换,将 0 值替换为 null。

【讨论】:

  • 谢谢,我会试一试。如果是这样,我会很高兴!
  • 还要确保变体实际上设置为“已启用”。这是我遇到的另一个问题。
  • 非常感谢。这真的解决了问题。但是如何仅在数据库上运行它以进行变体?
【解决方案2】:

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

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

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

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

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

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

然后它起作用了。

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

【讨论】:

  • 是的,这对我也有用。它在上述文档链接和常规价格输入的占位符文本中——我只是在这两个地方都错过了。
【解决方案3】:

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

  • WordPress 4.8.1
  • WooCommerce 3.1.2
  • PHP 5.6
  • 大量插件和 janky 主题

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

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

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

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

第 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();
}

我遇到了一些缓存问题,导致它看起来好像一开始不起作用。要测试它是否有效,您可以放弃

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

if 的顶部和底部。现在你可以查看页面了,价格正上方将是两个隐藏的&lt;pre&gt;,包含修复前后的$product

这通过使用sync() 函数来强制产品获取所有缺失的变体信息,然后更新变量以在模板中使用。我相信价格是模板中最早使用的任何变体数据,但如果需要,这个 sn-p 可以轻松地在模板链中更高。

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

【讨论】:

  • 我也有这个问题很长一段时间,我认为保存可以解决问题,但不知道为什么,就确认由于某种原因,某些产品有时会到达渲染没有任何变化数据,只保存产品修复了一段时间,但无论如何,谢谢,这对我有用 variable.php 而不是 price.php 但相同的代码!
【解决方案4】:

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

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

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

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

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

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

【讨论】:

    【解决方案5】:

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

    【讨论】:

    • 好建议。我确实尝试过重新安装 WooCommerce 的新副本,但没有成功(我将测试刚刚发布的 2.2 更新)。我们已经讨论过删除和添加产品,但还没有,因为客户在年底使用这些数据报告和发送税收收据(他们是非营利组织)。任何想法如何绕过这个限制?我看到的唯一另一种方法是保留坏项目并创建新项目,但我们会有重复的建议?
    【解决方案6】:

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

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

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

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

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

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

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

        基于 WordPress woocommerce 的缺货产品消息显示并更改了一些棘手的选项。如果您关心 wordpress 插件开发人员,您可以覆盖缺货挂钩。您也可以在 wordpress 插件目录中使用各种 wordpress 免费插件。

        您可以使用“缺货消息”或“缺货消息 woocommerce”搜索谷歌。

        有一个很棒的插件可以解决缺货消息。插件链接out of Stock message.

        【讨论】:

        猜你喜欢
        • 2014-09-26
        • 2015-03-14
        • 2015-04-30
        • 2019-09-01
        • 1970-01-01
        • 2014-08-20
        • 1970-01-01
        • 2013-04-12
        • 1970-01-01
        相关资源
        最近更新 更多