/**
  * Convert a WC Product into a Square Item for Create
  *
  * Creation (POST) allows more parameters than Updating, namely variations
  * See: https://docs.connect.squareup.com/api/connect/v1/index.html#post-items
  *
  * @param WC_Product $wc_product
  * @param bool       $include_category
  * @param bool       $include_inventory
  * @return array
  */
 public static function format_wc_product_create_for_square_api(WC_Product $wc_product, $include_category = false, $include_inventory = false)
 {
     $formatted = self::format_wc_product_update_for_square_api($wc_product, $include_category);
     // check product type
     if ('simple' === $wc_product->product_type) {
         $formatted['variations'] = array(WC_Square_Utils::format_wc_variation_for_square_api($wc_product, $include_inventory));
     } elseif ('variable' === $wc_product->product_type) {
         $wc_variations = self::get_wc_product_variations($wc_product);
         foreach ((array) $wc_variations as $wc_variation) {
             $formatted['variations'][] = WC_Square_Utils::format_wc_variation_for_square_api($wc_variation, $include_inventory);
         }
     }
     return array_filter($formatted);
 }
 /**
  * Update a Square Item Image for a WC Product
  *
  * @param WC_Product $wc_product
  * @param string     $square_item_id
  * @return bool Success.
  */
 public function update_product_image(WC_Product $wc_product, $square_item_id)
 {
     $image_id = get_post_thumbnail_id($wc_product->id);
     if (empty($image_id)) {
         WC_Square_Sync_Logger::log(sprintf('[WC -> Square] Update Product Image: No thumbnail ID for WC Product %d.', $wc_product->id));
         return true;
     }
     $mime_type = get_post_field('post_mime_type', $image_id, 'raw');
     $image_path = get_attached_file($image_id);
     $result = $this->connect->update_square_product_image($square_item_id, $mime_type, $image_path);
     if ($result && isset($result->id)) {
         WC_Square_Utils::update_wc_product_image_square_id($wc_product->id, $result->id);
         return true;
     } else {
         WC_Square_Sync_Logger::log(sprintf('[WC -> Square] Error updating Product Image for WC Product %d.', $wc_product->id));
         return false;
     }
 }
 /**
  * Update the corresponding Square Item for a WC Product.
  *
  * See: https://docs.connect.squareup.com/api/connect/v1/#put-itemid
  *
  * @param WC_Product $wc_product
  * @param string     $square_item_id
  * @param bool       $include_category
  * @param bool       $include_inventory
  * @return object|bool Updated Square Item object on success, boolean False on failure.
  */
 public function update_square_product($wc_product, $square_item_id, $include_category = false, $include_inventory = false)
 {
     // We can only handle simple products or ones with variations
     if (!in_array($wc_product->product_type, array('simple', 'variable'))) {
         return false;
     }
     // TODO: Consider making this method "dumber" - remove this formatting call.
     $product = WC_Square_Utils::format_wc_product_update_for_square_api($wc_product, $include_category);
     $endpoint = 'items/' . $square_item_id;
     return $this->_client->request('Updating a Product', $endpoint, 'PUT', $product);
 }
 /**
  * Sync all inventory from Square (expensive)
  * @todo if searching for square id fails, check for SKU
  */
 public function sync_all_inventory()
 {
     // refresh cache first to get the latest inventory
     $this->connect->refresh_inventory_cache();
     $square_inventory = $this->connect->get_square_inventory();
     // hopefully there has been a manual sync prior so that square item id
     // has already been saved in the product/variation metas to prevent
     // unnecessary round trip requests to Square to find the SKU
     foreach ($square_inventory as $variation_id => $stock) {
         $wc_product = WC_Square_Utils::get_wc_product_for_square_item_variation_id($variation_id);
         if (is_object($wc_product)) {
             $wc_product->set_stock((int) $stock);
         }
     }
     return true;
 }