Ignore Stock Status on WooCommerce Products Synchronized to QPilot

In the example below, a developer wants QPilot to ignore the Stock Status of a WooCommerce product, always showing it as In-Stock in QPilot. 

Remember: Always test code on a staging site before adding it to your live site

Reminder: Since the code hooks into the WooCommerce update and save product functionality, updating the product will be required to show up as In Stock in QPilot

Code Example

/**
 * Checks if the Stock Status should be ignored for the supplied product/variation
 * @param array $int The Product or Variation ID
 *
 * @return bool True ignore else false.
 */
function xx_ignore_stock_status_for_sync( $id ){
	// Add conditional code ( retrieve metadata or category or anything else needed )
	// Example: Targeting a specific Product Id would return "$id == 2846;" instead of true
	return true;
	
  }
  
   /**
   * Always treat Products as InStock for QPilot Product Upsert
   * @param array $product_data The Product Upsert Data
   *
   * @return array The filtered Data.
   */
  function xx_always_treat_products_as_in_stock_for_upsert( $product_data ) {
  
	// Check if product should be shown as instock
	if ( xx_ignore_stock_status_for_sync( $product_data['id'] ) )
	$product_data['availability'] = 'InStock';
	$product_data['backordersAllowed'] = true;
  
  
	return $product_data;
  }
  add_filter( 'autoship_product_upsert_data', 'xx_always_treat_products_as_in_stock_for_upsert', 10, 1 );
  
   /**
   * Always treat Products as InStock for QPilot Product Sync
   *
   * @param WP_REST_Response $response The response object.
   * @param WC_Data          $object   Object data.
   * @param WP_REST_Request  $request  Request object.
   *
   * @return WP_REST_Response The Adjusted Response
   */
  function xx_always_treat_products_as_in_stock_for_qpilot_sync( $response, $object, $request ) {
  
	// Check if product should be shown as instock
	if ( xx_ignore_stock_status_for_sync( $response->data['id'] ) )
	$response->data['in_stock'] = true;
  
	return $response;
  
  }
  add_filter( 'woocommerce_rest_prepare_product_object', 'xx_always_treat_products_as_in_stock_for_qpilot_sync', 90, 3 );
  
   /**
   * Removes the Stock Level Sync Check since the Status in QPilot may not match WC.
   *
   * @param array $data The current collected data for the variation.
   * @param stdClass $summary The QPilot Variation Object
   * @param WC_Product $product The WooCommerce product.
   * @return array The filtered Data
   */
  function xx_ignore_autoship_product_summary_stocklevel_sync_check( $data, $summary, $product ) {
  
	// Get the Stock Status and translate it to the QPilot Equivalent
	// Since QPilot doesn't track certain statuses and we want to ignore stock differences.
	// Check if product should be shown as instock
	if ( xx_ignore_stock_status_for_sync( $product->get_id() ) )
	$data['StockLevel'] = autoship_get_mapped_stocklevel( $product->get_stock_status() );
  
	return $data;
  
  }
  add_filter( 'autoship_product_summary_data', 'xx_ignore_autoship_product_summary_stocklevel_sync_check', 10, 3 );
  
   /**
   * Removes the Stock Level Sync Check since the Status in QPilot may not match WC.
   *
   * @param array $data The current collected data for the variation.
   * @param stdClass $summary The QPilot Variation Object
   * @param WC_Product $product The WooCommerce Variations Parent Product.
   * @return array The filtered Data
   */
  function xx_ignore_autoship_variable_product_summary_stocklevel_sync_check( $data, $summary, $product ) {
  
	// Get the Stock Status for the variation and translate it to the QPilot Equivalent
	// Since QPilot doesn't track certain statuses and we want to ignore stock differences.
	// Check if product should be shown as instock
	if ( xx_ignore_stock_status_for_sync( $data['Id'] ) ){
	$variation = wc_get_product( $data['Id'] );
	$data['StockLevel'] = autoship_get_mapped_stocklevel( $variation->get_stock_status() ); }
  
	return $data;
  
  }
  add_filter( 'autoship_variable_product_summary_data', 'xx_ignore_autoship_variable_product_summary_stocklevel_sync_check', 10, 3 );
  
  
   /**
   * Removes the Stock Level Sync Check on All Products table since the Status in QPilot may not match WC.
   *
   * @param array $data The current collected data for the variation.
   * @param stdClass $summary The QPilot Variation Object
   *
   * @return array The filtered Data
   */
  function xx_ignore_autoship_summary_stocklevel_sync_check( $data, $summary ) {
  
	// Get the Stock Status for the variation and translate it to the QPilot Equivalent
	// Since QPilot doesn't track certain statuses and we want to ignore stock differences for certain procucts.
	// Check if product should be shown as instock
	if ( xx_ignore_stock_status_for_sync( $data['Id'] ) ){
	$variation = wc_get_product( $data['Id'] );
	$data['StockLevel'] = autoship_get_mapped_stocklevel( $variation->get_stock_status() ); }
  
	return $data;
  
  }
  add_filter( 'autoship_all_products_summary_data_row', 'xx_ignore_autoship_summary_stocklevel_sync_check', 10, 2 );

Result:

After updating the Out of Stock product in WooCommerce it will be In-Stock in QPilot and be available to ship in Scheduled Orders (see an example of this below).