From f0e33525a0e8dad15d03cbbfcb58663a7abf4350 Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Thu, 20 Aug 2020 16:09:07 +0200 Subject: [PATCH 1/8] switch the request --- src/Calculator/StatisticsCalculator.php | 47 ++++++------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index 6bdcff32..d6f6148c 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -201,44 +201,19 @@ public function getProductImage($productDetails) */ public function computeConversionByProduct($id_product, $id_product_attribute, $dateStart = null) { - $queryCarts = new \DbQuery(); - $queryCarts->select('id_cart'); - $queryCarts->from('blockwishlist_statistics'); - $queryCarts->where('id_cart != 0'); - $queryCarts->where('id_product = ' . $id_product); - $queryCarts->where('id_product_attribute = ' . $id_product_attribute); - + $queryOrders = ' + SELECT count(os.id_order_state) + FROM ' . _DB_PREFIX_ . 'orders o + LEFT JOIN ' . _DB_PREFIX_ . 'order_history oh ON (o.`id_order` = oh.`id_order`) + LEFT JOIN ' . _DB_PREFIX_ . 'order_state os ON (os.`id_order_state` = oh.`id_order_state` AND os.`paid` = 1 AND os.`shipped` = 1) + LEFT JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . ') + LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order`) + '; if (null != $dateStart) { - $queryCarts->where('date_add >= "' . $dateStart . '"'); + $queryOrders .= 'WHERE bws.date_add >= "' . $dateStart . '"'; } - $carts = \Db::getInstance()->executeS($queryCarts); - $nbCartPaidAndShipped = 0; - - foreach ($carts as $cart) { - $queryOrder = new \DbQuery(); - $queryOrder->select('id_order'); - $queryOrder->from('orders'); - $queryOrder->where('id_cart = ' . $cart['id_cart']); - $orderID = \Db::getInstance()->getRow($queryOrder); - - if (empty($orderID)) { - continue; - } - - $order = new \Order($orderID['id_order']); - - if ($order->hasBeenPaid() >= 1) { - foreach ($order->getProducts() as $product) { - // if product/attribute combination is still in the order - if ($product['product_id'] == $id_product - && $product['product_attribute_id'] == $id_product_attribute - ) { - ++$nbCartPaidAndShipped; - } - } - } - } + $nbOrderPaidAndShipped = Db::getInstance()->getRow($queryOrders); $queryCountAll = new \DbQuery(); $queryCountAll->select('COUNT(id_statistics)'); @@ -252,6 +227,6 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $countAddedToWishlist = \Db::getInstance()->getValue($queryCountAll); - return round(($nbCartPaidAndShipped / $countAddedToWishlist) * 100, 2); + return round(($nbOrderPaidAndShipped / $countAddedToWishlist) * 100, 2); } } From 3e98223dd700d66a1fe18af80061f01eb85ecc7a Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Thu, 20 Aug 2020 16:52:09 +0200 Subject: [PATCH 2/8] fix stan --- src/Calculator/StatisticsCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index d6f6148c..539c1a5b 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -213,7 +213,7 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $queryOrders .= 'WHERE bws.date_add >= "' . $dateStart . '"'; } - $nbOrderPaidAndShipped = Db::getInstance()->getRow($queryOrders); + $nbOrderPaidAndShipped = \Db::getInstance()->getRow($queryOrders); $queryCountAll = new \DbQuery(); $queryCountAll->select('COUNT(id_statistics)'); From 0f793bdb9cc332add2954e838f07e6fcbe3134c8 Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Wed, 26 Aug 2020 15:12:37 +0200 Subject: [PATCH 3/8] fix sql --- src/Calculator/StatisticsCalculator.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index 539c1a5b..b6598b78 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -201,16 +201,19 @@ public function getProductImage($productDetails) */ public function computeConversionByProduct($id_product, $id_product_attribute, $dateStart = null) { + $nbOrderPaidAndShipped = []; $queryOrders = ' - SELECT count(os.id_order_state) + SELECT count(distinct(o.id_order)) as nb FROM ' . _DB_PREFIX_ . 'orders o + LEFT JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart ) LEFT JOIN ' . _DB_PREFIX_ . 'order_history oh ON (o.`id_order` = oh.`id_order`) LEFT JOIN ' . _DB_PREFIX_ . 'order_state os ON (os.`id_order_state` = oh.`id_order_state` AND os.`paid` = 1 AND os.`shipped` = 1) - LEFT JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . ') - LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order`) + LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = ' . (int) $id_product . ' AND od.`product_attribute_id` = ' . (int) $id_product_attribute . ') + WHERE bws.`id_cart` <> 0 AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . ' '; + if (null != $dateStart) { - $queryOrders .= 'WHERE bws.date_add >= "' . $dateStart . '"'; + $queryOrders .= 'AND bws.date_add >= "' . $dateStart . '"'; } $nbOrderPaidAndShipped = \Db::getInstance()->getRow($queryOrders); @@ -227,6 +230,6 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $countAddedToWishlist = \Db::getInstance()->getValue($queryCountAll); - return round(($nbOrderPaidAndShipped / $countAddedToWishlist) * 100, 2); + return round(($nbOrderPaidAndShipped['nb'] / $countAddedToWishlist) * 100, 2); } } From 2b6a48e76a34f59d0c56581e31cc6721655b567c Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Wed, 26 Aug 2020 15:38:26 +0200 Subject: [PATCH 4/8] add condition if no result --- src/Calculator/StatisticsCalculator.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index b6598b78..2b2889c6 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -218,6 +218,10 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $nbOrderPaidAndShipped = \Db::getInstance()->getRow($queryOrders); + if(empty($nbOrderPaidAndShipped['nb'])) { + return 0; + } + $queryCountAll = new \DbQuery(); $queryCountAll->select('COUNT(id_statistics)'); $queryCountAll->from('blockwishlist_statistics'); From 41eefaa9636a124fe53236e99933aa994ba928e0 Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Wed, 26 Aug 2020 15:38:52 +0200 Subject: [PATCH 5/8] fix ci --- src/Calculator/StatisticsCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index 2b2889c6..fa00c0bb 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -218,7 +218,7 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $nbOrderPaidAndShipped = \Db::getInstance()->getRow($queryOrders); - if(empty($nbOrderPaidAndShipped['nb'])) { + if (empty($nbOrderPaidAndShipped['nb'])) { return 0; } From 19ce70408c2cb9dcc4c3932fe5f69f9b022068fa Mon Sep 17 00:00:00 2001 From: pablopolyte <31857168+pablopolyte@users.noreply.github.com> Date: Wed, 26 Aug 2020 17:04:53 +0200 Subject: [PATCH 6/8] Update src/Calculator/StatisticsCalculator.php Co-authored-by: GoT --- src/Calculator/StatisticsCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index fa00c0bb..b334ab8c 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -208,7 +208,7 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ LEFT JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart ) LEFT JOIN ' . _DB_PREFIX_ . 'order_history oh ON (o.`id_order` = oh.`id_order`) LEFT JOIN ' . _DB_PREFIX_ . 'order_state os ON (os.`id_order_state` = oh.`id_order_state` AND os.`paid` = 1 AND os.`shipped` = 1) - LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = ' . (int) $id_product . ' AND od.`product_attribute_id` = ' . (int) $id_product_attribute . ') + LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = bws.`id_product` AND od.`product_attribute_id` = bws.`id_product_attribute`' WHERE bws.`id_cart` <> 0 AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . ' '; From 1b23074f46c70e716a0ecc03b8a018789a984ed4 Mon Sep 17 00:00:00 2001 From: pablopolyte <31857168+pablopolyte@users.noreply.github.com> Date: Wed, 26 Aug 2020 17:05:07 +0200 Subject: [PATCH 7/8] Update src/Calculator/StatisticsCalculator.php Co-authored-by: GoT --- src/Calculator/StatisticsCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index b334ab8c..2dde747e 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -205,7 +205,7 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ $queryOrders = ' SELECT count(distinct(o.id_order)) as nb FROM ' . _DB_PREFIX_ . 'orders o - LEFT JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart ) + INNER JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart ) LEFT JOIN ' . _DB_PREFIX_ . 'order_history oh ON (o.`id_order` = oh.`id_order`) LEFT JOIN ' . _DB_PREFIX_ . 'order_state os ON (os.`id_order_state` = oh.`id_order_state` AND os.`paid` = 1 AND os.`shipped` = 1) LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = bws.`id_product` AND od.`product_attribute_id` = bws.`id_product_attribute`' From cafc97635f9ebf9838543f9b3bb257da0f99d894 Mon Sep 17 00:00:00 2001 From: pablopolyte Date: Wed, 26 Aug 2020 17:07:13 +0200 Subject: [PATCH 8/8] fix quote in sql --- src/Calculator/StatisticsCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculator/StatisticsCalculator.php b/src/Calculator/StatisticsCalculator.php index 2dde747e..e70c4adc 100644 --- a/src/Calculator/StatisticsCalculator.php +++ b/src/Calculator/StatisticsCalculator.php @@ -208,7 +208,7 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $ INNER JOIN ' . _DB_PREFIX_ . 'blockwishlist_statistics bws ON (o.id_cart = bws.id_cart ) LEFT JOIN ' . _DB_PREFIX_ . 'order_history oh ON (o.`id_order` = oh.`id_order`) LEFT JOIN ' . _DB_PREFIX_ . 'order_state os ON (os.`id_order_state` = oh.`id_order_state` AND os.`paid` = 1 AND os.`shipped` = 1) - LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = bws.`id_product` AND od.`product_attribute_id` = bws.`id_product_attribute`' + LEFT JOIN ' . _DB_PREFIX_ . 'order_detail od ON (od.`id_order` = o.`id_order` AND od.`product_id` = bws.`id_product` AND od.`product_attribute_id` = bws.`id_product_attribute` WHERE bws.`id_cart` <> 0 AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . ' ';