public function __construct()
 {
     global $wpdb;
     $theme = $wpdb->get_row("SELECT * FROM {$wpdb->options} WHERE option_name = 'current_theme'", ARRAY_A);
     // Ligne à retirer à l'avenir
     $this->software = $theme['option_value'];
     // Définir les variables avec les résultats de la base
     if ('shopperpress' == $theme['option_value']) {
         $this->software = $theme['option_value'];
         $this->setVersion();
         $this->supportComments = false;
     } else {
         if (is_plugin_active_custom("woocommerce/woocommerce.php")) {
             $this->software = "Woocommerce";
             $this->setVersion();
             $this->supportComments = true;
         } else {
             if (is_plugin_active_custom("shopp/Shopp.php")) {
                 $this->software = "Shopp";
                 $this->setVersion();
                 $this->supportComments = false;
             } else {
                 if (is_plugin_active_custom("wp-e-commerce/wp-shopping-cart.php")) {
                     $this->software = "WP eCommerce";
                     $this->setVersion();
                     $this->supportComments = false;
                 } else {
                     if (is_plugin_active_custom("cart66-lite/cart66.php")) {
                         $this->software = "Cart66 Lite";
                         $this->setVersion();
                         $this->supportComments = false;
                     } else {
                         if (is_plugin_active_custom("cart66.php")) {
                             $this->software = "Cart66 Pro";
                             $this->setVersion();
                             $this->supportComments = false;
                         } else {
                             if (is_plugin_active_custom("jigoshop/jigoshop.php")) {
                                 $this->software = "Jigoshop";
                                 $this->setVersion();
                                 $this->supportComments = true;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 protected function setInfoWoocommerce()
 {
     include_once PLUGIN_PATH_SHIPWORKSWORDPRESS . 'functions/woocommerce/functionsWoocommerce.php';
     $time = strtotime($this->date . ' UTC');
     $this->date = date("y-m-d", $time);
     global $wpdb;
     $table = $wpdb->prefix . "posts";
     $tracking_number = $this->tracking;
     //checking the identify shipping company
     $usps_pattern = $this->usps_pattern;
     $ups_pattern = $this->ups_pattern;
     $fedex_pattern = $this->fedex_pattern;
     /*if ( preg_match( $usps_pattern, $tracking_number ) ) { //test USPS
     			$tracking_name = 'usps';
     		} elseif( preg_match( $fedex_pattern, $tracking_number ) ) { //test Fedex
     			$tracking_name = 'fedex';
     		} elseif( preg_match( $ups_pattern, $tracking_number ) ) { //test Ups
     			$tracking_name = 'ups';
     		}*/
     $tracking_name = strtolower($this->carrier);
     // On ne veut plus utiliser les filtres, on va directement récupérer le nom du carier depuis la requete
     // Avant de mettre à jour on veut retrouver le bon order_number et pas celui de sequential woocommerce
     if (is_plugin_active_custom("woocommerce-sequential-order-numbers/woocommerce-sequential-order-numbers.php") || is_plugin_active_custom("woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php")) {
         $row = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_order_number' and meta_value = " . $this->order, ARRAY_A);
         if ($row == null) {
             $this->result = false;
             $this->code = 'ERR004';
             $this->description = 'The order is not in the Database';
         } else {
             $id = $row['post_id'];
             $this->order = $id;
         }
     }
     // Cheking if the order is in the database
     $row = $wpdb->get_row("SELECT * FROM " . $table . " WHERE id = " . $this->order, ARRAY_A);
     if (!$row) {
         $this->result = false;
         $this->code = 'ERR004';
         $this->description = 'The order is not in the Database';
     } else {
         if (false) {
             $this->result = false;
             $this->code = 'ERR005';
             $this->description = "Carrier Company didn't find";
         } else {
             $note = "Your order was shipped on " . $this->date . " via " . $this->carrier . ". Tracking number is " . $this->tracking . ".";
             // On regarde si le plugin Tracking Shipments est actif ou pas, auquel cas on doit insérer le tracking number à un autre endroit que dans les notes
             if (is_plugin_active_custom("woocommerce-shipment-tracking/shipment-tracking.php")) {
                 $newDate = date("y-m-d", strtotime($this->date));
                 $table = $wpdb->prefix . "postmeta";
                 $this->result = $wpdb->replace($table, array('post_id' => $id, 'meta_key' => '_tracking_number', 'meta_value' => $tracking_number));
                 $wpdb->update($table, array('meta_value' => $tracking_number), array('post_id' => $id, 'meta_key' => '_tracking_number'));
                 $wpdb->replace($table, array('post_id' => $id, 'meta_key' => '_tracking_provider', 'meta_value' => $this->carrier));
                 $wpdb->update($table, array('meta_value' => $this->carrier), array('post_id' => $id, 'meta_key' => '_tracking_provider'));
                 $wpdb->replace($table, array('post_id' => $id, 'meta_key' => '_custom_tracking_provider', 'meta_value' => $this->carrier));
                 $wpdb->update($table, array('meta_value' => $this->carrier), array('post_id' => $id, 'meta_key' => '_custom_tracking_provider'));
                 $wpdb->replace($table, array('post_id' => $id, 'meta_key' => '_date_shipped', 'meta_value' => strtotime($this->date)));
                 $wpdb->update($table, array('meta_value' => strtotime($this->date)), array('post_id' => $id, 'meta_key' => '_date_shipped'));
             } else {
                 $this->result = add_customer_note($note, $this->order);
             }
             //$this->result = add_customer_note( $note, $this->order );
             if ($this->result === false) {
                 $this->code = 'ERR010';
                 $this->description = "The tracking number coudn't be insert in the database.";
             }
         }
     }
 }
 protected function setInfoWoocommerce()
 {
     include_once PLUGIN_PATH_SHIPWORKSWORDPRESS . 'functions/woocommerce/functionsWoocommerce.php';
     $this->itemID = $this->row['order_item_id'];
     $this->productID = getItemInfo($this->row, '_product_id');
     if (null == (int) getItemInfo($this->row, '_variation_id')) {
         // Dans ce cas le variation Id vaut l'id du produit ce qui est bon
         $variationId = getItemInfo($this->row, '_product_id');
     } else {
         // Dans ce cas l'id est celui de la variation qui va permettre d'aller cherche le sku et le prix
         $variationId = getItemInfo($this->row, '_variation_id');
     }
     // On ajoute les attributs
     global $wpdb;
     $table = $wpdb->prefix . "woocommerce_order_itemmeta";
     $results = $wpdb->get_results("SELECT * FROM " . $table . " WHERE order_item_id = " . $this->row['order_item_id'], ARRAY_A);
     foreach ($results as $row) {
         if (substr($row['meta_key'], 0, 1) != "_") {
             // On regarde si pour cet item il existe des extra options : différent de 0
             // Si oui on enlève les champs qui on un id supérieur à celui de la ligne _tmcartepo_data : le plugin est vraiment mal fichu en base de donnée
             if (isAttributeTMOption($this->row['order_item_id']) != 0) {
                 if ($row['meta_id'] < isAttributeTMOption($this->row['order_item_id'])) {
                     array_push($this->attributes, new Attribute($this->software, $this->date, $row['meta_key'], $row['meta_value']));
                 }
             } else {
                 array_push($this->attributes, new Attribute($this->software, $this->date, $row['meta_key'], $row['meta_value']));
             }
         }
     }
     // On ajoute les attributs dans le cas ou on a des Extra product Options
     if (isAttributeTMOption($this->row['order_item_id']) != 0) {
         $tab = getTMOptionTab($this->row['order_item_id']);
         foreach ($tab as $option) {
             array_push($this->attributes, new Attribute($this->software, $this->date, $option['value'], 'Extra Options(s)', $option['price']));
         }
     }
     // On veut dans tous les cas enregistrer l'id du produit original pour avoir le bon nom
     $productId = getItemInfo($this->row, '_product_id');
     if (null != getProductInfo($variationId, '_sku')) {
         $this->code = getProductInfo($variationId, '_sku');
         $this->sku = getProductInfo($variationId, '_sku');
     } else {
         $this->code = getProductInfo($productId, '_sku');
         $this->sku = getProductInfo($productId, '_sku');
     }
     $this->name = getProductName($productId);
     $this->quantity = getItemInfo($this->row, '_qty');
     // Cas ou on a woocommerce Composite Products
     $this->price = getItemInfo($this->row, '_line_total');
     if (isComposed($this->row)) {
         $this->unitprice = 0;
     } else {
         // On regarde si il y a le plugin woocommerce-bulk-discount woocommerce-bulk-discount.php
         if (is_plugin_active_custom("woocommerce-bulk-discount/woocommerce-bulk-discount.php")) {
             //$this->unitprice = getProductInfo( $variationId, '_price' );
             // On met le prix au cas où le variation id ne soit pas présent dans le champ
             $this->unitprice = getProductInfo($variationId, '_price');
             $bulkInfo = getProductInfo($this->row['order_id'], '_woocommerce_t4m_discount_coeffs');
             if ($bulkInfo) {
                 $bulkData = json_decode($bulkInfo);
                 foreach ($bulkData as $key => $value) {
                     if ($key == $variationId && $value->coeff != 1) {
                         // Si le variation id est présent on remplace le prix
                         $this->unitprice = $value->coeff * $value->orig_price;
                         //var_dump( $value->coeff );
                     }
                 }
                 //var_dump( $bulkData );
             }
             //echo 'nico' . $bulkInfo;
         } else {
             $this->unitprice = getProductInfo($variationId, '_price');
         }
     }
     // Si le poid du variation vaut 0 on prend celui du parent
     if (0 != getProductInfo($variationId, '_weight')) {
         $this->weight = wooWeightNormal(getProductInfo($variationId, '_weight'), 'lbs');
     } else {
         $this->weight = wooWeightNormal(getProductInfo($productId, '_weight'), 'lbs');
     }
     // Les images
     $image = wp_get_attachment_image_src(get_post_thumbnail_id($variationId), 'Single Thumbs');
     $imageThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($variationId), 'Product Thumbs');
     if ($image[0] == null) {
         // Cas ou on a pas d'image pour le variation product
         $image = wp_get_attachment_image_src(get_post_thumbnail_id($this->productID), 'Single Thumbs');
     }
     if ($imageThumbnail[0] == null) {
         // Cas ou on a pas d'image pour le variation product
         $imageThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($this->productID), 'Product Thumbs');
     }
     $this->image = $image[0];
     $this->imageThumbnail = $imageThumbnail[0];
 }
 protected function setInfoWoocommerce()
 {
     include_once PLUGIN_PATH_SHIPWORKSWORDPRESS . 'functions/woocommerce/functionsWoocommerce.php';
     include_once PLUGIN_PATH_SHIPWORKSWORDPRESS . '../woocommerce/woocommerce.php';
     if ((is_plugin_active_custom("woocommerce-sequential-order-numbers/woocommerce-sequential-order-numbers.php") || is_plugin_active_custom("woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php")) && is_numeric(getInformation($this->row, '_order_number'))) {
         $this->id_order = $seqOrderProPrefix . getInformation($this->row, '_order_number');
     } else {
         $this->id_order = $this->row['ID'];
     }
     $this->createdDate = gmdate("Y-m-d\\TH:i:s\\Z", strtotime($this->row['post_date_gmt']));
     $this->modifiedDate = gmdate("Y-m-d\\TH:i:s\\Z", strtotime($this->row['post_modified_gmt']));
     $split = explode('.', $this->software->getVersion());
     $this->shipoption = getInformation($this->row, '_shipping_method_title');
     if ($split[0] >= 2 && $split[1] >= 1 && $split[2] >= 2) {
         if (null != getShippingInfo($this->row)) {
             $this->shipoption = getShippingInfo($this->row);
         }
     }
     $this->status = getStatus($this->software, $this->row);
     $this->firstname = getInformation($this->row, '_billing_first_name');
     $this->middlename = '';
     $this->lastname = getInformation($this->row, '_billing_last_name');
     $this->company = getInformation($this->row, '_billing_company');
     $this->address = getInformation($this->row, '_billing_address_1');
     $this->xaddress = '';
     $this->street2 = getInformation($this->row, '_billing_address_2');
     $this->street3 = '';
     $this->city = getInformation($this->row, '_billing_city');
     $this->state = getInformation($this->row, '_billing_state');
     $this->postcode = getInformation($this->row, '_billing_postcode');
     $this->country = getInformation($this->row, '_billing_country');
     if ('' != $this->company) {
         $this->residential = 'false';
     }
     $this->phone = getInformation($this->row, '_billing_phone');
     $this->email = getInformation($this->row, '_billing_email');
     $this->fax = '';
     $this->website = '';
     $this->shipfirstname = getInformation($this->row, '_shipping_first_name');
     $this->shiplastname = getInformation($this->row, '_shipping_last_name');
     $this->shipCompany = getInformation($this->row, '_shipping_company');
     $this->shipaddress = getInformation($this->row, '_shipping_address_1');
     $this->shipstreet2 = getInformation($this->row, '_shipping_address_2');
     $this->shipxaddress = getInformation($this->row, '_shipping_address_1');
     $this->shipcity = getInformation($this->row, '_shipping_city');
     $this->shipstate = getInformation($this->row, '_shipping_state');
     $this->shippostcode = getInformation($this->row, '_shipping_postcode');
     $this->shipcountry = getInformation($this->row, '_shipping_country');
     $this->cardtype = getInformation($this->row, '_payment_method_title');
     $this->freight = getInformation($this->row, '_order_shipping');
     // Shipping Fee
     $this->tax = (double) getInformation($this->row, '_order_tax') + (double) getInformation($this->row, '_order_shipping_tax');
     //Tax Fee
     $this->discount = (double) getInformation($this->row, '_order_discount') + (double) getInformation($this->row, '_cart_discount');
     // Discount
     $this->fees = '';
     // Add Fee
     // Ajout des coupons
     if (getCoupons($this->row) != null) {
         $coupons = getCoupons($this->row);
         foreach ($coupons as $coupon) {
             /*var_dump( $coupon );*/
             array_push($this->coupons, 'Coupon : ' . $coupon['order_item_name']);
         }
     }
     // Ajout des notes
     $notes = getOrderNotes($this->row['ID']);
     foreach ($notes as $note) {
         if (getNotePrivacy($note['comment_ID']) == 1) {
             /*echo 'ok' . $note['comment_ID'] . $note['comment_content'];*/
             array_push($this->coupons, $note['comment_content']);
         } else {
             if (getNotePrivacy($note['comment_ID']) == 0) {
                 array_push($this->privateNotes, $note['comment_content']);
             }
         }
     }
     //Ajout les champs du custom checkout field si le plugin est la
     if (is_plugin_active_custom("woocommerce-checkout-field-editor/checkout-field-editor.php")) {
         global $wpdb;
         $table = $wpdb->prefix . "options";
         // On list les champs additionnels qui existent
         $row = $wpdb->get_row("SELECT * FROM " . $table . " WHERE option_name = 'wc_fields_additional'", ARRAY_A);
         $fields = unserialize($row["option_value"]);
         $table = $wpdb->prefix . "postmeta";
         foreach ($fields as $key => $value) {
             $row = $wpdb->get_row("SELECT * FROM " . $table . " WHERE post_id = " . $this->row['ID'] . " AND meta_key = '" . $key . "'", ARRAY_A);
             $value = $row["meta_value"];
             array_push($this->coupons, $value);
         }
     }
     global $wpdb;
     $time = strtotime($this->date . ' UTC');
     $dateInLocal = date("Y-m-d H:i:s", $time);
     $rows = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "woocommerce_order_items WHERE order_id = " . $this->row['ID'] . " AND order_item_type = 'line_item'", ARRAY_A);
     for ($k = 0; $k < count($rows); $k++) {
         array_push($this->items, new Item($this->software, $this->date, $rows[$k]));
     }
 }
 protected function setInfoWoocommerce2v2()
 {
     include_once PLUGIN_PATH_SHIPWORKSWORDPRESS . 'functions/woocommerce/functionsWoocommerce.php';
     global $wpdb;
     $status = $this->status;
     // Avant de mettre à jour on veut retrouver le bon order_number et pas celui de sequential woocommerce
     if (is_plugin_active_custom("woocommerce-sequential-order-numbers/woocommerce-sequential-order-numbers.php") || is_plugin_active_custom("woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php")) {
         $row = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_order_number' and meta_value = " . $this->order, ARRAY_A);
         if ($row != null) {
             $id = $row['post_id'];
             $this->order = $id;
         }
     }
     $table = $wpdb->prefix . "posts";
     $tab = array(0 => "pending", 1 => "failed", 2 => "on-hold", 3 => "processing", 4 => "completed", 5 => "refunded", 6 => "cancelled");
     if ($tab[$this->status] != null) {
         $this->result = $wpdb->update($table, array('post_status' => 'wc-' . $tab[$this->status]), array('ID' => $this->order));
     }
     // Si on a 0 ca veut just dire que la base de donnée était déjà à jour et qu'on avait rien à updater
     // Du coup on veut checker $this->result === false et pas $this->result === 0 car ca donnera false alors que c'est bon
     if ($this->result === false) {
         $this->code = 'ERR004';
         $this->description = "The Status coudn't be update in the database";
     } else {
         if ($this->comment != '') {
             add_private_note($this->comment, $this->order);
         }
     }
 }