From 690f43e1c08faf8204707d934edb6163f2a409d2 Mon Sep 17 00:00:00 2001 From: cmoinon Date: Thu, 7 Jul 2016 10:44:27 +0200 Subject: [PATCH 1/6] corretion bug in line 50 : add a condition to check if rates_value have array_key [0] --- includes/class-wc-tax.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index 9caf020..b9919c6 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -47,9 +47,12 @@ public function get_tax_class( $tax_rate ) { foreach ( $tax_classes as $class ) { $rates = $this->get_rates( $class ); $rates_values = array_values( $rates ); - if ( floatval( $rates_values[0]['rate'] ) === $tax_rate ) { - // Use the first class found - return $class; + // Ajout CMO - Juillet 2016 + if(array_key_exists(0, $rates_values)){ + if ( floatval( $rates_values[0]['rate'] ) === $tax_rate ) { + // Use the first class found + return $class; + } } } From e05d9a1f5fa35374988f187eb551ab505d4dc7db Mon Sep 17 00:00:00 2001 From: cmoinon Date: Thu, 7 Jul 2016 13:51:28 +0200 Subject: [PATCH 2/6] search client in Dolibarr with company_name if client have else with last_name ans first_name --- includes/class-dolibarr.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/includes/class-dolibarr.php b/includes/class-dolibarr.php index 5d55451..c4ce878 100644 --- a/includes/class-dolibarr.php +++ b/includes/class-dolibarr.php @@ -85,6 +85,8 @@ public function dolibarr_create_order() { array( 'location' => $this->ws_endpoint . self::ORDER_ENDPOINT, 'uri' => 'http://www.dolibar.org/ns/', + 'wsdl_cache' => 0, + 'trace' => 1, ) ); } catch ( SoapFault $exception ) { @@ -108,14 +110,20 @@ public function dolibarr_create_order() { $order->thirdparty_id = $thirdparty_id; } else { if ( 0 === intval( get_user_meta( $user_id, 'billing_company', true ) ) ) { - if ( wp_verify_nonce( 'woocommerce-cart' ) ) { + + // CMOINON (OPEN-DSI) Correctif + //if ( wp_verify_nonce( 'woocommerce-cart' ) ) { + + /* Suppression de lal vérification => contrôle à revoir dans un deuxième temps + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'] , 'woocommerce-process_checkout')) { $billing_company = $_POST['billing_company']; } else { // TODO: fail message? $this->logger->add( 'doliwoo', 'Failed to verify nonce' ); exit; - } + }*/ + $billing_company = $_POST['billing_company']; update_user_meta( $user_id, 'billing_company', $billing_company ); } $this->dolibarr_create_thirdparty_if_not_exists( $user_id ); @@ -125,7 +133,7 @@ public function dolibarr_create_order() { $order->status = 1; $this->create_order_lines( $order ); - + try { $result = $soap_client->createOrder( $this->ws_auth, $order ); } catch ( SoapFault $exception ) { @@ -159,13 +167,18 @@ public function dolibarr_create_order() { private function dolibarr_create_thirdparty_if_not_exists( $user_id ) { + $result = $this->dolibarr_thirdparty_exists( $user_id ); if ( null === $result ) { // Does not exist, create it $result = $this->dolibarr_create_thirdparty( $user_id ); + update_user_meta( $user_id, 'dolibarr_id', $result['id'] ); + } else { + // find in Dolibarr + update_user_meta( $user_id, 'dolibarr_id', $result['thirdparty']->id ); } - update_user_meta( $user_id, 'dolibarr_id', $result['id'] ); + } /** @@ -176,6 +189,7 @@ private function dolibarr_create_thirdparty_if_not_exists( * @return int $result Array with the request results if it succeeds, null if there's an error */ private function dolibarr_thirdparty_exists( $user_id ) { + try { $soap_client = new SoapClient( $this->ws_endpoint . self::THIRDPARTY_ENDPOINT . self::WSDL_MODE @@ -190,6 +204,11 @@ private function dolibarr_thirdparty_exists( $user_id ) { $dol_id = get_user_meta( $user_id, 'dolibarr_id', true ); $dol_ref = get_user_meta( $user_id, 'billing_company', true ); + // If the user has not a billing_company $dol_ref = last_name + first_name + if ( ! $dol_ref) { + $dol_ref = get_user_meta( $user_id, 'billing_last_name', true ) . ' ' . get_user_meta( $user_id, 'billing_first_name', true ); + } + // If the user has a Dolibarr ID, use it, else search his company name if ( $dol_id ) { $dol_ref = null; From c6898c4ffc1edecc733259f7a9de6a0eb92cfccc Mon Sep 17 00:00:00 2001 From: cmoinon Date: Thu, 7 Jul 2016 17:01:35 +0200 Subject: [PATCH 3/6] for import_product take price instead price_net and make the import of images od product --- includes/class-dolibarr.php | 92 ++++++++++++++++++++++++++++++++----- 1 file changed, 80 insertions(+), 12 deletions(-) diff --git a/includes/class-dolibarr.php b/includes/class-dolibarr.php index c4ce878..9ddff51 100644 --- a/includes/class-dolibarr.php +++ b/includes/class-dolibarr.php @@ -123,7 +123,13 @@ public function dolibarr_create_order() { exit; }*/ - $billing_company = $_POST['billing_company']; + // if it is not a company, billing_company = first_name + last_name ( as Dolibarr) + if (! $_POST['billing_company']) { + $billing_company = $_POST['billing_first_name'] . ' ' . $_POST['billing_last_name']; + } else { + $billing_company = $_POST['billing_company']; + } + update_user_meta( $user_id, 'billing_company', $billing_company ); } $this->dolibarr_create_thirdparty_if_not_exists( $user_id ); @@ -176,9 +182,15 @@ private function dolibarr_create_thirdparty_if_not_exists( update_user_meta( $user_id, 'dolibarr_id', $result['id'] ); } else { // find in Dolibarr - update_user_meta( $user_id, 'dolibarr_id', $result['thirdparty']->id ); + // if same email => get the id of Dolibarr + if ($result['thirdparty']->email == $_POST['billing_email']) { + update_user_meta( $user_id, 'dolibarr_id', $result['thirdparty']->id ); + // else create a new thirdparty in Dolibarr + } else { + $result = $this->dolibarr_create_thirdparty( $user_id ); + update_user_meta( $user_id, 'dolibarr_id', $result['id'] ); + } } - } /** @@ -204,11 +216,6 @@ private function dolibarr_thirdparty_exists( $user_id ) { $dol_id = get_user_meta( $user_id, 'dolibarr_id', true ); $dol_ref = get_user_meta( $user_id, 'billing_company', true ); - // If the user has not a billing_company $dol_ref = last_name + first_name - if ( ! $dol_ref) { - $dol_ref = get_user_meta( $user_id, 'billing_last_name', true ) . ' ' . get_user_meta( $user_id, 'billing_first_name', true ); - } - // If the user has a Dolibarr ID, use it, else search his company name if ( $dol_id ) { $dol_ref = null; @@ -468,9 +475,14 @@ private function update_product_attributes( $dolibarr_product, $post_id ) { add_post_meta( $post_id, 'dolibarr_type', $dolibarr_product->type, true ); update_post_meta( $post_id, '_sku', $dolibarr_product->ref ); update_post_meta( $post_id, '_purchase_note', $dolibarr_product->note ); - update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net ); +/* update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net ); update_post_meta( $post_id, '_sale_price', $dolibarr_product->price_net ); - update_post_meta( $post_id, '_price', $dolibarr_product->price_net ); + update_post_meta( $post_id, '_price', $dolibarr_product->price_net );*/ + // Update in July 2016 CMOINON => get the price instead if the price_net witch is false with de webservice + // if we have multiple-price + update_post_meta( $post_id, '_regular_price', $dolibarr_product->price ); + update_post_meta( $post_id, '_price', $dolibarr_product->price ); + update_post_meta( $post_id, '_visibility', 'visible' ); update_post_meta( $post_id, @@ -488,9 +500,12 @@ private function update_product_attributes( $dolibarr_product, $post_id ) { } } + // Check if the product has images + $the_product = $this->dolibarr_images_exit($dolibarr_product->id); + // Product images management - if ( ! empty( $dolibarr_product->images ) ) { - $this->import_product_images( $dolibarr_product, $post_id ); + if ( $the_product && ! empty($the_product->images) ) { + $this->import_product_images( $the_product, $post_id ); } // Cleanup @@ -539,6 +554,50 @@ private function delete_post_attachments( $post_id ) { } } + /** + * Check if in Dolibar there is an images => call Webservice + * + * @param $dolibar->product_id + */ + + private function dolibarr_images_exit($product_id) { + 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 null; + } + + // Get the product + try { + $result = $soap_client->getProductOrService( + $this->ws_auth, + $product_id + ); + } catch ( SoapFault $exception ) { + $this->logger->add( + 'doliwoo', + 'getProductOrService request: ' . $exception->getMessage() + ); + } + + if ( ! ( 'OK' === $result['result']->result_code ) ) { + $this->logger->add( + 'doliwoo', + 'getProductOrService response: ' . $result['result']->result_code . ': ' . $result['result']->result_label + ); + + // Do nothing + return false; + } else { + return $result['product'] ; + } + } + /** * Webservice calls to get the product's images * @@ -548,6 +607,15 @@ private function delete_post_attachments( $post_id ) { * @return int[] Attachment IDs */ private function get_product_image( $dolibarr_product, $post_id ) { + + // Ajout CMOINON + // Need to require these files + if ( !function_exists('media_handle_upload') ) { + require_once(ABSPATH . "wp-admin" . '/includes/image.php'); + require_once(ABSPATH . "wp-admin" . '/includes/file.php') ; + require_once(ABSPATH . "wp-admin" . '/includes/media.php'); + } + try { $soap_client = new SoapClient( $this->ws_endpoint . self::OTHER_ENDPOINT . self::WSDL_MODE From 60d23a388a9597a20515177c1fef648f295daf4f Mon Sep 17 00:00:00 2001 From: cmoinon Date: Fri, 8 Jul 2016 10:52:23 +0200 Subject: [PATCH 4/6] Add a parameter to disable order synchronization --- includes/class-dolibarr.php | 13 ++++++++++--- includes/class-wc-integration.php | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/includes/class-dolibarr.php b/includes/class-dolibarr.php index 9ddff51..25a464f 100644 --- a/includes/class-dolibarr.php +++ b/includes/class-dolibarr.php @@ -79,6 +79,14 @@ public function dolibarr_create_order() { * We use non WSDL mode to workaround Dolibarr broken declaration marking all the fields as required * when they're not. */ + + // if no synchronization order with Dolibarr + if ( $this->settings->dolibarr_order_sync === 'no' ) { + // Do nothing + return ; + } + + try { $soap_client = new SoapClient( null, @@ -110,12 +118,11 @@ public function dolibarr_create_order() { $order->thirdparty_id = $thirdparty_id; } else { if ( 0 === intval( get_user_meta( $user_id, 'billing_company', true ) ) ) { - // CMOINON (OPEN-DSI) Correctif //if ( wp_verify_nonce( 'woocommerce-cart' ) ) { - /* Suppression de lal vérification => contrôle à revoir dans un deuxième temps - if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'] , 'woocommerce-process_checkout')) { + // Suppression de lal vérification => contrôle à revoir dans un deuxième temps +/* if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'] , 'woocommerce-process_checkout')) { $billing_company = $_POST['billing_company']; } else { // TODO: fail message? diff --git a/includes/class-wc-integration.php b/includes/class-wc-integration.php index ecf1e7d..3d2cf83 100644 --- a/includes/class-wc-integration.php +++ b/includes/class-wc-integration.php @@ -63,6 +63,9 @@ class Doliwoo_WC_Integration extends WC_Integration { /** @var int[] The distant Dolibarr version */ private $dolibarr_version; + /** @var boolean Parameter to disable order synchronization */ + public $dolibarr_order_sync; + /** * Init and hook in the integration. */ @@ -85,6 +88,7 @@ public function __construct() { $this->dolibarr_entity = $this->get_option( 'dolibarr_entity' ); $this->dolibarr_category_id = $this->get_option( 'dolibarr_category_id' ); $this->dolibarr_generic_id = $this->get_option( 'dolibarr_generic_id' ); + $this->dolibarr_order_sync = $this->get_option( 'dolibarr_order_sync' ); // Actions add_action( @@ -176,6 +180,16 @@ public function init_form_fields() { 'desc_tip' => false, 'default' => '', ), + + 'dolibarr_order_sync' => array( + 'title' => __( 'Order synchronisation', 'doliwoo' ), + 'desc' => __( 'Dolibarr order synchronisation', 'doliwoo' ), + 'description' => __( 'If is checked the orders will be synchronized with Dolibarr ', 'doliwoo' ), + 'type' => 'checkbox', + 'checkboxgroup' => 'start', + 'desc_tip' => false, + 'default' => 'yes' + ), 'dolibarr_version' => array( 'title' => __( 'Dolibarr version', 'doliwoo' ), 'description' => __( 'If the webservice communication is OK, it displays your Dolibarr version', 'doliwoo' ), @@ -218,7 +232,7 @@ protected function generate_info_html( $key, $data ) { ?> - + get_tooltip_html( $data ) ); ?> From 5b27c658ddea880b305516175359295f2a555513 Mon Sep 17 00:00:00 2001 From: cmoinon Date: Fri, 8 Jul 2016 12:19:31 +0200 Subject: [PATCH 5/6] Add a parameter to disable image synchronization and correct the notice Trying to get property of non-object in /var/www/wpwoocommerce/wp-content/plugins/doliwoo/doliwoo.php on line 214 --- doliwoo.php | 1 + includes/class-dolibarr.php | 16 ++++++++++------ includes/class-wc-integration.php | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/doliwoo.php b/doliwoo.php index 966544b..946e59a 100644 --- a/doliwoo.php +++ b/doliwoo.php @@ -211,6 +211,7 @@ public function add_integration( $integrations ) { * @return void */ public function reschedule_import_products() { + $this->settings = new Doliwoo_WC_Integration(); $delay = $this->settings->delay_update; wp_clear_scheduled_hook( 'import_products' ); wp_schedule_event( time(), $delay, 'import_products' ); diff --git a/includes/class-dolibarr.php b/includes/class-dolibarr.php index 25a464f..3caf9e1 100644 --- a/includes/class-dolibarr.php +++ b/includes/class-dolibarr.php @@ -368,6 +368,7 @@ private function create_order_lines( $order ) { * @return void */ public function dolibarr_import_products() { + try { $soap_client = new SoapClient( $this->ws_endpoint . self::PRODUCT_ENDPOINT . self::WSDL_MODE @@ -507,13 +508,16 @@ private function update_product_attributes( $dolibarr_product, $post_id ) { } } - // Check if the product has images - $the_product = $this->dolibarr_images_exit($dolibarr_product->id); + // If synchronize images with Dolibarr + if ( 'yes' === $this->settings->dolibarr_images_sync ) { + // Check if the product has images + $the_product = $this->dolibarr_images_exit($dolibarr_product->id); - // Product images management - if ( $the_product && ! empty($the_product->images) ) { - $this->import_product_images( $the_product, $post_id ); - } + // Product images management + if ( $the_product && ! empty($the_product->images) ) { + $this->import_product_images( $the_product, $post_id ); + } + } // Cleanup wc_delete_product_transients( $post_id ); diff --git a/includes/class-wc-integration.php b/includes/class-wc-integration.php index 3d2cf83..dcaf5dd 100644 --- a/includes/class-wc-integration.php +++ b/includes/class-wc-integration.php @@ -66,6 +66,9 @@ class Doliwoo_WC_Integration extends WC_Integration { /** @var boolean Parameter to disable order synchronization */ public $dolibarr_order_sync; + /** @var boolean Parameter to disable images load */ + public $dolibarr_images_sync; + /** * Init and hook in the integration. */ @@ -89,6 +92,7 @@ public function __construct() { $this->dolibarr_category_id = $this->get_option( 'dolibarr_category_id' ); $this->dolibarr_generic_id = $this->get_option( 'dolibarr_generic_id' ); $this->dolibarr_order_sync = $this->get_option( 'dolibarr_order_sync' ); + $this->dolibarr_images_sync = $this->get_option( 'dolibarr_images_sync' ); // Actions add_action( @@ -189,7 +193,16 @@ public function init_form_fields() { 'checkboxgroup' => 'start', 'desc_tip' => false, 'default' => 'yes' - ), + ), + 'dolibarr_images_sync' => array( + 'title' => __( 'Images synchronisation', 'doliwoo' ), + 'desc' => __( 'Dolibarr images synchronisation', 'doliwoo' ), + 'description' => __( 'If is checked the images of product in Dolibarr will be loaded in WooCommerce', 'doliwoo' ), + 'type' => 'checkbox', + 'checkboxgroup' => 'start', + 'desc_tip' => false, + 'default' => 'yes' + ), 'dolibarr_version' => array( 'title' => __( 'Dolibarr version', 'doliwoo' ), 'description' => __( 'If the webservice communication is OK, it displays your Dolibarr version', 'doliwoo' ), From a8e618fe1fe75eba5d091980aa5b6491cfed361e Mon Sep 17 00:00:00 2001 From: cmoinon Date: Fri, 8 Jul 2016 17:13:14 +0200 Subject: [PATCH 6/6] Return price or price_net depending of the configuration of Woocommerce or multiprice ou multiprice_ttc depending of a parameter in doliwoo dolibar_price_level add --- includes/class-dolibarr.php | 41 +++++++++++++++++++++++++------ includes/class-wc-integration.php | 14 +++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/includes/class-dolibarr.php b/includes/class-dolibarr.php index 3caf9e1..17d6a56 100644 --- a/includes/class-dolibarr.php +++ b/includes/class-dolibarr.php @@ -483,13 +483,40 @@ private function update_product_attributes( $dolibarr_product, $post_id ) { add_post_meta( $post_id, 'dolibarr_type', $dolibarr_product->type, true ); update_post_meta( $post_id, '_sku', $dolibarr_product->ref ); update_post_meta( $post_id, '_purchase_note', $dolibarr_product->note ); -/* update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net ); - update_post_meta( $post_id, '_sale_price', $dolibarr_product->price_net ); - update_post_meta( $post_id, '_price', $dolibarr_product->price_net );*/ - // Update in July 2016 CMOINON => get the price instead if the price_net witch is false with de webservice - // if we have multiple-price - update_post_meta( $post_id, '_regular_price', $dolibarr_product->price ); - update_post_meta( $post_id, '_price', $dolibarr_product->price ); + + + // Update in July 2016 CMOINON => + // if in woocommerce price not include tax + // => if not price_list => price_net else multiprice[level_price] + // else woocommerce price include tax + // => if not price_list => price else multiprice_ttc[level_price] + + $level = $this->settings->dolibarr_price_level ; + + if ($level > count($dolibarr_product->multiprices)) { + $level = ''; + } + if ( 'no' === get_option( 'woocommerce_prices_include_tax' ) ) { + if ( '' === $level) { + update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net ); + update_post_meta( $post_id, '_sale_price', $dolibarr_product->price_net ); + update_post_meta( $post_id, '_price', $dolibarr_product->price_net ); + } else { + update_post_meta( $post_id, '_regular_price', $dolibarr_product->multiprices[$level-1] ); + update_post_meta( $post_id, '_sale_price', $dolibarr_product->multiprices[$level-1] ); + update_post_meta( $post_id, '_price', $dolibarr_product->multiprices[$level-1] ); + } + } else { + if ( '' === $level) { + update_post_meta( $post_id, '_regular_price', $dolibarr_product->price ); + update_post_meta( $post_id, '_sale_price', $dolibarr_product->price ); + update_post_meta( $post_id, '_price', $dolibarr_product->price ); + } else { + update_post_meta( $post_id, '_regular_price', $dolibarr_product->multiprices_ttc[$level-1] ); + update_post_meta( $post_id, '_sale_price', $dolibarr_product->multiprices_ttc[$level-1] ); + update_post_meta( $post_id, '_price', $dolibarr_product->multiprices_ttc[$level-1] ); + } + } update_post_meta( $post_id, '_visibility', 'visible' ); update_post_meta( diff --git a/includes/class-wc-integration.php b/includes/class-wc-integration.php index dcaf5dd..abaa577 100644 --- a/includes/class-wc-integration.php +++ b/includes/class-wc-integration.php @@ -69,6 +69,9 @@ class Doliwoo_WC_Integration extends WC_Integration { /** @var boolean Parameter to disable images load */ public $dolibarr_images_sync; + /** @var int Price level to load */ + public $dolibarr_price_level; + /** * Init and hook in the integration. */ @@ -93,6 +96,7 @@ public function __construct() { $this->dolibarr_generic_id = $this->get_option( 'dolibarr_generic_id' ); $this->dolibarr_order_sync = $this->get_option( 'dolibarr_order_sync' ); $this->dolibarr_images_sync = $this->get_option( 'dolibarr_images_sync' ); + $this->dolibarr_price_level = $this->get_option( 'dolibarr_price_level' ); // Actions add_action( @@ -185,12 +189,19 @@ public function init_form_fields() { 'default' => '', ), + 'dolibarr_price_level' => array( + 'title' => __( 'Price Level for price', 'doliwoo' ), + 'description' => __( 'The level of price to load for the product. If no price list, leave blank.', 'doliwoo' ), + 'type' => 'text', + 'desc_tip' => false, + 'default' => '', + ), + 'dolibarr_order_sync' => array( 'title' => __( 'Order synchronisation', 'doliwoo' ), 'desc' => __( 'Dolibarr order synchronisation', 'doliwoo' ), 'description' => __( 'If is checked the orders will be synchronized with Dolibarr ', 'doliwoo' ), 'type' => 'checkbox', - 'checkboxgroup' => 'start', 'desc_tip' => false, 'default' => 'yes' ), @@ -199,7 +210,6 @@ public function init_form_fields() { 'desc' => __( 'Dolibarr images synchronisation', 'doliwoo' ), 'description' => __( 'If is checked the images of product in Dolibarr will be loaded in WooCommerce', 'doliwoo' ), 'type' => 'checkbox', - 'checkboxgroup' => 'start', 'desc_tip' => false, 'default' => 'yes' ),