wishlist.php
210 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Wishlist; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | class Wishlist |
| 8 | { |
| 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 | if($this->get_where_to() == 'after') { |
| 21 | |
| 22 | add_action('woocommerce_after_add_to_cart_button', [$this, 'print_wish_button'], 10, 0); |
| 23 | |
| 24 | } else { |
| 25 | |
| 26 | add_action('woocommerce_before_add_to_cart_button', [$this, 'print_wish_button'], 10, 0); |
| 27 | } |
| 28 | |
| 29 | $show_in_archive = apply_filters('shopengine/module/wishlist/show_icon_in_shop_page', true); |
| 30 | |
| 31 | if($show_in_archive === true) { |
| 32 | |
| 33 | add_filter('woocommerce_loop_add_to_cart_link', [$this, 'print_button_in_shop'], 10, 3); |
| 34 | } |
| 35 | |
| 36 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 37 | |
| 38 | |
| 39 | if(is_user_logged_in()) { |
| 40 | |
| 41 | if(!empty($_COOKIE[Wishlist::COOKIE_KEY])) { |
| 42 | |
| 43 | $uid = get_current_user_id(); |
| 44 | |
| 45 | $content = get_user_meta($uid, self::UMK_WISHLIST, true); |
| 46 | $content = empty($content) ? [] : $content; |
| 47 | |
| 48 | $cck = explode(',', $_COOKIE[Wishlist::COOKIE_KEY]); |
| 49 | |
| 50 | foreach($cck as $pid) { |
| 51 | |
| 52 | $content[$pid] = $pid; |
| 53 | } |
| 54 | |
| 55 | update_user_meta($uid, self::UMK_WISHLIST, $content); |
| 56 | |
| 57 | setcookie(Wishlist::COOKIE_KEY, '', time() - 3600); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | add_action('init', function () { |
| 63 | add_rewrite_endpoint('wishlist', EP_ROOT | EP_PAGES); |
| 64 | }); |
| 65 | |
| 66 | add_filter('woocommerce_account_menu_items', [$this, 'add_to_menu'], 40); |
| 67 | add_action('woocommerce_account_wishlist_endpoint', [$this, 'wishlist_content']); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | public function enqueue() { |
| 72 | |
| 73 | wp_enqueue_style('shopengine-wishlist', plugin_dir_url(__FILE__) . 'assets/css/wishlist.css'); |
| 74 | |
| 75 | wp_enqueue_script( |
| 76 | 'shopengine-wishlist', |
| 77 | plugin_dir_url(__FILE__) . '/assets/js/wishlist.js', |
| 78 | ['jquery'] |
| 79 | ); |
| 80 | |
| 81 | wp_localize_script('shopengine-wishlist', 'shopEngineWishlist', [ |
| 82 | 'product_id' => get_the_ID(), |
| 83 | 'resturl' => get_rest_url(), |
| 84 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 85 | ]); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * |
| 91 | * @return mixed - only other exact value we are expecting is after! |
| 92 | */ |
| 93 | public function get_where_to() { |
| 94 | |
| 95 | return apply_filters('shopengine/module/wishlist/put_icon_in_side', 'before'); |
| 96 | //return apply_filters('shopengine/module/wishlist/put_icon_in_side', 'after'); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | private function is_exists_in_wishlist($idd) { |
| 101 | |
| 102 | if(is_user_logged_in()) { |
| 103 | |
| 104 | $content = get_user_meta(get_current_user_id(), self::UMK_WISHLIST, true); |
| 105 | |
| 106 | return isset($content[$idd]); |
| 107 | } |
| 108 | |
| 109 | if(empty($_COOKIE[Wishlist::COOKIE_KEY])) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | $content = explode(',', $_COOKIE[Wishlist::COOKIE_KEY]); |
| 114 | |
| 115 | return in_array($idd, $content); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | public function print_wish_button() { |
| 120 | |
| 121 | $pid = get_the_ID(); |
| 122 | $exist = $this->is_exists_in_wishlist($pid); |
| 123 | $prop = $exist ? 'darkorange' : 'none'; |
| 124 | $cls = $exist ? 'active' : 'inactive'; |
| 125 | |
| 126 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 127 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 128 | |
| 129 | ?> |
| 130 | |
| 131 | <a |
| 132 | class="<?php echo self::ONCLICK_SELECTOR_CLS ?> shopengine-wishlist badge <?php echo esc_attr($cls) ?>" |
| 133 | data-pid="<?php echo $pid ?>" |
| 134 | href="#" |
| 135 | > |
| 136 | <?php echo $left_text ?> |
| 137 | <svg style="width: 30px;" xmlns="http://www.w3.org/2000/svg" fill="<?php echo esc_attr($prop) ?>" |
| 138 | viewBox="0 0 24 24" |
| 139 | stroke="currentColor"> |
| 140 | <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" |
| 141 | d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/> |
| 142 | </svg> |
| 143 | <?php echo $right_text ?> |
| 144 | </a> |
| 145 | |
| 146 | <?php |
| 147 | } |
| 148 | |
| 149 | |
| 150 | function print_button_in_shop($add_to_cart_html, $product, $args = []) { |
| 151 | |
| 152 | $exist = $this->is_exists_in_wishlist($product->get_id()); |
| 153 | $prop = $exist ? 'darkorange' : 'none'; |
| 154 | $cls = $exist ? 'active' : 'inactive'; |
| 155 | |
| 156 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 157 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 158 | |
| 159 | $btn = '<a |
| 160 | data-pid="'.$product->get_id().'" |
| 161 | class="'.self::ONCLICK_SELECTOR_CLS.' shopengine-wishlist badge se-btn '. esc_attr($cls) .'" |
| 162 | href="#" >'. |
| 163 | $left_text . |
| 164 | '<svg style="width: 18px;" |
| 165 | xmlns="http://www.w3.org/2000/svg" |
| 166 | fill="'. esc_attr($prop) .'" |
| 167 | viewBox="0 0 24 24" |
| 168 | stroke="currentColor"> |
| 169 | <path stroke-linecap="round" |
| 170 | stroke-linejoin="round" |
| 171 | stroke-width="2" |
| 172 | d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/> |
| 173 | </svg>'. |
| 174 | $right_text . '</a>'; |
| 175 | |
| 176 | |
| 177 | if($this->get_where_to() == 'after') { |
| 178 | |
| 179 | $before = ''; |
| 180 | $after = $btn; |
| 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 | $logout = $menu_links['customer-logout']; |
| 194 | |
| 195 | unset($menu_links['customer-logout']); |
| 196 | |
| 197 | $menu_links['wishlist'] = 'Wishlist'; |
| 198 | $menu_links['customer-logout'] = $logout; |
| 199 | |
| 200 | return $menu_links; |
| 201 | } |
| 202 | |
| 203 | public function wishlist_content() { |
| 204 | |
| 205 | $list = get_user_meta(get_current_user_id(), Wishlist::UMK_WISHLIST, true); |
| 206 | |
| 207 | include __DIR__ . '/screens/default.php'; |
| 208 | } |
| 209 | } |
| 210 |