Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
WonderCart | |
0.00% |
0 / 75 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
register_hooks | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
register_campaign | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
create_campaign_modal_open | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
campaign_selected | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
campaign_abandoned | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
checkout_campaigns_used | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
4 | |
5 | /** |
6 | * Monitors WonderCart events |
7 | */ |
8 | class WonderCart extends Listener { |
9 | |
10 | /** |
11 | * Register the hooks for the listener |
12 | * |
13 | * @return void |
14 | */ |
15 | public function register_hooks() { |
16 | add_action( 'rest_after_insert_yith_campaign', array( $this, 'register_campaign' ), 10 ); |
17 | add_action( 'yith_sales_edit_campaign_event_modal_opened', array( $this, 'create_campaign_modal_open' ), 10, 2 ); |
18 | add_action( 'yith_sales_edit_campaign_event_campaign_selected', array( $this, 'campaign_selected' ), 10, 2 ); |
19 | add_action( 'yith_sales_edit_campaign_event_campaign_abandoned', array( $this, 'campaign_abandoned' ), 10, 2 ); |
20 | add_action( 'woocommerce_payment_complete', array( $this, 'checkout_campaigns_used' ) ); |
21 | } |
22 | |
23 | /** |
24 | * Campaign created |
25 | * |
26 | * @param string $post Campaign data |
27 | * |
28 | * @return string The post value |
29 | */ |
30 | public function register_campaign( $post ) { |
31 | $campaign = yith_sales_get_campaign( $post->ID ); |
32 | if ( $campaign ) { |
33 | $type = $campaign->get_type(); |
34 | |
35 | $data = array( |
36 | 'label_key' => 'type', |
37 | 'type' => $type, |
38 | ); |
39 | |
40 | $this->push( |
41 | 'campaign_created', |
42 | $data |
43 | ); |
44 | } |
45 | |
46 | return $post; |
47 | } |
48 | |
49 | /** |
50 | * Track wonder_cart create campaign modal window open |
51 | * Send data to hiive |
52 | |
53 | * @param string $args A list of details that were involved on the event. |
54 | * @param string $event The name of the event. |
55 | |
56 | * @return void |
57 | */ |
58 | public function create_campaign_modal_open( $args, $event ) { |
59 | $url = is_ssl() ? 'https://' : 'http://'; |
60 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
61 | |
62 | $data = array( |
63 | 'label_key' => 'trigger', |
64 | 'trigger' => 'Campaign Modal Open', |
65 | 'page' => $url, |
66 | ); |
67 | |
68 | $this->push( |
69 | 'modal_open', |
70 | $data |
71 | ); |
72 | } |
73 | |
74 | /** |
75 | * Track wonder_cart campaign selection |
76 | * Send data to hiive |
77 | |
78 | * @param string $args A list of details that were involved on the event. |
79 | * @param string $event The name of the event. |
80 | |
81 | * @return void |
82 | */ |
83 | public function campaign_selected( $args, $event ) { |
84 | $data = array( |
85 | 'label_key' => 'campaign_slug', |
86 | 'type' => $args['type'], |
87 | 'campaign_slug' => $args['type'], |
88 | ); |
89 | |
90 | $this->push( |
91 | 'campaign_selected', |
92 | $data |
93 | ); |
94 | } |
95 | |
96 | /** |
97 | * Track wonder_cart campaign abondoned |
98 | * Send data to hiive |
99 | |
100 | * @param string $args A list of details that were involved on the event. |
101 | * @param string $event The name of the event. |
102 | |
103 | * @return void |
104 | */ |
105 | public function campaign_abandoned( $args, $event ) { |
106 | $data = array( |
107 | 'label_key' => 'campaign_slug', |
108 | 'type' => $args['type'], |
109 | 'campaign_slug' => $args['type'] . '-' . $args['id'], |
110 | ); |
111 | |
112 | $this->push( |
113 | 'campaign_abondoned', |
114 | $data |
115 | ); |
116 | } |
117 | |
118 | /** |
119 | * Track wonder_cart campaigns used in checkout page |
120 | * Send data to hiive |
121 | |
122 | * @return void |
123 | */ |
124 | public function checkout_campaigns_used() { |
125 | $campaigns = array(); |
126 | $campaign_total = 0; |
127 | |
128 | $cart = WC()->cart; |
129 | |
130 | // To track Cart Discount |
131 | foreach ( $cart->get_applied_coupons() as $coupon_item ) { |
132 | array_push( $campaigns, $coupon_item ); |
133 | $campaign_total += $cart->coupon_discount_totals[ $coupon_item ]; |
134 | } |
135 | |
136 | // To track free shipping campaign ( Using reflection to access protected properties) |
137 | $reflection_class = new \ReflectionClass( $cart ); |
138 | $shipping_methods_property = $reflection_class->getProperty( 'shipping_methods' ); |
139 | $shipping_methods_property->setAccessible( true ); |
140 | $shipping_methods = $shipping_methods_property->getValue( $cart ); |
141 | foreach ( $shipping_methods as $shipping_method ) { |
142 | if ( 'yith_sales_free_shipping' === $shipping_method->id ) { |
143 | array_push( $campaigns, 'yith_sales_free_shipping' ); |
144 | } |
145 | } |
146 | |
147 | // To track rest of the campaigns |
148 | foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { |
149 | if ( isset( $cart_item['yith_sales'] ) && isset( $cart_item['yith_sales']['campaigns'] ) ) { |
150 | $campaign_type = $cart_item['yith_sales_discounts']['type']; |
151 | array_push( $campaigns, $campaign_type ); |
152 | $campaign_total += $cart_item['yith_sales_discounts']['price_base'] - $cart_item['yith_sales_discounts']['price_adjusted']; |
153 | } |
154 | } |
155 | if ( count( $campaigns ) > 0 ) { |
156 | $data = array( |
157 | 'label_key' => 'type', |
158 | 'type' => array_unique( $campaigns ), |
159 | 'campaign_count' => count( $campaigns ), |
160 | 'campaign_total' => '$' . $campaign_total, |
161 | ); |
162 | $this->push( |
163 | 'checkout_campaign_type', |
164 | $data |
165 | ); |
166 | } |
167 | } |
168 | } |