Skip to content

Commit c324546

Browse files
authored
Merge pull request #2716 from themeum/harun-v4
Fix duplicate query execution on cart and checkout page.
2 parents c5d13a2 + 2fcd8a1 commit c324546

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

models/CartModel.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Tutor\Models;
1212

13+
use Tutor\Cache\TutorCache;
1314
use Tutor\Helpers\QueryHelper;
1415

1516
/**
@@ -101,7 +102,12 @@ public function add_course_to_cart( $user_id, $course_id, $item_type = '', $item
101102
* @return array Array containing the cart items and their total count.
102103
*/
103104
public function get_cart_items( $user_id, $is_details = true ) {
104-
global $wpdb;
105+
$cache_key = "tutor_get_cart_items_{$user_id}_{$is_details}";
106+
$cached = TutorCache::get( $cache_key );
107+
108+
if ( $cached ) {
109+
return $cached;
110+
}
105111

106112
$cart_data = array(
107113
'cart' => null,
@@ -112,7 +118,7 @@ public function get_cart_items( $user_id, $is_details = true ) {
112118
);
113119

114120
$user_cart = QueryHelper::get_row(
115-
"{$wpdb->prefix}tutor_carts",
121+
'tutor_carts',
116122
array(
117123
'user_id' => $user_id,
118124
),
@@ -122,11 +128,11 @@ public function get_cart_items( $user_id, $is_details = true ) {
122128
if ( $user_cart ) {
123129
$cart_data['cart'] = $user_cart;
124130

125-
$primary_table = "{$wpdb->prefix}tutor_cart_items AS item";
131+
$primary_table = 'tutor_cart_items AS item';
126132
$joining_tables = array(
127133
array(
128134
'type' => 'LEFT',
129-
'table' => "{$wpdb->prefix}posts AS post",
135+
'table' => 'posts AS post',
130136
'on' => 'item.course_id = post.ID',
131137
),
132138
);
@@ -142,7 +148,10 @@ public function get_cart_items( $user_id, $is_details = true ) {
142148
);
143149
}
144150

145-
return $is_details ? $cart_data : $cart_data['courses']['results'];
151+
$result = $is_details ? $cart_data : $cart_data['courses']['results'];
152+
TutorCache::set( $cache_key, $result );
153+
154+
return $result;
146155
}
147156

148157
/**

models/OrderModel.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Tutor\Models;
1212

1313
use Exception;
14+
use Tutor\Cache\TutorCache;
1415
use Tutor\Components\Badge;
1516
use Tutor\Components\Button;
1617
use Tutor\Components\Constants\Variant;
@@ -632,20 +633,23 @@ public function mark_as_paid( $order_id, $note = '', $trigger_hooks = true ) {
632633
*
633634
* @since 3.0.0
634635
*
635-
* @global wpdb $wpdb WordPress database abstraction object.
636-
*
637636
* @param int $order_id The ID of the order to retrieve items for.
638637
*
639638
* @return array The order items, each containing details and course titles, or an empty array if no items are found.
640639
*/
641640
public function get_order_items_by_id( $order_id ) {
642-
global $wpdb;
641+
$cache_key = 'tutor_get_order_items_by_id_' . $order_id;
642+
$cached = TutorCache::get( $cache_key );
643643

644-
$primary_table = "{$wpdb->prefix}tutor_order_items AS oi";
644+
if ( $cached ) {
645+
return $cached;
646+
}
647+
648+
$primary_table = 'tutor_order_items AS oi';
645649
$joining_tables = array(
646650
array(
647651
'type' => 'LEFT',
648-
'table' => "{$wpdb->prefix}posts AS p",
652+
'table' => 'posts AS p',
649653
'on' => 'p.ID = oi.item_id',
650654
),
651655
);
@@ -657,13 +661,11 @@ public function get_order_items_by_id( $order_id ) {
657661
$courses_data = QueryHelper::get_joined_data( $primary_table, $joining_tables, $select_columns, $where, array(), 'id', 0, 0 );
658662
$courses = $courses_data['results'];
659663

660-
if ( tutor()->has_pro ) {
661-
$bundle_model = new \TutorPro\CourseBundle\Models\BundleModel();
662-
}
664+
$bundle_model = tutor()->has_pro ? new \TutorPro\CourseBundle\Models\BundleModel() : null;
663665

664666
if ( ! empty( $courses_data['total_count'] ) ) {
665667
foreach ( $courses as &$course ) {
666-
if ( tutor()->has_pro && 'course-bundle' === $course->type ) {
668+
if ( is_object( $bundle_model ) && tutor()->bundle_post_type === $course->type ) {
667669
$course->total_courses = count( $bundle_model->get_bundle_course_ids( $course->id ) );
668670
}
669671

@@ -679,7 +681,10 @@ public function get_order_items_by_id( $order_id ) {
679681

680682
unset( $course );
681683

682-
return ! empty( $courses ) ? $courses : array();
684+
$result = ! empty( $courses ) ? $courses : array();
685+
TutorCache::set( $cache_key, $result );
686+
687+
return $result;
683688
}
684689

685690
/**

0 commit comments

Comments
 (0)