autoship_update_product_availability (function)
( src/products.php) Used to update a product / variation availability in QPilot. It takes the WP Post Id or product object ( $id) & the product's $availability as its parameters.
Function
/**
* Updates the product / product variation Availability in QPilot.
*
* @param mixed $id. Post object or post ID of the product.
* @param string $availability The product availability to update. Values should be
* "AddToScheduledOrder,ProcessScheduledOrder", "AddToScheduledOrder",
* "ProcessScheduledOrder" or empty string
*/
function autoship_update_product_availability ( $id, $availability = '' ){
// 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() );
}
// Based on supplied value set the flags
$addToScheduledOrder = strpos($availability, 'AddToScheduledOrder') !== false;
$processScheduledOrder = strpos($availability, 'ProcessScheduledOrder') !== false;
// update product availability
$client->update_product_availability( $product->get_id() , $addToScheduledOrder, $processScheduledOrder );
}
catch (Exception $e) {
autoship_log_entry( __( 'Autoship Products', 'autoship' ), $e->getMessage() );
}
do_action( 'autoship_after_update_product_availability', $product );
return true;
}