Last active
June 11, 2020 21:52
-
-
Save rvdsteege/6b363f5c9c99ee82442bd32839f41a8c to your computer and use it in GitHub Desktop.
WordPress action to sync stock quantity to connected sites with the WooCommerce Stock Synchronization plugin when using WP All Import to update stock on the master shop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add below code to functions.php of your WordPress theme. | |
/** | |
* Sync stock with WooCommerce Stock Synchronization after WP All Import product update. | |
* | |
* @see https://www.pronamic.eu/plugins/woocommerce-stock-synchronization/ | |
*/ | |
function wcss_pmxi_stock_update( $post_id ) { | |
if ( ! is_callable( 'wc_get_product' ) ) { | |
return; | |
} | |
$product = wc_get_product( $post_id ); | |
if ( ! $product ) { | |
return; | |
} | |
if ( $product->is_type( 'variation' ) ) { | |
do_action( 'woocommerce_variation_set_stock', $product ); | |
} else { | |
do_action( 'woocommerce_product_set_stock', $product ); | |
} | |
} | |
add_action( 'pmxi_saved_post', 'wcss_pmxi_stock_update', 10, 1 ); | |
// The WP All Import - WooCommerce Add-On Pro adds the `pmwi_pmxi_after_post_import` action, | |
// which removes all action hooked into `shutdown`. WooCommerce Stock Synchronization uses | |
// the shutdown action to sync. We therefore remove the `pmwi_pmxi_after_post_import` action. | |
function remove_pmwi_pmxi_after_post_import() { | |
remove_action( 'pmxi_after_post_import', 'pmwi_pmxi_after_post_import' ); | |
} | |
add_action( 'pmxi_after_post_import', 'remove_pmwi_pmxi_after_post_import', 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment