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