Skip to content

Instantly share code, notes, and snippets.

@rvdsteege
Last active June 11, 2020 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvdsteege/6b363f5c9c99ee82442bd32839f41a8c to your computer and use it in GitHub Desktop.
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.
<?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