Skip to content

Instantly share code, notes, and snippets.

@simonporter007
Last active November 22, 2022 03:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonporter007/3fb9b3d86fdc51ed1848132cfbcc0bea to your computer and use it in GitHub Desktop.
Save simonporter007/3fb9b3d86fdc51ed1848132cfbcc0bea to your computer and use it in GitHub Desktop.
Facebook for WooCommerce: Sync inventory count along with product
<?php //only copy if required!
/*
* Sync product inventory to Facebook
*/
function sv_facebook_for_woocommerce_integration_prepare_product( $product_data, $id ) {
$product = wc_get_product( $id );
// Get variation if variable product ID provided
if ( 'variable' === $product->get_type() ) {
if ( preg_match('/.*_(\d+)$/', $product_data[ 'retailer_id' ], $matches) ) {
$product = wc_get_product( $matches[1] );
}
}
if ( $product ) {
$product_data[ 'inventory' ] = $product->get_stock_quantity();
}
return $product_data;
}
add_filter( 'facebook_for_woocommerce_integration_prepare_product', 'sv_facebook_for_woocommerce_integration_prepare_product', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment