autoship_pu​sh_product (function)

(src/products.php) Main upload function which upserts a product and its variations to QPilot. It takes the WP Post Id or product object ( $id) as its parameter. 

Function

/**
 * Main Upload Function Creates the product / product variations in QPilot.
 *
 * Receives a Product to create, checks for the QPilot client,
 * validates the product via {@see autoship_productize_and_validate()}, and then upserts ( updates or creates ) the product and its
 * variations in QPilot.
 *
 * @param mixed $id. Post object or post ID of the product.
 * @return bool|WP_Error True on success else false|wp_error
 */
function autoship_push_product ( $id ){

  // Create the QPilot client
  // Grab the client and only continue if connection exists.
  if ( empty( $client = autoship_get_default_client() ) || empty( $client->get_token_auth() ) )
  return false;

  // Get the product ( if invalid or wrong type or not correct status returns false )
  if ( ( $product = autoship_productize_and_validate( $id ) ) === false )
  return false;

  try {

      // If this is a variation then lets initialize Autoship fields
      // If they aren't already
      if ( 'variation' == $product->get_type() ){

        // Since this is a variation we need to ensure the parent exists.
        // Currently the best route is to try and get the product via external id.
        try {
          $Qproduct = $client->get_product( $product->get_parent_id() );
        } catch ( Exception $e ) {

          // Since this is an create / update for a variation
          // and a 404 is thrown Assume the parent needs to be created
          // before moving on.
          if ( $e->getCode() == 404 ) {
           do_action( 'autoship_push_product_404_missing_variable_parent', $product, $product->get_parent_id() );
           autoship_push_product ( $product->get_parent_id() );
          }
          autoship_log_entry( __( 'Autoship Products', 'autoship' ), $e->getMessage() );
        }

        // Initialize the variation - any missing fields add them.
        $product = autoship_init_variation_metafields( $product->get_id() );

      }

      // Gather the data to send
      if ( empty( $upsert_data = autoship_generate_product_upsert_data ( $product ) ) )
      return new WP_Error( 'Product Data Not Found', __( "The Supplied Product's data could not be found.", "autoship" ) );


      // Send / Create product
      $client->upsert_product( $upsert_data['id'], $upsert_data['title'], $upsert_data );

      // Check if the product is a variable product
      // If variable & has children we should loop through
      // children to update if needed.
      $variations = autoship_get_available_variations( $product );

      // Update the child variations if they exist
      foreach ( $variations as $variation ) {

        // Recursively push the variations.
        autoship_push_product( $variation );

      }

  } catch (Exception $e) {
    $notice = autoship_expand_http_code( $e->getCode() );
    autoship_log_entry( __( 'Autoship Products', 'autoship' ), sprintf( 'Product Upsert Failed Additional Details: %s', $e->getMessage() ) );
    return new WP_Error( 'Product Upsert Failed', __( $notice['desc'], "autoship" ) );
  }

  do_action( 'autoship_after_push_product', $product );
  return true;

}

Still need help? Contact Us Contact Us