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

product type and catalog visibility not synchronising, problems switching from variable to other product types #548

Closed Jon007 closed 3 years ago

Jon007 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?

yes

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

Steps to Reproduce

  1. setup variable product in 3 languages
  2. change main language to Simple product, change catalog visibility
  3. save

What I Expected

expect translations to also switch to simple product and update catalog visibility

What Happened Instead

inconsistent behaviour, translations not updated as expected

Jon007 commented 3 years ago

as a cautionary note, stock status also affects product visibility

Jon007 commented 3 years ago

ok the real root cause is that this plugin currently hooks 'save_post_product' but at this point although the post is saved, the product data is not saved since WooCommerce save is hooked to 'save_post' which comes one line later in wordpress post.php:

    /**
     * Fires once a post has been saved.
     *
     * The dynamic portion of the hook name, `$post->post_type`, refers to
     * the post type slug.
     *
     * @since 3.7.0
     *
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated.
     */
    do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );

    /**
     * Fires once a post has been saved.
     *
     * @since 1.5.0
     *
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated.
     */
    do_action( 'save_post', $post_ID, $post, $update );

save_post_product is logical and nice and specific to products not intercepting every post save but where the action happens in WooCommerce is save_post eg WC_Admin_Meta_Boxes: add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );

class WC_Meta_Box_Product_Data save() actually does $product->save(); Post save actions would be do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id ); from within the meta box save, or woocommerce_after_product_save

        /**
         * Trigger action after saving to the DB.
         *
         * @param WC_Data          $this The object being saved.
         * @param WC_Data_Store_WP $data_store The data store persisting the data.
         */
        do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );

This last is the preferred since products can be saved in various different contexts.. This also has the advantage that the product object and its data store are also available as parameters which means this plugin code could be adapted to use these directly in future to avoid problems arising from using the wp api rather than the woo api