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