hyyan / woo-poly-integration

Looking for maintainers! - Wordpress WooCommerce Polylang Integration
https://wordpress.org/plugins-wp/woo-poly-integration/
MIT License
183 stars 66 forks source link

"Out of stock" when selling a product with variations #535

Closed fourlife closed 3 years ago

fourlife commented 3 years ago

Can you reproduce this issue on default Wordpress theme (eg Storefront)?

Yes

Can you reproduce this issue when all other plugins are disabled except WooCommerce, Polylang and Hyyan WooCommerce Polylang Integration?

What product versions and settings are you using when this issue occurs?

Steps to Reproduce

  1. Buying a product with variations
  2. After buying, the product goes out of stock in one of the languages

What I Expected

Variations have enough stock, so it should keep showing the product as available. All issues of the past months regarding variations seem to be fixed, but I'm still having this one. The only solution I've found for this has been not updating Polylang since version 2.7.4 (approximately september 2020).

What Happened Instead

Goes out of stock.

Thanks a lot to all contributors for the amazing work

Best regards

Jon007 commented 3 years ago

duplicates #475 should be fixed now, please try new draft release here: https://github.com/hyyan/woo-poly-integration/releases/tag/1.4.5 if this is ok there will be a full release sometime over the next week

fourlife commented 3 years ago

I've tried 1.4.5 but the issue isn't fixed at least for me

Jon007 commented 3 years ago

ok, reopen, some issue still remaining.

Jon007 commented 3 years ago

ok the problem can be seen in the changelog for Polylang 2.8: https://plugins.svn.wordpress.org/polylang/trunk/changelog.txt

this has caused various problems with workarounds implemented in other places since whenever we are trying to synchronise data in other languages, Polylang is interfering with this by filtering every query, more so than previous versions.

In this particular case the problem occurs when updating variable product on the other languages here: WooCommerce class-wc-product-variable.php

public function save() {
        $this->validate_props();

before validate_props, all is ok and instock, however after validate_props although stock status is still "instock", the changes property now contains stock_status "outofstock" .. this will be because WooCommerce can no longer find any child variationa with stock - $product->child_is_in_stock() fails - because Polylang has filtered them out because they are not in the "current" language.

Unlike some other areas of the plugin we can't change the query or add the language parameter because it's not our query, it's WooCommerce internal validation check $this->data_store->sync_stock_status( $this ),

To solve definitively we might need to switch current language for every variation language which needs stock synchronised. That's a more major change which might have functional an performance impacts, needs more thought and testing. In the meantime it will probably work with Polylang 2.7.4 ..

It would be great if someone else contributed, I don't have any use for WooCommerce stock features, though this is symptom of a potentially wider problem.

Jon007 commented 3 years ago

root cause: WooCommerce class-wc-product-variable-data-store-cpt method read_children() internally uses WordPress get_posts which uses WPQuery which has a language filter added by Polylang.

However there is a filter woocommerce_variable_children_args: $children['all'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $all_args, $product, false ) );

In theory hooking this filter and adding empty lang parameter would suppress Polylang adding an additional current language filter. As per recent change to Variation.php and related Polylang documentation:

                    //JM2021:empty lang parameter required otherwise polylang adds language filter
                     'lang' => '',  
fourlife commented 3 years ago

Thanks for checking it Jon007, wish I could help

Jon007 commented 3 years ago

no worries I'll check something in later, fix along the lines above does work but there were some additional edge cases where may not synchronise properly, just working through those.

Jon007 commented 3 years ago

changes to several files to ensure:

caveats: if problems continue, go to WooCommerce, Status, Tools ie /wp-admin/admin.php?page=wc-status&tab=tools and:

  1. clear transients. not convinced WooCommerce clear transients always works, also clear Redis cache etc if you use that.
  2. Regenerate product lookup tables
  3. save any affected product
  4. repeat 1 and 2 if necessary

generally any time the product is saved or sold the stock should resync itself and correct any issues that are already there, but getting the files alone will not automatically clear existing issues especially where out-of-sync information is already cached