Skip to content

Commit

Permalink
Merge pull request #80 from pablopolyte/switchStatsRequest
Browse files Browse the repository at this point in the history
switch the request
  • Loading branch information
SZCZUPAK Valentin authored Aug 27, 2020
2 parents 84298b5 + cafc976 commit 842c11d
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions src/Calculator/StatisticsCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,43 +211,25 @@ 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);
$nbOrderPaidAndShipped = [];
$queryOrders = '
SELECT count(distinct(o.id_order)) as nb
FROM ' . _DB_PREFIX_ . 'orders o
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`
WHERE bws.`id_cart` <> 0 AND bws.`id_product` = ' . (int) $id_product . ' AND bws.`id_product_attribute` = ' . (int) $id_product_attribute . '
';

if (null != $dateStart) {
$queryCarts->where('date_add >= "' . $dateStart . '"');
$queryOrders .= 'AND bws.date_add >= "' . $dateStart . '"';
}

$carts = \Db::getInstance()->executeS($queryCarts);
$nbCartPaidAndShipped = 0;
$nbOrderPaidAndShipped = \Db::getInstance()->getRow($queryOrders);

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;
}
}
}
if (empty($nbOrderPaidAndShipped['nb'])) {
return 0;
}

$queryCountAll = new \DbQuery();
Expand All @@ -262,6 +244,6 @@ public function computeConversionByProduct($id_product, $id_product_attribute, $

$countAddedToWishlist = \Db::getInstance()->getValue($queryCountAll);

return round(($nbCartPaidAndShipped / $countAddedToWishlist) * 100, 2);
return round(($nbOrderPaidAndShipped['nb'] / $countAddedToWishlist) * 100, 2);
}
}

0 comments on commit 842c11d

Please sign in to comment.