/var/www/html_uk/wp-content/plugins/automatewoo/includes/Triggers/Abstract_Abandoned_Cart.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

if ( ! 
defined'ABSPATH' ) ) exit;

/**
 * @class Trigger_Abstract_Abandoned_Cart
 */
abstract class Trigger_Abstract_Abandoned_Cart extends Trigger {


    function 
load_admin_details() {
        
$this->description .= ' ' sprintf(
            
/* translators: %1$d number of minutes for cart timeout, %2$s documentation link start, %3$ss documentation link end. */
            
__'Carts are considered abandoned if they are inactive for %1$d minutes. When a customer purchases or empties their abandoned cart all queued workflows will be automatically cleared. %2$sView documentation.%3$s''automatewoo' ),
            
AW()->options()->abandoned_cart_timeout,
            
'<a href="' Admin::get_docs_link'abandoned-cart''trigger-description' ) . '" target="_blank">',
            
'</a>'
        
);
        
$this->group __'Carts''automatewoo' );
    }


    function 
load_fields() {
        
$this->add_field_user_pause_period();
    }


    function 
register_hooks() {
        
add_action'automatewoo/cart/status_changed', [ $this'status_changed' ], 10);
        
add_action'automatewoo/object/delete', [ $this'cart_deleted' ] );
    }


    
/**
     * @param Cart $cart
     * @param string $old_status
     * @param string $new_status
     */
    
function status_changed$cart$old_status$new_status ) {
        if ( 
$new_status == 'abandoned' ) {
            
$this->cart_abandoned$cart );
        }
        elseif ( 
$new_status == 'active' ) {
            
$this->maybe_clear_queued_emails$cart );
        }
    }


    
/**
     * @param Cart $cart
     */
    
function cart_abandoned$cart ) {

        if ( ! 
$cart->has_items() ) {
            return;
        }

        
$this->maybe_run([
            
'customer' => $cart->get_customer(),
            
'cart' => $cart
        
]);
    }


    
/**
     * @param Model|Cart $object
     */
    
function cart_deleted$object ) {
        if ( 
$object->object_type == 'cart' ) {
            
$this->maybe_clear_queued_emails$object );
        }
    }


    
/**
     * @param Cart $cart
     */
    
function maybe_clear_queued_emails$cart ) {
        
$query = new Queue_Query();
        
$query->where_workflow$this->get_workflow_ids() );
        
$query->where_cart$cart->get_id() );

        foreach ( 
$query->get_results() as $event ) {
            
$event->delete();
        }
    }


    
/**
     * @param $workflow Workflow
     * @return bool
     */
    
function validate_workflow$workflow ) {

        
$cart $workflow->data_layer()->get_cart();

        if ( ! 
$cart ) {
            return 
false;
        }

        if ( ! 
$this->validate_field_user_pause_period$workflow ) ) {
            return 
false;
        }

        
// Run once foreach workflow for each stored cart
        // Skip the queue check because the queue should have been cleared when the cart status changed
        
if ( $workflow->has_run_for_data_item'cart'falsetrue ) ) {
            return 
false;
        }

        return 
true;
    }


    
/**
     * @param Workflow $workflow
     * @return bool
     */
    
function validate_before_queued_event$workflow ) {

        if ( ! 
$this->validate_field_user_pause_period$workflow ) ) {
            return 
false;
        }

        return 
true;
    }


}