Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SalesPromotion | |
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 SalesPromotion 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_promotion/edit_campaign_event_modal_opened', array( |
| 21 | $this, |
| 22 | 'create_campaign_modal_open', |
| 23 | ), 10, 2 ); |
| 24 | add_action( 'bluehost/sales_promotion/edit_campaign_event_campaign_selected', array( |
| 25 | $this, |
| 26 | 'campaign_selected', |
| 27 | ), 10, 2 ); |
| 28 | add_action( 'bluehost/sales_promotion/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_promotion_get_campaign' ) ? bh_sales_promotion_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 | * Send data to hiive |
| 64 | * |
| 65 | * @param string $args A list of details that were involved on the event. |
| 66 | * @param string $event The name of the event. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function create_campaign_modal_open( $args, $event ) { |
| 71 | $url = is_ssl() ? 'https://' : 'http://'; |
| 72 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 73 | |
| 74 | $data = array( |
| 75 | 'label_key' => 'trigger', |
| 76 | 'trigger' => 'Campaign Modal Open', |
| 77 | 'page' => $url, |
| 78 | ); |
| 79 | |
| 80 | $this->push( |
| 81 | 'modal_open', |
| 82 | $data |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Track sales_promotion campaign selection |
| 88 | * Send data to hiive |
| 89 | * |
| 90 | * @param string $args A list of details that were involved on the event. |
| 91 | * @param string $event The name of the event. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function campaign_selected( $args, $event ) { |
| 96 | $data = array( |
| 97 | 'label_key' => 'campaign_slug', |
| 98 | 'type' => $args['type'], |
| 99 | 'campaign_slug' => $args['type'], |
| 100 | ); |
| 101 | |
| 102 | $this->push( |
| 103 | 'campaign_selected', |
| 104 | $data |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Track sales_promotion campaign abondoned |
| 110 | * Send data to hiive |
| 111 | * |
| 112 | * @param string $args A list of details that were involved on the event. |
| 113 | * @param string $event The name of the event. |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | public function campaign_abandoned( $args, $event ) { |
| 118 | $data = array( |
| 119 | 'label_key' => 'campaign_slug', |
| 120 | 'type' => $args['type'], |
| 121 | 'campaign_slug' => $args['type'] . '-' . $args['id'], |
| 122 | ); |
| 123 | |
| 124 | $this->push( |
| 125 | 'campaign_abandoned', |
| 126 | $data |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Track sales_promotion campaigns used in checkout page |
| 132 | * Send data to hiive |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | public function checkout_campaigns_used() { |
| 137 | $campaigns = array(); |
| 138 | $campaign_total = 0; |
| 139 | |
| 140 | $cart = WC()->cart; |
| 141 | |
| 142 | if ( $cart instanceof WC_Cart ) { |
| 143 | // To track Cart Discount |
| 144 | foreach ( $cart->get_applied_coupons() as $coupon_item ) { |
| 145 | if( false !== strpos ( $coupon_item, 'bh_sales_promotion_cart_discount') ) { |
| 146 | array_push( $campaigns, 'cart-discount' ); |
| 147 | $campaign_total += $cart->coupon_discount_totals[ $coupon_item ]; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // To track free shipping campaign ( Using reflection to access protected properties) |
| 152 | $reflection_class = new \ReflectionClass( $cart ); |
| 153 | $shipping_methods_property = $reflection_class->getProperty( 'shipping_methods' ); |
| 154 | $shipping_methods_property->setAccessible( true ); |
| 155 | $shipping_methods = $shipping_methods_property->getValue( $cart ); |
| 156 | foreach ( $shipping_methods as $shipping_method ) { |
| 157 | if ( 'bh_sales_promotion_free_shipping' === $shipping_method->id ) { |
| 158 | array_push( $campaigns, 'free-shipping' ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // To track rest of the campaigns |
| 163 | foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { |
| 164 | if ( isset( $cart_item['bh_sales_promotion'] ) && isset( $cart_item['bh_sales_promotion']['campaigns'] ) ) { |
| 165 | $campaign_type = $cart_item['bh_sales_promotion_discounts']['type']; |
| 166 | array_push( $campaigns, $campaign_type ); |
| 167 | $campaign_total += $cart_item['bh_sales_promotion_discounts']['price_base'] - $cart_item['bh_sales_promotion_discounts']['price_adjusted']; |
| 168 | } |
| 169 | } |
| 170 | if ( count( $campaigns ) > 0 ) { |
| 171 | $data = array( |
| 172 | 'label_key' => 'type', |
| 173 | 'type' => array_unique( $campaigns ), |
| 174 | 'campaign_count' => count( $campaigns ), |
| 175 | 'campaign_total' => '$' . $campaign_total, |
| 176 | ); |
| 177 | |
| 178 | |
| 179 | $this->push( |
| 180 | 'checkout_campaign_type', |
| 181 | $data |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |