Example #1
0
 /**
  * Webservice calls to get the product's images
  *
  * @param Dolibarr_Product $dolibarr_product SOAP product object
  * @param int $post_id WooCommerce product ID
  *
  * @return int[] Attachment IDs
  */
 private function get_product_image($dolibarr_product, $post_id)
 {
     try {
         $soap_client = new SoapClient($this->ws_endpoint . self::OTHER_ENDPOINT . self::WSDL_MODE);
     } catch (SoapFault $exception) {
         $this->logger->add('doliwoo', $exception->getMessage());
         // Do nothing.
         return null;
     }
     $file_array = array();
     $attach_ids = array();
     foreach ($dolibarr_product->images as $images) {
         // Get the image from Dolibarr
         try {
             $result = $soap_client->getDocument($this->ws_auth, 'product', $dolibarr_product->dir . $images->photo);
         } catch (SoapFault $exception) {
             $this->logger->add('doliwoo', 'getDocument request:' . $exception->getMessage());
             // Do nothing.
             continue;
         }
         if (!('OK' == $result['result']->result_code)) {
             $this->logger->add('doliwoo', 'getDocument response: ' . $result['result']->result_code . ': ' . $result['result']->result_label);
             // Do nothing
             continue;
         }
         $file_array['name'] = $images->photo;
         $file_array['tmp_name'] = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $images->photo;
         file_put_contents($file_array['tmp_name'], base64_decode($result['document']->content));
         $res = media_handle_sideload($file_array, $post_id);
         // Handle errors nicely ( logging )
         if (is_wp_error($res)) {
             $message = $res->get_error_message();
             $this->logger->add('doliwoo', $message);
         } else {
             $attach_ids[] = $res;
         }
     }
     return $attach_ids;
 }