cart_has_items() ) { return; } $popups = $this->get_cart_abandonment_popups(); if ( empty( $popups ) ) { return; } // Ensure frontend scripts/styles are loaded (FOXCP_Frontend may not // have loaded them if there are no regular popups on this page). wp_enqueue_style( 'wp-block-library' ); wp_enqueue_style( 'foxcp-frontend', FOXCP_PLUGIN_URL . 'assets/css/popup-frontend.css', [ 'wp-block-library' ], FOXCP_VERSION ); wp_enqueue_script( 'foxcp-frontend', FOXCP_PLUGIN_URL . 'assets/js/popup-frontend.js', [], FOXCP_VERSION, true ); // Build popup data with exit intent forced. $popup_data = []; foreach ( $popups as $popup ) { $popup_data[] = [ 'id' => $popup->ID, 'delay' => 0, 'scroll' => 0, 'exitIntent' => true, // Always use exit intent for cart abandonment. 'preview' => false, 'cookieAlways' => (bool) get_post_meta( $popup->ID, '_foxcp_cookie_always', true ), 'cookieDuration' => (int) ( get_post_meta( $popup->ID, '_foxcp_cookie_duration', true ) ?: self::COOKIE_DAYS_FALLBACK ), 'cookieUnit' => get_post_meta( $popup->ID, '_foxcp_cookie_unit', true ) ?: 'days', 'animation' => get_post_meta( $popup->ID, '_foxcp_animation', true ) ?: 'none', 'imageMode' => (bool) get_post_meta( $popup->ID, '_foxcp_image_mode', true ), 'linkUrl' => get_post_meta( $popup->ID, '_foxcp_link_url', true ) ?: '', ]; } // Merge with any existing foxcpData (from FOXCP_Frontend). // wp_localize_script only works once per handle, so we use an // inline script to extend the data. wp_add_inline_script( 'foxcp-frontend', 'if (typeof foxcpData !== "undefined" && foxcpData.popups) {' . 'foxcpData.popups = foxcpData.popups.concat(' . wp_json_encode( $popup_data ) . ');' . '} else {' . 'var foxcpData = { popups: ' . wp_json_encode( $popup_data ) . ' };' . '}', 'before' ); } // ------------------------------------------------------------------------- // HTML output // ------------------------------------------------------------------------- public function render_popups() { if ( ! $this->cart_has_items() ) { return; } $popups = $this->get_cart_abandonment_popups(); if ( empty( $popups ) ) { return; } global $post; $original_post = $post; foreach ( $popups as $popup ) { $popup_id = (int) $popup->ID; $image_mode = (bool) get_post_meta( $popup_id, '_foxcp_image_mode', true ); $link_url = get_post_meta( $popup_id, '_foxcp_link_url', true ); $coupon_code = get_post_meta( $popup_id, '_foxcp_woo_coupon_code', true ); $popup_classes = 'foxcp-popup'; if ( $image_mode ) { $popup_classes .= ' foxcp-image-mode'; } $post = $popup; // phpcs:ignore WordPress.WP.GlobalVariablesOverride setup_postdata( $popup ); ?>
cart ) { return false; } return WC()->cart->get_cart_contents_count() > 0; } /** * Get all published popups marked as cart-abandonment popups. * * @return WP_Post[] */ private function get_cart_abandonment_popups() { $all_popups = get_posts( [ 'post_type' => 'foxcp_popup', 'post_status' => 'publish', 'posts_per_page' => -1, ] ); if ( empty( $all_popups ) ) { return []; } return array_filter( $all_popups, static function ( $popup ) { return (bool) get_post_meta( $popup->ID, '_foxcp_woo_cart_abandonment', true ); } ); } }