wishlist.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Wishlist; |
| 4 | |
| 5 | use ShopEngine\Core\Register\Module_List; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | |
| 8 | class Wishlist { |
| 9 | |
| 10 | const COOKIE_KEY = 'shopengine_wishlist_offline'; |
| 11 | const UMK_WISHLIST = 'shopengine_wishlist'; |
| 12 | const ONCLICK_SELECTOR_CLS = 'shopengine_add_to_list_action'; |
| 13 | |
| 14 | use Singleton; |
| 15 | |
| 16 | public function init() { |
| 17 | |
| 18 | new Route(); |
| 19 | |
| 20 | $sett = Module_List::instance()->get_settings('wishlist'); |
| 21 | $is_show_in_single = isset($sett['show_on_single_page']['value']) ? ($sett['show_on_single_page']['value'] === 'yes') : true; |
| 22 | |
| 23 | if($is_show_in_single === true) { |
| 24 | |
| 25 | if($this->get_where_to($sett) === 'after') { |
| 26 | |
| 27 | add_action('woocommerce_after_add_to_cart_button', [$this, 'print_wish_button'], 10, 0); |
| 28 | |
| 29 | } else { |
| 30 | |
| 31 | add_action('woocommerce_before_add_to_cart_button', [$this, 'print_wish_button'], 10, 0); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | $is_show = isset($sett['show_on_archive_page']['value']) ? ($sett['show_on_archive_page']['value'] === 'yes') : true; |
| 37 | |
| 38 | $show_in_archive = apply_filters('shopengine/module/wishlist/show_icon_in_shop_page', $is_show); |
| 39 | |
| 40 | if($show_in_archive === true) { |
| 41 | |
| 42 | add_filter('woocommerce_loop_add_to_cart_link', [$this, 'print_button_in_shop'], 10, 3); |
| 43 | } |
| 44 | |
| 45 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 46 | |
| 47 | |
| 48 | if(is_user_logged_in()) { |
| 49 | |
| 50 | if(!empty($_COOKIE[Wishlist::COOKIE_KEY])) { |
| 51 | |
| 52 | $uid = get_current_user_id(); |
| 53 | |
| 54 | $content = get_user_meta($uid, self::UMK_WISHLIST, true); |
| 55 | $content = empty($content) ? [] : $content; |
| 56 | |
| 57 | $cck = explode(',', $_COOKIE[Wishlist::COOKIE_KEY]); |
| 58 | |
| 59 | foreach($cck as $pid) { |
| 60 | |
| 61 | $content[$pid] = $pid; |
| 62 | } |
| 63 | |
| 64 | update_user_meta($uid, self::UMK_WISHLIST, $content); |
| 65 | |
| 66 | setcookie(Wishlist::COOKIE_KEY, '', time() - 3600); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | add_action('init', function () { |
| 72 | add_rewrite_endpoint('wishlist', EP_ROOT | EP_PAGES); |
| 73 | }); |
| 74 | |
| 75 | add_filter('woocommerce_account_menu_items', [$this, 'add_to_menu'], 40); |
| 76 | add_action('woocommerce_account_wishlist_endpoint', [$this, 'wishlist_content']); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | public function enqueue() { |
| 81 | |
| 82 | wp_enqueue_style('shopengine-wishlist', plugin_dir_url(__FILE__) . 'assets/css/wishlist.css'); |
| 83 | |
| 84 | wp_enqueue_script( |
| 85 | 'shopengine-wishlist', |
| 86 | plugin_dir_url(__FILE__) . '/assets/js/wishlist.js', |
| 87 | ['jquery'] |
| 88 | ); |
| 89 | |
| 90 | wp_localize_script('shopengine-wishlist', 'shopEngineWishlist', [ |
| 91 | 'product_id' => get_the_ID(), |
| 92 | 'resturl' => get_rest_url(), |
| 93 | 'isLoggedIn' => is_user_logged_in(), |
| 94 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 95 | ]); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * |
| 101 | * @return mixed - only other exact value we are expecting is after! |
| 102 | */ |
| 103 | public function get_where_to($settings = []) { |
| 104 | |
| 105 | $position = !empty($settings['show_icon_where_to']['value']) ? $settings['show_icon_where_to']['value'] : 'before'; |
| 106 | |
| 107 | return apply_filters('shopengine/module/wishlist/put_icon_in_side', $position); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | private function is_exists_in_wishlist($idd) { |
| 112 | |
| 113 | if(is_user_logged_in()) { |
| 114 | |
| 115 | $content = get_user_meta(get_current_user_id(), self::UMK_WISHLIST, true); |
| 116 | |
| 117 | return isset($content[$idd]); |
| 118 | } |
| 119 | |
| 120 | if(empty($_COOKIE[Wishlist::COOKIE_KEY])) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | $content = explode(',', $_COOKIE[Wishlist::COOKIE_KEY]); |
| 125 | |
| 126 | return in_array($idd, $content); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | public function print_wish_button() { |
| 131 | |
| 132 | $pid = get_the_ID(); |
| 133 | $exist = $this->is_exists_in_wishlist($pid); |
| 134 | $prop = $exist ? 'darkorange' : 'none'; |
| 135 | $cls = $exist ? 'active' : 'inactive'; |
| 136 | |
| 137 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 138 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 139 | |
| 140 | ?> |
| 141 | |
| 142 | <a |
| 143 | class="<?php echo self::ONCLICK_SELECTOR_CLS ?> shopengine-wishlist badge <?php echo esc_attr($cls) ?>" |
| 144 | data-pid="<?php echo $pid ?>" |
| 145 | href="#" |
| 146 | > |
| 147 | <?php echo $left_text ?> |
| 148 | <i class="shopengine-icon-add_to_favourite_1"></i> |
| 149 | <?php echo $right_text ?> |
| 150 | </a> |
| 151 | |
| 152 | <?php |
| 153 | } |
| 154 | |
| 155 | |
| 156 | function print_button_in_shop($add_to_cart_html, $product, $args = []) { |
| 157 | |
| 158 | $sett = Module_List::instance()->get_settings('wishlist'); |
| 159 | |
| 160 | $exist = $this->is_exists_in_wishlist($product->get_id()); |
| 161 | $prop = $exist ? 'darkorange' : 'none'; |
| 162 | $cls = $exist ? 'active' : 'inactive'; |
| 163 | |
| 164 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 165 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 166 | |
| 167 | $btn = '<a |
| 168 | data-pid="' . $product->get_id() . '" |
| 169 | class="' . self::ONCLICK_SELECTOR_CLS . ' shopengine-wishlist badge se-btn ' . esc_attr($cls) . '" |
| 170 | href="#" >' . |
| 171 | $left_text . |
| 172 | '<i class="shopengine-icon-add_to_favourite_1"></i>' . |
| 173 | $right_text . '</a>'; |
| 174 | |
| 175 | |
| 176 | if($this->get_where_to($sett) == 'after') { |
| 177 | |
| 178 | $before = ''; |
| 179 | $after = $btn; |
| 180 | |
| 181 | } else { |
| 182 | |
| 183 | $before = $btn; |
| 184 | $after = ''; |
| 185 | } |
| 186 | |
| 187 | return $before . $add_to_cart_html . $after; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | public function add_to_menu($menu_links) { |
| 192 | |
| 193 | $menu_links['wishlist'] = 'Wishlist'; |
| 194 | |
| 195 | if(isset($menu_links['customer-logout'])) { |
| 196 | |
| 197 | $logout = $menu_links['customer-logout']; |
| 198 | unset($menu_links['customer-logout']); |
| 199 | $menu_links['customer-logout'] = $logout; |
| 200 | } else { |
| 201 | $menu_links['customer-logout'] = esc_html__('Logout', 'shopengine'); |
| 202 | } |
| 203 | |
| 204 | return $menu_links; |
| 205 | } |
| 206 | |
| 207 | public function wishlist_content() { |
| 208 | |
| 209 | $list = get_user_meta(get_current_user_id(), Wishlist::UMK_WISHLIST, true); |
| 210 | |
| 211 | include __DIR__ . '/screens/default.php'; |
| 212 | } |
| 213 | } |
| 214 |