| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sticky Abandoned Cart Recovery DB Module Class. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Modules\AbandonedCartRecovery; |
| 9 |
|
| 10 |
use RadiusTheme\SB\Helpers\Fns; |
| 11 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit(); |
| 14 |
|
| 15 |
/** |
| 16 |
* Sticky add-to-cart Module Class. |
| 17 |
*/ |
| 18 |
class CartRecoveryTable { |
| 19 |
/** |
| 20 |
* Singleton Trait. |
| 21 |
*/ |
| 22 |
use SingletonTrait; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->create_cart_table_if_need(); |
| 29 |
} |
| 30 |
/** |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function create_cart_table_if_need() { |
| 34 |
global $wpdb; |
| 35 |
$isMissing = false; |
| 36 |
$required_tables = [ |
| 37 |
CartRecoveryFns::$ca_abandonment, |
| 38 |
CartRecoveryFns::$ca_abandonment_meta, |
| 39 |
CartRecoveryFns::$ca_email, |
| 40 |
CartRecoveryFns::$ca_email_meta, |
| 41 |
CartRecoveryFns::$ca_email_history, |
| 42 |
]; |
| 43 |
foreach ( $required_tables as $table ) { |
| 44 |
$is_table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . $table ) ); //phpcs:ignore |
| 45 |
if ( empty( $is_table_exist ) ) { |
| 46 |
$isMissing = true; |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
if ( $isMissing ) { |
| 51 |
$this->create_cart_abandonment_table(); |
| 52 |
$this->create_cart_abandonment_email_template_table(); |
| 53 |
$this->create_email_templates_meta_table(); |
| 54 |
$this->create_email_history_table(); |
| 55 |
$this->create_abandonment_meta_table(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
private function create_cart_abandonment_table() { |
| 63 |
Fns::DB()::create( CartRecoveryFns::$ca_abandonment ) |
| 64 |
->column( 'id' )->bigInt( 20 )->unsigned()->autoIncrement()->primary()->required() |
| 65 |
->column( 'checkout_id' )->int( 11 )->required() |
| 66 |
->column( 'email' )->string( 100 )->nullable() |
| 67 |
->column( 'cart_contents' )->longText() |
| 68 |
->column( 'cart_total' )->string( 10 )->nullable() |
| 69 |
->column( 'ca_session_id' )->string( 60 )->required() |
| 70 |
->column( 'other_fields' )->longText() |
| 71 |
->column( 'order_status' )->enum( [ 'normal', 'abandoned', 'completed', 'lost' ] )->default( 'normal' ) |
| 72 |
->column( 'unsubscribed' )->boolean()->default( 0 ) |
| 73 |
->column( 'coupon_code' )->string( 50 )->nullable() |
| 74 |
->column( 'time' )->dateTime() |
| 75 |
->execute(); |
| 76 |
} |
| 77 |
/** |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
private function create_abandonment_meta_table() { |
| 81 |
Fns::DB()::create( CartRecoveryFns::$ca_abandonment_meta ) |
| 82 |
->column( 'id' )->bigInt( 20 )->unsigned()->autoIncrement()->primary()->required() |
| 83 |
->column( 'abandonment_id' )->bigInt( 20 )->unsigned()->required() |
| 84 |
->column( 'meta_key' )->string( 255 )->required() |
| 85 |
->column( 'meta_value' )->longText()->required() |
| 86 |
->execute(); |
| 87 |
} |
| 88 |
/** |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
private function create_cart_abandonment_email_template_table() { |
| 92 |
Fns::DB()::create( CartRecoveryFns::$ca_email ) |
| 93 |
->column( 'id' )->bigInt( 20 )->unsigned()->autoIncrement()->primary()->required() |
| 94 |
->column( 'title' )->text()->required() |
| 95 |
->column( 'email_subject' )->text()->required() |
| 96 |
->column( 'email_body' )->longText()->required() |
| 97 |
->column( 'other_fields' )->longText() |
| 98 |
->column( 'is_activated' )->text() |
| 99 |
->column( 'frequency' )->int( 11 )->required() |
| 100 |
->column( 'frequency_unit' )->enum( [ 'MINUTE','HOUR','DAY' ] )->default( 'MINUTE' )->required() |
| 101 |
->column( 'menu_order' )->int( 11 )->default( 0 ) |
| 102 |
->execute(); |
| 103 |
} |
| 104 |
/** |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
private function create_email_history_table() { |
| 108 |
Fns::DB()::create( CartRecoveryFns::$ca_email_history ) |
| 109 |
->column( 'id' )->bigInt( 20 )->unsigned()->autoIncrement()->primary()->required() |
| 110 |
->column( 'template_id' )->bigInt( 20 )->unsigned()->required() |
| 111 |
->column( 'abandonment_id' )->bigInt( 20 )->required() |
| 112 |
->column( 'coupon_code' )->string( 50 )->nullable() |
| 113 |
->column( 'scheduled_time' )->dateTime() |
| 114 |
->column( 'email_sent' )->boolean()->default( 0 ) |
| 115 |
->column( 'open_time' )->dateTime() |
| 116 |
->column( 'action' )->string( 50 )->nullable() |
| 117 |
->execute(); |
| 118 |
} |
| 119 |
/** |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
private function create_email_templates_meta_table() { |
| 123 |
Fns::DB()::create( CartRecoveryFns::$ca_email_meta ) |
| 124 |
->column( 'id' )->bigInt( 20 )->unsigned()->autoIncrement()->primary()->required() |
| 125 |
->column( 'email_template_id' )->bigInt( 20 )->unsigned()->required() |
| 126 |
->column( 'meta_key' )->string( 255 )->required() |
| 127 |
->column( 'meta_value' )->longText()->required() |
| 128 |
->execute(); |
| 129 |
} |
| 130 |
} |
| 131 |
|