PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.1.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.1.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / models / cart.php

cart.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.1.0, at includes/models/cart.php

74 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Models;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class Cart {
10
11 public static function get_carts_by_hash_or_user_id( $hash, $user_id ): array {
12 global $wpdb;
13
14 if ( ! $hash && ! $user_id ) {
15 return [];
16 }
17
18 if ( 0 === $user_id ) {
19 return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_cart WHERE cart_hash = %s ORDER BY `updated_at` DESC;", $hash ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
20 }
21
22 return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_cart WHERE cart_hash = %s OR user_id=%d ORDER BY `updated_at` DESC;", $hash, $user_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
23 }
24
25 public static function get_cart_hash_by_user_id( $user_id ) {
26 global $wpdb;
27 $result = $wpdb->get_var( $wpdb->prepare( "SELECT cart_hash FROM {$wpdb->prefix}storeengine_cart WHERE user_id = %d;", $user_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
28
29 if ( $result ) {
30 return $result;
31 }
32
33 return null;
34 }
35
36 public static function update( int $id, string $cart_hash, array $cart_data ): bool {
37 global $wpdb;
38
39 $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
40 $wpdb->prefix . 'storeengine_cart',
41 [
42 'user_id' => get_current_user_id(),
43 'cart_hash' => $cart_hash,
44 'cart_data' => maybe_serialize( $cart_data ),
45 ],
46 [ 'cart_id' => $id ]
47 );
48
49 return true;
50 }
51
52 public static function delete( int $id ): bool {
53 global $wpdb;
54
55 $wpdb->delete( $wpdb->prefix . 'storeengine_cart', [ 'cart_id' => $id ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
56
57 return true;
58 }
59
60 public static function create( int $user_id, string $cart_hash, array $cart_data ) {
61 global $wpdb;
62 $wpdb->insert( $wpdb->prefix . 'storeengine_cart', // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
63 [
64 'user_id' => $user_id,
65 'cart_hash' => $cart_hash,
66 'cart_data' => maybe_serialize( $cart_data ),
67 ],
68 [ '%d', '%s', '%s' ]
69 );
70
71 return $wpdb->insert_id;
72 }
73 }
74