Skip to content

Commit

Permalink
Merge pull request #77 from boboldehampsink/patch-1
Browse files Browse the repository at this point in the history
Check if commerce_orders table exists before setting foreign key + PostgreSQL fixes
  • Loading branch information
mediabeastnz authored Jan 4, 2024
2 parents 9c31563 + 6609b90 commit c94e008
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public function createTables()

public function addForeignKeys()
{
$this->addForeignKey(null, '{{%abandonedcart_carts}}', ['orderId'], '{{%commerce_orders}}', ['id'], 'CASCADE', null);
if ($this->_tableExists('{{%commerce_orders}}')) {
$this->addForeignKey(null, '{{%abandonedcart_carts}}', ['orderId'], '{{%commerce_orders}}', ['id'], 'CASCADE', null);
}
}

public function dropTables()
Expand Down
27 changes: 14 additions & 13 deletions src/services/Carts.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use DateTime;
use DateTimeZone;
use yii\base\Component;
use yii\db\Expression;

class Carts extends Component
{
Expand Down Expand Up @@ -53,7 +54,7 @@ public function scheduleReminders()
{
// get all created abandoned carts that havent been completed
// Completed being: reminders have already been sent
$carts = CartRecord::find()->where('isScheduled = 0')->all();
$carts = CartRecord::find()->where(['isScheduled' => 0])->all();

$firstDelay = Craft::parseEnv(AbandonedCart::$plugin->getSettings()->firstReminderDelay);
$secondDelay = Craft::parseEnv(AbandonedCart::$plugin->getSettings()->secondReminderDelay);
Expand Down Expand Up @@ -113,7 +114,7 @@ public function createNewCarts(\craft\commerce\elements\db\OrderQuery $orders)
foreach ($orders as $order) {
// check for existing cart first
// if none exist - create a new record
$existingCart = CartRecord::find()->where(['orderID' => $order->id])->one();
$existingCart = CartRecord::find()->where(['orderId' => $order->id])->one();
if (!$existingCart) {
$newCart = new CartRecord();
$newCart->orderId = $order->id;
Expand Down Expand Up @@ -148,15 +149,15 @@ public function getAbandonedOrders($start = '1', $end = '12')
$dateUpdatedEnd->sub(new DateInterval('PT'.$end.'H'));

$carts = Order::find();
$carts->where(['<=', 'commerce_orders.dateUpdated', $dateUpdatedStart->format('Y-m-d H:i:s')]);
$carts->andWhere(['>=', 'commerce_orders.dateUpdated', $dateUpdatedEnd->format('Y-m-d H:i:s')]);
$carts->andWhere('totalPrice > 0');
$carts->andWhere('isCompleted = 0');
$carts->andWhere('email != ""');
$carts->where(['<=', 'commerce_orders.[[dateUpdated]]', $dateUpdatedStart->format('Y-m-d H:i:s')]);
$carts->andWhere(['>=', 'commerce_orders.[[dateUpdated]]', $dateUpdatedEnd->format('Y-m-d H:i:s')]);
$carts->andWhere(['>', 'totalPrice', 0]);
$carts->andWhere(['=', 'isCompleted', 0]);
$carts->andWhere(['!=', 'email', '']);
if (is_array($blacklist)) {
$carts->andWhere(['not in', 'email', $blacklist]);
}
$carts->orderBy('commerce_orders.dateUpdated desc');
$carts->orderBy('commerce_orders.[[dateUpdated]] desc');
$carts->all();
return $carts;
}
Expand Down Expand Up @@ -204,7 +205,7 @@ public function getAbandonedCartsRecovered()
if($ids) {
$orders = Order::find()
->where(['commerce_orders.id' => $ids])
->select('SUM(totalPrice) as total')
->select(new Expression('SUM([[totalPrice]]) as total'))
->column();
return $orders[0];
}
Expand All @@ -216,12 +217,12 @@ public function getAbandonedCartsRecoveredThisMonth()
$ids = $this->_createAbandonedCartsQuery()
->select('orderId')
->where(['isRecovered' => 1])
->andWhere('MONTH(dateUpdated) = MONTH(CURDATE())')
->andWhere(new Expression('EXTRACT(MONTH FROM [[dateUpdated]]) = EXTRACT(MONTH FROM NOW())'))
->column();
if($ids) {
$orders = Order::find()
->where(['commerce_orders.id' => $ids])
->select('SUM(totalPrice) as total')
->select(new Expression('SUM(totalPrice) as total'))
->column();
return $orders[0];
}
Expand All @@ -240,7 +241,7 @@ public function getAbandonedCartsRecoveredCount()

public function getAbandondedCartsConversion()
{
$recovered = $this->_createAbandonedCartsQuery()->where('isRecovered = 1')->count();
$recovered = $this->_createAbandonedCartsQuery()->where(['isRecovered' => 1])->count();
$total = $this->getAbandonedCartsTotal();
if ($total > 0 && $recovered > 0) {
$percent = ($recovered / $total) * 100;
Expand Down Expand Up @@ -443,7 +444,7 @@ private function _createAbandonedCartsQuery(): Query
->select('*')
->from(['{{%abandonedcart_carts}}'])
->where(['not in', 'email', $blacklist])
->orderBy('dateUpdated desc');
->orderBy('[[dateUpdated]] desc');
}

}
2 changes: 1 addition & 1 deletion src/stats/TotalCartsRecovered.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getData(): mixed

// get all orders that are recovered
$query2 = $this->_createStatQuery();
$query2->select('SUM(totalPrice) as total');
$query2->select(new Expression('SUM([[totalPrice]]) as total'));
$query2->andWhere(['orders.id' => $cartIds]);
$total = $query2->scalar();

Expand Down

0 comments on commit c94e008

Please sign in to comment.