PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.63
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.63
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / wishlist / Wishlist_WooCommerce.php

Wishlist_WooCommerce.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.63, at includes/wishlist/Wishlist_WooCommerce.php

176 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons\Wishlist;
4
5 use WC_Product;
6 use WC_Order;
7
8 if (!defined('ABSPATH')) {
9 exit;
10 }
11
12 /**
13 * Integrates wishlist UI into WooCommerce templates and tracks conversions.
14 */
15 class Wishlist_WooCommerce
16 {
17 private Wishlist_Service $service;
18 private Wishlist_Renderer $renderer;
19
20 /**
21 * @param Wishlist_Service $service Wishlist service.
22 * @param Wishlist_Renderer $renderer Wishlist renderer.
23 */
24 public function __construct(Wishlist_Service $service, Wishlist_Renderer $renderer)
25 {
26 $this->service = $service;
27 $this->renderer = $renderer;
28 }
29
30 /**
31 * Register WooCommerce hooks.
32 *
33 * @return void
34 */
35 public function hooks(): void
36 {
37 if (!Wishlist_Settings::is_enabled() || !function_exists('wc_get_product')) {
38 return;
39 }
40
41 $position = Wishlist_Settings::get('button_position', 'after_add_to_cart');
42 $hook = $position === 'before_add_to_cart' ? 'woocommerce_before_add_to_cart_button' : 'woocommerce_after_add_to_cart_button';
43 add_action($hook, [$this, 'render_single_button']);
44
45 if (Wishlist_Settings::get('show_in_archives', true)) {
46 add_action('woocommerce_after_shop_loop_item', [$this, 'render_loop_button'], 12);
47 }
48
49 // Track conversions when order is completed
50 add_action('woocommerce_order_status_completed', [$this, 'track_conversion'], 10, 1);
51 add_action('woocommerce_order_status_processing', [$this, 'track_conversion'], 10, 1);
52 }
53
54 /**
55 * Track wishlist to purchase conversions.
56 *
57 * @param int $order_id Order ID.
58 * @return void
59 */
60 public function track_conversion(int $order_id): void
61 {
62 $order = wc_get_order($order_id);
63 if (!$order instanceof WC_Order) {
64 return;
65 }
66
67 // Check if already tracked
68 $tracked = $order->get_meta('_ka_wishlist_conversion_tracked');
69 if ($tracked) {
70 return;
71 }
72
73 $user_id = $order->get_customer_id();
74 if ($user_id <= 0) {
75 return;
76 }
77
78 global $wpdb;
79 $items_table = Wishlist_DB::get_items_table();
80 $conversions_table = Wishlist_DB::get_conversions_table();
81
82 // Get user's wishlist product IDs
83 $wishlist_products = $wpdb->get_col(
84 $wpdb->prepare(
85 "SELECT DISTINCT product_id FROM {$items_table} WHERE user_id = %d",
86 $user_id
87 )
88 );
89
90 if (empty($wishlist_products)) {
91 return;
92 }
93
94 // Check each order item against wishlist
95 foreach ($order->get_items() as $item) {
96 $product_id = $item->get_product_id();
97 $variation_id = $item->get_variation_id();
98
99 if (!in_array($product_id, $wishlist_products, false)) {
100 continue;
101 }
102
103 // This product was in wishlist - record conversion
104 $wpdb->replace(
105 $conversions_table,
106 [
107 'user_id' => $user_id,
108 'product_id' => $product_id,
109 'variation_id' => $variation_id ?: 0,
110 'order_id' => $order_id,
111 'order_item_qty' => $item->get_quantity(),
112 'order_item_total' => $item->get_total(),
113 'converted_at' => current_time('mysql', true),
114 ],
115 ['%d', '%d', '%d', '%d', '%d', '%f', '%s']
116 );
117 }
118
119 // Mark order as tracked
120 $order->update_meta_data('_ka_wishlist_conversion_tracked', 1);
121 $order->save();
122 }
123
124 /**
125 * Render wishlist button on single product pages.
126 *
127 * @return void
128 */
129 public function render_single_button(): void
130 {
131 if (!Wishlist_Settings::is_enabled()) {
132 return;
133 }
134
135 global $product;
136 if (!$product instanceof WC_Product) {
137 return;
138 }
139
140 echo wp_kses_post(
141 $this->renderer->render_button([
142 'product_id' => $product->get_id(),
143 'variation_id' => 0,
144 'class' => 'king-addons-wishlist-button--single',
145 ])
146 );
147 }
148
149 /**
150 * Render wishlist button inside product loop cards.
151 *
152 * @return void
153 */
154 public function render_loop_button(): void
155 {
156 if (!Wishlist_Settings::is_enabled()) {
157 return;
158 }
159
160 global $product;
161 if (!$product instanceof WC_Product) {
162 return;
163 }
164
165 echo wp_kses_post(
166 $this->renderer->render_button([
167 'product_id' => $product->get_id(),
168 'class' => 'king-addons-wishlist-button--loop',
169 ])
170 );
171 }
172 }
173
174
175
176