| 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 |
if (Wishlist_Settings::get('show_on_single', false)) { |
| 42 |
$position = Wishlist_Settings::get('button_position', 'after_add_to_cart'); |
| 43 |
$hook = $position === 'before_add_to_cart' ? 'woocommerce_before_add_to_cart_button' : 'woocommerce_after_add_to_cart_button'; |
| 44 |
add_action($hook, [$this, 'render_single_button']); |
| 45 |
|
| 46 |
$atc_hook = $position === 'before_add_to_cart' ? 'king_addons/woo_product_atc/before_buttons' : 'king_addons/woo_product_atc/after_buttons'; |
| 47 |
add_action($atc_hook, [$this, 'render_single_button']); |
| 48 |
} |
| 49 |
|
| 50 |
if (Wishlist_Settings::get('show_in_archives', false)) { |
| 51 |
add_action('woocommerce_after_shop_loop_item', [$this, 'render_loop_button'], 12); |
| 52 |
add_action('king_addons/woo_products_grid/after_add_to_cart', [$this, 'render_grid_button']); |
| 53 |
} |
| 54 |
|
| 55 |
// Track conversions when order is completed |
| 56 |
add_action('woocommerce_order_status_completed', [$this, 'track_conversion'], 10, 1); |
| 57 |
add_action('woocommerce_order_status_processing', [$this, 'track_conversion'], 10, 1); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Track wishlist to purchase conversions. |
| 62 |
* |
| 63 |
* @param int $order_id Order ID. |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function track_conversion(int $order_id): void |
| 67 |
{ |
| 68 |
$order = wc_get_order($order_id); |
| 69 |
if (!$order instanceof WC_Order) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
// Check if already tracked |
| 74 |
$tracked = $order->get_meta('_ka_wishlist_conversion_tracked'); |
| 75 |
if ($tracked) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$user_id = $order->get_customer_id(); |
| 80 |
if ($user_id <= 0) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
global $wpdb; |
| 85 |
$items_table = Wishlist_DB::get_items_table(); |
| 86 |
$conversions_table = Wishlist_DB::get_conversions_table(); |
| 87 |
|
| 88 |
// Get user's wishlist product IDs |
| 89 |
$wishlist_products = $wpdb->get_col( |
| 90 |
$wpdb->prepare( |
| 91 |
"SELECT DISTINCT product_id FROM {$items_table} WHERE user_id = %d", |
| 92 |
$user_id |
| 93 |
) |
| 94 |
); |
| 95 |
|
| 96 |
if (empty($wishlist_products)) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
// Check each order item against wishlist |
| 101 |
foreach ($order->get_items() as $item) { |
| 102 |
$product_id = $item->get_product_id(); |
| 103 |
$variation_id = $item->get_variation_id(); |
| 104 |
|
| 105 |
if (!in_array($product_id, $wishlist_products, false)) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
// This product was in wishlist - record conversion |
| 110 |
$wpdb->replace( |
| 111 |
$conversions_table, |
| 112 |
[ |
| 113 |
'user_id' => $user_id, |
| 114 |
'product_id' => $product_id, |
| 115 |
'variation_id' => $variation_id ?: 0, |
| 116 |
'order_id' => $order_id, |
| 117 |
'order_item_qty' => $item->get_quantity(), |
| 118 |
'order_item_total' => $item->get_total(), |
| 119 |
'converted_at' => current_time('mysql', true), |
| 120 |
], |
| 121 |
['%d', '%d', '%d', '%d', '%d', '%f', '%s'] |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
// Mark order as tracked |
| 126 |
$order->update_meta_data('_ka_wishlist_conversion_tracked', 1); |
| 127 |
$order->save(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Render wishlist button on single product pages. |
| 132 |
* |
| 133 |
* @param mixed $product Optional product from King Addons ATC widget. |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function render_single_button($passed = null): void |
| 137 |
{ |
| 138 |
if (!Wishlist_Settings::is_enabled() || !Wishlist_Settings::get('show_on_single', false)) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$product = $passed instanceof WC_Product ? $passed : $GLOBALS['product'] ?? null; |
| 143 |
if (!$product instanceof WC_Product) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
echo Wishlist_Renderer::kses( |
| 148 |
$this->renderer->render_button([ |
| 149 |
'product_id' => $product->get_id(), |
| 150 |
'variation_id' => 0, |
| 151 |
'class' => 'king-addons-wishlist-button--single', |
| 152 |
]) |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Render wishlist button inside product loop cards. |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public function render_loop_button(): void |
| 162 |
{ |
| 163 |
if (!Wishlist_Settings::is_enabled() || !Wishlist_Settings::get('show_in_archives', false)) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
global $product; |
| 168 |
if (!$product instanceof WC_Product) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
echo Wishlist_Renderer::kses( |
| 173 |
$this->renderer->render_button([ |
| 174 |
'product_id' => $product->get_id(), |
| 175 |
'class' => 'king-addons-wishlist-button--loop', |
| 176 |
]) |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Render wishlist button on King Addons Products Grid cards. |
| 182 |
* |
| 183 |
* @param mixed $product Product from the grid card. |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function render_grid_button($product): void |
| 187 |
{ |
| 188 |
if (!Wishlist_Settings::is_enabled() || !Wishlist_Settings::get('show_in_archives', false) || !$product instanceof WC_Product) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
echo Wishlist_Renderer::kses( |
| 193 |
$this->renderer->render_button([ |
| 194 |
'product_id' => $product->get_id(), |
| 195 |
'class' => 'king-addons-wishlist-button--loop', |
| 196 |
]) |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|