Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
#548 #519 correction for new products
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon007 committed May 16, 2021
1 parent cd136d7 commit 479b3cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/Hyyan/WPI/Product/Meta.php
Expand Up @@ -41,6 +41,9 @@ public function __construct()
'woocommerce_product_quick_edit_save', array($this, 'saveQuickEdit'), 10, 1
);

//setup new translations only which are not yet saved by WooCommerce
add_action('save_post_product', array($this, 'handleNewProduct'), 5, 3);

// suppress "Invalid or duplicated SKU." error message when SKU syncronization is enabled
add_filter(
'wc_product_has_unique_sku',
Expand All @@ -57,6 +60,19 @@ public function __construct()
}
}

/*
* if a new product do the initial sync now as there is no woo save event at this point
*
* @param int $post_id Id of product being edited: if new product copy from source,
* if existing product synchronize translations
* @param \WP_Post $post Post object
* @param boolean $update Whether this is an existing post being updated or not
*/
public function handleNewProduct($post_id, $post, $update){
if (! $update && (isset($_GET['from_post'])) ){
$this->syncTaxonomiesAndProductAttributes($post_id, $post, $update);
}
}

/**
* On insert of a new product attribute, attempt to set it to translateable by default
Expand Down Expand Up @@ -143,10 +159,7 @@ public function handleProductScreen()
return false;
}

//JM2021: switch from wp_insert_post to save_post_product, and higher priority than variable hook
//#548 action is documented in wp-includes/post.php but product not yet saved
//add_action('save_post_product', array($this, 'syncTaxonomiesAndProductAttributes'), 5, 3);
//do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
//JM2021: #548 use WooCommerce save hook, and higher priority than variable hook
add_action('woocommerce_after_product_object_save', array($this, 'syncTaxonomiesAndProductAttributesAfterProductUpdate'), 5, 2);

$ID = false;
Expand Down
21 changes: 17 additions & 4 deletions src/Hyyan/WPI/Product/Variable.php
Expand Up @@ -28,10 +28,9 @@ class Variable
public function __construct()
{
// Handle variations duplication
//#548 action is documented in wp-includes/post.php but product not yet saved
//add_action( 'save_post_product', array( $this, 'duplicateVariations' ), 10, 3 );
//add_action( 'save_post_product', array( $this, 'syncDefaultAttributes' ), 10, 3 );
//replace with do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
//#548 action is documented in wp-includes/post.php but product not yet saved, use only for new products
add_action('save_post_product', array($this, 'handleNewProduct'), 5, 3);
//for updates use do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
add_action( 'woocommerce_after_product_object_save', array( $this, 'after_product_save' ), 10, 2 );

// Remove variations
Expand All @@ -50,6 +49,20 @@ public function __construct()
}
}

/*
* if a new product do the initial sync now as there is no woo save event at this point
*
* @param int $post_id Id of product being edited: if new product copy from source,
* if existing product synchronize translations
* @param \WP_Post $post Post object
* @param boolean $update Whether this is an existing post being updated or not
*/
public function handleNewProduct($post_id, $post, $update){
if (! $update && (isset($_GET['from_post'])) ){
$this->duplicateVariations($post_id, $post, $update);
$this->syncDefaultAttributes($post_id, $post, $update);
}
}

/*
* wrapper for compatibility with woocommerce_after_product_save action
Expand Down

0 comments on commit 479b3cb

Please sign in to comment.