/** * Pull products data from Dolibarr via webservice and save it in Wordpress * * @return void */ public function dolibarr_import_products() { try { $soap_client = new SoapClient($this->ws_endpoint . self::PRODUCT_ENDPOINT . self::WSDL_MODE); } catch (SoapFault $exception) { $this->logger->add('doliwoo', $exception->getMessage()); // Do nothing. return; } // Get all products that are meant to be displayed on the website try { $result = $soap_client->getProductsForCategory($this->ws_auth, $this->settings->dolibarr_category_id); } catch (SoapFault $exception) { $this->logger->add('doliwoo', 'getProductsForCategory request: ' . $exception->getMessage()); // Do nothing. return; } if (!('OK' == $result['result']->result_code)) { $this->logger->add('doliwoo', 'getProductsForCategory response: ' . $result['result']->result_code . ': ' . $result['result']->result_label); // Do nothing return; } /** @var Dolibarr_Product[] $dolibarr_products */ $dolibarr_products = $result['products']; if (!empty($dolibarr_products)) { foreach ($dolibarr_products as $dolibarr_product) { if (0 == $dolibarr_product->status_tosell) { // This product is not for sale, let's skip it. continue; } $existing_product = $this->dolibarr_product_exists($dolibarr_product->id); if ($existing_product) { // Update the product $post = array('ID' => $existing_product->ID, 'post_title' => $dolibarr_product->label, 'post_content' => $dolibarr_product->description); $post_id = wp_update_post($post); } else { // Create a new product $post = array('post_title' => $dolibarr_product->label, 'post_content' => $dolibarr_product->description, 'post_status' => 'publish', 'post_type' => 'product'); $post_id = wp_insert_post($post); } // Error management (logging) if (is_wp_error($post_id)) { /** @var WP_Error $post_id */ $this->logger->add('doliwoo', $post_id->get_error_message()); } if (0 < $post_id && !is_wp_error($post_id)) { /** @var int $post_id */ $this->update_product_attributes($dolibarr_product, $post_id); } } } }