| @@ -1,456 +1,466 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | - | |
| 5 | -use RadiusTheme\SB\Helpers\Fns; | |
| 6 | -use RadiusTheme\SB\Traits\SingletonTrait; | |
| 7 | -use WC_Product; | |
| 8 | - | |
| 9 | -// Do not allow directly accessing this file. | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit( 'This script cannot be accessed directly.' ); | |
| 12 | -} | |
| 13 | - | |
| 14 | -class WishlistFns { | |
| 15 | - | |
| 16 | - use SingletonTrait; | |
| 17 | - | |
| 18 | - const WC_MENU_SLUG = 'wishlist'; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Remove product id | |
| 22 | - * | |
| 23 | - * @param int $product_id | |
| 24 | - * | |
| 25 | - * @return bool|int | |
| 26 | - */ | |
| 27 | - public function remove_product( $product_id ) { | |
| 28 | - | |
| 29 | - if ( ! $this->is_exists_in_wishlist( $product_id ) ) { | |
| 30 | - return 0; | |
| 31 | - } | |
| 32 | - $pIds = $this->get_wishlist_ids(); | |
| 33 | - $pIds = array_diff( $pIds, [ $product_id ] ); | |
| 34 | - if ( is_user_logged_in() ) { | |
| 35 | - $this->update_user_wishlist_ids( $pIds ); | |
| 36 | - } else { | |
| 37 | - $cookie_name = $this->get_cookie_name(); | |
| 38 | - if ( empty( $pIds ) ) { | |
| 39 | - setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 40 | - $_COOKIE[ $cookie_name ] = false; | |
| 41 | - } else { | |
| 42 | - setcookie( $cookie_name, wp_json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 43 | - $_COOKIE[ $cookie_name ] = wp_json_encode( $pIds ); | |
| 44 | - } | |
| 45 | - } | |
| 46 | - | |
| 47 | - return true; | |
| 48 | - } | |
| 49 | - | |
| 50 | - public static function get_page_url() { | |
| 51 | - $page_id = Fns::get_option( 'modules', 'wishlist', 'page' ); | |
| 52 | - return absint( $page_id ) ? get_permalink( $page_id ) : '#'; | |
| 53 | - } | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Product Add | |
| 57 | - * | |
| 58 | - * @param integer $product_id | |
| 59 | - */ | |
| 60 | - public function add_product( $product_id ) { | |
| 61 | - | |
| 62 | - if ( $this->is_exists_in_wishlist( $product_id ) ) { | |
| 63 | - return 0; | |
| 64 | - } | |
| 65 | - | |
| 66 | - $pIds = $this->get_wishlist_ids(); | |
| 67 | - $pIds[] = $product_id; | |
| 68 | - if ( is_user_logged_in() ) { | |
| 69 | - $this->update_user_wishlist_ids( $pIds ); | |
| 70 | - } else { | |
| 71 | - $cookie_name = $this->get_cookie_name(); | |
| 72 | - | |
| 73 | - setcookie( $cookie_name, wp_json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 74 | - $_COOKIE[ $cookie_name ] = wp_json_encode( $pIds ); | |
| 75 | - } | |
| 76 | - | |
| 77 | - return 1; | |
| 78 | - } | |
| 79 | - | |
| 80 | - /** | |
| 81 | - * [get_products_data] generate wishlist products data | |
| 82 | - * | |
| 83 | - * @return array product list | |
| 84 | - */ | |
| 85 | - public function get_products_data() { | |
| 86 | - | |
| 87 | - $ids = $this->get_wishlist_ids(); | |
| 88 | - | |
| 89 | - if ( empty( $ids ) ) { | |
| 90 | - return []; | |
| 91 | - } | |
| 92 | - | |
| 93 | - $args = [ | |
| 94 | - 'include' => $ids, | |
| 95 | - ]; | |
| 96 | - | |
| 97 | - $products = wc_get_products( $args ); | |
| 98 | - | |
| 99 | - $products_data = []; | |
| 100 | - | |
| 101 | - $fields = $this->get_field_ids(); | |
| 102 | - | |
| 103 | - $fields = array_filter( | |
| 104 | - $fields, | |
| 105 | - function ( $field ) { | |
| 106 | - return 'pa_' === substr( $field, 0, 3 ); | |
| 107 | - }, | |
| 108 | - ARRAY_FILTER_USE_KEY | |
| 109 | - ); | |
| 110 | - | |
| 111 | - $data_none = '-'; | |
| 112 | - | |
| 113 | - foreach ( $products as $product ) { | |
| 114 | - | |
| 115 | - $rating_count = $product->get_rating_count(); | |
| 116 | - $average = $product->get_average_rating(); | |
| 117 | - $quantity_args = [ | |
| 118 | - 'input_value' => 1, | |
| 119 | - 'min_value' => $product->get_min_purchase_quantity(), | |
| 120 | - 'max_value' => $product->get_max_purchase_quantity(), | |
| 121 | - ]; | |
| 122 | - | |
| 123 | - $products_data[ $product->get_id() ] = [ | |
| 124 | - 'id' => $product->get_id(), | |
| 125 | - 'remove' => $product->get_id(), | |
| 126 | - 'image' => $product->get_image() ? $product->get_image() : $data_none, | |
| 127 | - 'title' => $product->get_title() ? $product->get_title() : $data_none, | |
| 128 | - 'image_id' => $product->get_image_id(), | |
| 129 | - 'permalink' => $product->get_permalink(), | |
| 130 | - 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none, | |
| 131 | - 'rating' => wc_get_rating_html( $average, $rating_count ), | |
| 132 | - 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none, | |
| 133 | - 'quantity' => woocommerce_quantity_input( $quantity_args, $product, false ), | |
| 134 | - 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ), | |
| 135 | - 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none, | |
| 136 | - 'weight' => $product->get_weight() ? $product->get_weight() : $data_none, | |
| 137 | - 'sku' => $product->get_sku() ? $product->get_sku() : $data_none, | |
| 138 | - 'availability' => $this->availability_html( $product ), | |
| 139 | - ]; | |
| 140 | - | |
| 141 | - foreach ( $fields as $field_id => $field_name ) { | |
| 142 | - if ( taxonomy_exists( $field_id ) ) { | |
| 143 | - $products_data[ $product->get_id() ][ $field_id ] = []; | |
| 144 | - $terms = get_the_terms( $product->get_id(), $field_id ); | |
| 145 | - if ( ! empty( $terms ) ) { | |
| 146 | - foreach ( $terms as $term ) { | |
| 147 | - $term = sanitize_term( $term, $field_id ); | |
| 148 | - $products_data[ $product->get_id() ][ $field_id ][] = $term->name; | |
| 149 | - } | |
| 150 | - } else { | |
| 151 | - $products_data[ $product->get_id() ][ $field_id ][] = '-'; | |
| 152 | - } | |
| 153 | - $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] ); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - } | |
| 157 | - | |
| 158 | - return $products_data; | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Table field list | |
| 163 | - * | |
| 164 | - * @return array Table Field list | |
| 165 | - */ | |
| 166 | - public function get_field_ids() { | |
| 167 | - | |
| 168 | - $default_show = [ | |
| 169 | - 'info', | |
| 170 | - 'price', | |
| 171 | - 'availability', | |
| 172 | - 'add_to_cart', | |
| 173 | - 'remove', | |
| 174 | - ]; | |
| 175 | - | |
| 176 | - $fields_settings = Fns::get_option( 'modules', 'wishlist', 'page_show_fields', [] ); | |
| 177 | - | |
| 178 | - if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) { | |
| 179 | - $fields = $fields_settings; | |
| 180 | - } else { | |
| 181 | - $fields = $default_show; | |
| 182 | - } | |
| 183 | - | |
| 184 | - return $fields; | |
| 185 | - } | |
| 186 | - | |
| 187 | - /** | |
| 188 | - * Get default fields List | |
| 189 | - * return array | |
| 190 | - */ | |
| 191 | - public function get_default_fields() { | |
| 192 | - $fields = [ | |
| 193 | - 'info' => esc_html__( 'Products', 'shopbuilder' ), | |
| 194 | - 'image' => esc_html__( 'Image', 'shopbuilder' ), | |
| 195 | - 'title' => esc_html__( 'Title', 'shopbuilder' ), | |
| 196 | - 'price' => esc_html__( 'Price', 'shopbuilder' ), | |
| 197 | - 'quantity' => esc_html__( 'Quantity', 'shopbuilder' ), | |
| 198 | - 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ), | |
| 199 | - 'remove' => esc_html__( 'Remove', 'shopbuilder' ), | |
| 200 | - 'description' => esc_html__( 'Description', 'shopbuilder' ), | |
| 201 | - 'availability' => esc_html__( 'Availability', 'shopbuilder' ), | |
| 202 | - 'sku' => esc_html__( 'Sku', 'shopbuilder' ), | |
| 203 | - 'weight' => esc_html__( 'Weight', 'shopbuilder' ), | |
| 204 | - 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ), | |
| 205 | - ]; | |
| 206 | - return apply_filters( 'rtsb/module/wishlist/list_default_fields', $fields ); | |
| 207 | - } | |
| 208 | - | |
| 209 | - /** | |
| 210 | - * [get_cookie_name] Get cookie name | |
| 211 | - * | |
| 212 | - * @return string [string] | |
| 213 | - */ | |
| 214 | - public function get_cookie_name() { | |
| 215 | - $name = Wishlist::KEY; | |
| 216 | - if ( is_multisite() ) { | |
| 217 | - $name .= '_' . get_current_blog_id(); | |
| 218 | - } | |
| 219 | - | |
| 220 | - return $name; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * display_field | |
| 225 | - * | |
| 226 | - * @param string $field_id | |
| 227 | - * @param array $product | |
| 228 | - * | |
| 229 | - * @return void [html] | |
| 230 | - */ | |
| 231 | - public function display_field( $field_id, $product ) { | |
| 232 | - | |
| 233 | - $type = $field_id; | |
| 234 | - | |
| 235 | - if ( 'pa_' === substr( $field_id, 0, 3 ) ) { | |
| 236 | - $type = 'attribute'; | |
| 237 | - } | |
| 238 | - | |
| 239 | - switch ( $type ) { | |
| 240 | - case 'info': | |
| 241 | - ?> | |
| 242 | - <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a> | |
| 243 | - <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product['title'] ); ?></a> | |
| 244 | - <?php | |
| 245 | - break; | |
| 246 | - | |
| 247 | - case 'remove': | |
| 248 | - ?> | |
| 249 | - <a href="#" class="rtsb-wishlist-remove" | |
| 250 | - data-product_id="<?php echo esc_attr( $product['id'] ); ?>"><i class="rtsb-icon rtsb-icon-trash-empty"></i></a> | |
| 251 | - <?php | |
| 252 | - break; | |
| 253 | - | |
| 254 | - case 'image': | |
| 255 | - ?> | |
| 256 | - <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a> | |
| 257 | - <?php | |
| 258 | - break; | |
| 259 | - | |
| 260 | - case 'title': | |
| 261 | - ?> | |
| 262 | - <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product[ $field_id ] ); ?></a> | |
| 263 | - <?php | |
| 264 | - break; | |
| 265 | - | |
| 266 | - case 'price': | |
| 267 | - echo wp_kses_post( $product[ $field_id ] ); | |
| 268 | - break; | |
| 269 | - | |
| 270 | - case 'quantity': | |
| 271 | - Fns::print_html( $product[ $field_id ] ); | |
| 272 | - break; | |
| 273 | - | |
| 274 | - case 'ratting': | |
| 275 | - echo '<span class="rtsb-wl-product-ratting">' . wp_kses_post( $product[ $field_id ] ) . '</span>'; | |
| 276 | - break; | |
| 277 | - | |
| 278 | - case 'add_to_cart': | |
| 279 | - Fns::print_html( apply_filters( 'rtsb/modules/wishlist/add_to_cart_btn', $product[ $field_id ] ) ); | |
| 280 | - break; | |
| 281 | - | |
| 282 | - case 'attribute': | |
| 283 | - echo wp_kses_post( $product[ $field_id ] ); | |
| 284 | - break; | |
| 285 | - | |
| 286 | - case 'weight': | |
| 287 | - if ( $product[ $field_id ] ) { | |
| 288 | - $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : ''; | |
| 289 | - Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) ); | |
| 290 | - } | |
| 291 | - break; | |
| 292 | - | |
| 293 | - case 'description': | |
| 294 | - Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) ); | |
| 295 | - break; | |
| 296 | - | |
| 297 | - default: | |
| 298 | - echo wp_kses_post( $product[ $field_id ] ); | |
| 299 | - break; | |
| 300 | - } | |
| 301 | - } | |
| 302 | - | |
| 303 | - /** | |
| 304 | - * Field name | |
| 305 | - * | |
| 306 | - * @param string $field | |
| 307 | - * | |
| 308 | - * @return string|void | |
| 309 | - */ | |
| 310 | - public function field_name( $field, $custom = false ) { | |
| 311 | - | |
| 312 | - if ( empty( $field ) ) { | |
| 313 | - return; | |
| 314 | - } | |
| 315 | - | |
| 316 | - if ( $custom === true ) { | |
| 317 | - return $field; | |
| 318 | - } | |
| 319 | - | |
| 320 | - $default = $this->get_default_fields(); | |
| 321 | - | |
| 322 | - $str = substr( $field, 0, 3 ); | |
| 323 | - if ( 'pa_' === $str ) { | |
| 324 | - $field_name = wc_attribute_label( $field ); | |
| 325 | - } else { | |
| 326 | - $field_name = $default[ $field ]; | |
| 327 | - } | |
| 328 | - | |
| 329 | - return $field_name; | |
| 330 | - } | |
| 331 | - | |
| 332 | - /** | |
| 333 | - * Get wishlist product ids | |
| 334 | - * | |
| 335 | - * @return array | |
| 336 | - */ | |
| 337 | - public function get_wishlist_ids() { | |
| 338 | - $ids = []; | |
| 339 | - if ( is_user_logged_in() ) { | |
| 340 | - $listIds = get_user_meta( get_current_user_id(), Wishlist::KEY, true ); | |
| 341 | - if ( is_array( $listIds ) && ! empty( $listIds ) ) { | |
| 342 | - $ids = array_map( 'absint', $listIds ); | |
| 343 | - } | |
| 344 | - } else { | |
| 345 | - $cookie_name = $this->get_cookie_name(); | |
| 346 | - if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { | |
| 347 | - $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ); | |
| 348 | - } | |
| 349 | - } | |
| 350 | - | |
| 351 | - return $ids; | |
| 352 | - } | |
| 353 | - | |
| 354 | - public function update_user_wishlist_ids( $product_ids ) { | |
| 355 | - $uid = get_current_user_id(); | |
| 356 | - update_user_meta( $uid, Wishlist::KEY, $product_ids ); | |
| 357 | - } | |
| 358 | - | |
| 359 | - /** | |
| 360 | - * @param $product_id | |
| 361 | - * | |
| 362 | - * @return bool | |
| 363 | - */ | |
| 364 | - public function is_exists_in_wishlist( $product_id ) { | |
| 365 | - $id = $product_id; | |
| 366 | - $list = $this->get_wishlist_ids(); | |
| 367 | - if ( is_array( $list ) ) { | |
| 368 | - return in_array( $id, $list, true ); | |
| 369 | - } else { | |
| 370 | - return false; | |
| 371 | - } | |
| 372 | - } | |
| 373 | - | |
| 374 | - /** | |
| 375 | - * @param WC_Product $product | |
| 376 | - * | |
| 377 | - * @return string | |
| 378 | - */ | |
| 379 | - public function availability_html( $product ) { | |
| 380 | - $html = ''; | |
| 381 | - $availability = $product->get_availability(); | |
| 382 | - | |
| 383 | - if ( empty( $availability['availability'] ) ) { | |
| 384 | - $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' ); | |
| 385 | - } | |
| 386 | - | |
| 387 | - if ( ! empty( $availability['availability'] ) ) { | |
| 388 | - ob_start(); | |
| 389 | - | |
| 390 | - wc_get_template( | |
| 391 | - 'single-product/stock.php', | |
| 392 | - [ | |
| 393 | - 'product' => $product, | |
| 394 | - 'class' => $availability['class'], | |
| 395 | - 'availability' => $availability['availability'], | |
| 396 | - ] | |
| 397 | - ); | |
| 398 | - | |
| 399 | - $html = ob_get_clean(); | |
| 400 | - } | |
| 401 | - | |
| 402 | - return apply_filters( 'woocommerce_get_stock_html', $html, $product ); | |
| 403 | - } | |
| 404 | - | |
| 405 | - /** | |
| 406 | - * Generate Cart button | |
| 407 | - * | |
| 408 | - * @param WC_Product $product | |
| 409 | - */ | |
| 410 | - public function add_to_cart_html( $product ) { | |
| 411 | - if ( ! $product ) { | |
| 412 | - return; | |
| 413 | - } | |
| 414 | - | |
| 415 | - $defaults = [ | |
| 416 | - 'quantity' => 1, | |
| 417 | - 'class' => implode( | |
| 418 | - ' ', | |
| 419 | - array_filter( | |
| 420 | - [ | |
| 421 | - 'htcompare-cart-button button', | |
| 422 | - 'product_type_' . $product->get_type(), | |
| 423 | - $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', | |
| 424 | - $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', | |
| 425 | - ] | |
| 426 | - ) | |
| 427 | - ), | |
| 428 | - 'attributes' => [ | |
| 429 | - 'data-product_id' => $product->get_id(), | |
| 430 | - 'data-product_sku' => $product->get_sku(), | |
| 431 | - 'aria-label' => $product->add_to_cart_description(), | |
| 432 | - 'rel' => 'nofollow', | |
| 433 | - ], | |
| 434 | - ]; | |
| 435 | - | |
| 436 | - $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product ); | |
| 437 | - | |
| 438 | - if ( isset( $args['attributes']['aria-label'] ) ) { | |
| 439 | - $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); | |
| 440 | - } | |
| 441 | - | |
| 442 | - return apply_filters( | |
| 443 | - 'woocommerce_loop_add_to_cart_link', | |
| 444 | - sprintf( | |
| 445 | - '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>', | |
| 446 | - esc_url( $product->add_to_cart_url() ), | |
| 447 | - esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), | |
| 448 | - esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), | |
| 449 | - isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', | |
| 450 | - esc_html( $product->add_to_cart_text() ) | |
| 451 | - ), | |
| 452 | - $product, | |
| 453 | - $args | |
| 454 | - ); | |
| 455 | - } | |
| 456 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | + | |
| 5 | +use RadiusTheme\SB\Helpers\Fns; | |
| 6 | +use RadiusTheme\SB\Traits\SingletonTrait; | |
| 7 | +use WC_Product; | |
| 8 | + | |
| 9 | +// Do not allow directly accessing this file. | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 'This script cannot be accessed directly.' ); | |
| 12 | +} | |
| 13 | + | |
| 14 | +class WishlistFns { | |
| 15 | + | |
| 16 | + use SingletonTrait; | |
| 17 | + | |
| 18 | + const WC_MENU_SLUG = 'wishlist'; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Remove product id | |
| 22 | + * | |
| 23 | + * @param int $product_id | |
| 24 | + * | |
| 25 | + * @return bool|int | |
| 26 | + */ | |
| 27 | + public function remove_product( $product_id ) { | |
| 28 | + | |
| 29 | + if ( ! $this->is_exists_in_wishlist( $product_id ) ) { | |
| 30 | + return 0; | |
| 31 | + } | |
| 32 | + $pIds = $this->get_wishlist_ids(); | |
| 33 | + $pIds = array_diff( $pIds, [ $product_id ] ); | |
| 34 | + if ( is_user_logged_in() ) { | |
| 35 | + $this->update_user_wishlist_ids( $pIds ); | |
| 36 | + } else { | |
| 37 | + $cookie_name = $this->get_cookie_name(); | |
| 38 | + if ( empty( $pIds ) ) { | |
| 39 | + setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 40 | + $_COOKIE[ $cookie_name ] = false; | |
| 41 | + } else { | |
| 42 | + setcookie( $cookie_name, json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 43 | + $_COOKIE[ $cookie_name ] = json_encode( $pIds ); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + | |
| 47 | + return true; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public static function get_page_url() { | |
| 51 | + $page_id = Fns::get_option( 'modules', 'wishlist', 'page' ); | |
| 52 | + | |
| 53 | + return get_permalink( $page_id ); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Product Add | |
| 58 | + * | |
| 59 | + * @param integer $product_id | |
| 60 | + */ | |
| 61 | + public function add_product( $product_id ) { | |
| 62 | + | |
| 63 | + if ( $this->is_exists_in_wishlist( $product_id ) ) { | |
| 64 | + return 0; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $pIds = $this->get_wishlist_ids(); | |
| 68 | + $pIds[] = $product_id; | |
| 69 | + if ( is_user_logged_in() ) { | |
| 70 | + $this->update_user_wishlist_ids( $pIds ); | |
| 71 | + } else { | |
| 72 | + $cookie_name = $this->get_cookie_name(); | |
| 73 | + | |
| 74 | + setcookie( $cookie_name, json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); | |
| 75 | + $_COOKIE[ $cookie_name ] = json_encode( $pIds ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + return 1; | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * [get_products_data] generate wishlist products data | |
| 83 | + * | |
| 84 | + * @return array product list | |
| 85 | + */ | |
| 86 | + public function get_products_data() { | |
| 87 | + | |
| 88 | + $ids = $this->get_wishlist_ids(); | |
| 89 | + | |
| 90 | + if ( empty( $ids ) ) { | |
| 91 | + return []; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $args = [ | |
| 95 | + 'include' => $ids, | |
| 96 | + ]; | |
| 97 | + | |
| 98 | + $products = wc_get_products( $args ); | |
| 99 | + | |
| 100 | + $products_data = []; | |
| 101 | + | |
| 102 | + $fields = $this->get_field_ids(); | |
| 103 | + | |
| 104 | + $fields = array_filter( | |
| 105 | + $fields, | |
| 106 | + function ( $field ) { | |
| 107 | + return 'pa_' === substr( $field, 0, 3 ); | |
| 108 | + }, | |
| 109 | + ARRAY_FILTER_USE_KEY | |
| 110 | + ); | |
| 111 | + | |
| 112 | + $data_none = '-'; | |
| 113 | + | |
| 114 | + foreach ( $products as $product ) { | |
| 115 | + | |
| 116 | + $rating_count = $product->get_rating_count(); | |
| 117 | + $average = $product->get_average_rating(); | |
| 118 | + $quantity_args = [ | |
| 119 | + 'input_value' => 1, | |
| 120 | + 'min_value' => $product->get_min_purchase_quantity(), | |
| 121 | + 'max_value' => $product->get_max_purchase_quantity(), | |
| 122 | + ]; | |
| 123 | + | |
| 124 | + $products_data[ $product->get_id() ] = [ | |
| 125 | + 'id' => $product->get_id(), | |
| 126 | + 'remove' => $product->get_id(), | |
| 127 | + 'image' => $product->get_image() ? $product->get_image() : $data_none, | |
| 128 | + 'title' => $product->get_title() ? $product->get_title() : $data_none, | |
| 129 | + 'image_id' => $product->get_image_id(), | |
| 130 | + 'permalink' => $product->get_permalink(), | |
| 131 | + 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none, | |
| 132 | + 'rating' => wc_get_rating_html( $average, $rating_count ), | |
| 133 | + 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none, | |
| 134 | + 'quantity' => woocommerce_quantity_input( $quantity_args, $product, false ), | |
| 135 | + 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ), | |
| 136 | + 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none, | |
| 137 | + 'weight' => $product->get_weight() ? $product->get_weight() : $data_none, | |
| 138 | + 'sku' => $product->get_sku() ? $product->get_sku() : $data_none, | |
| 139 | + 'availability' => $this->availability_html( $product ), | |
| 140 | + ]; | |
| 141 | + | |
| 142 | + foreach ( $fields as $field_id => $field_name ) { | |
| 143 | + if ( taxonomy_exists( $field_id ) ) { | |
| 144 | + $products_data[ $product->get_id() ][ $field_id ] = []; | |
| 145 | + $terms = get_the_terms( $product->get_id(), $field_id ); | |
| 146 | + if ( ! empty( $terms ) ) { | |
| 147 | + foreach ( $terms as $term ) { | |
| 148 | + $term = sanitize_term( $term, $field_id ); | |
| 149 | + $products_data[ $product->get_id() ][ $field_id ][] = $term->name; | |
| 150 | + } | |
| 151 | + } else { | |
| 152 | + $products_data[ $product->get_id() ][ $field_id ][] = '-'; | |
| 153 | + } | |
| 154 | + $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] ); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + } | |
| 158 | + | |
| 159 | + return $products_data; | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Table field list | |
| 164 | + * | |
| 165 | + * @return array Table Field list | |
| 166 | + */ | |
| 167 | + public function get_field_ids() { | |
| 168 | + | |
| 169 | + $default_show = [ | |
| 170 | + 'info', | |
| 171 | + 'price', | |
| 172 | + 'availability', | |
| 173 | + 'add_to_cart', | |
| 174 | + 'remove', | |
| 175 | + ]; | |
| 176 | + | |
| 177 | + $fields_settings = Fns::get_option( 'modules', 'wishlist', 'page_show_fields', [] ); | |
| 178 | + | |
| 179 | + if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) { | |
| 180 | + $fields = $fields_settings; | |
| 181 | + } else { | |
| 182 | + $fields = $default_show; | |
| 183 | + } | |
| 184 | + | |
| 185 | + return $fields; | |
| 186 | + } | |
| 187 | + | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Get default fields List | |
| 191 | + * return array | |
| 192 | + */ | |
| 193 | + function get_default_fields() { | |
| 194 | + $fields = [ | |
| 195 | + 'info' => esc_html__( 'Products', 'shopbuilder' ), | |
| 196 | + 'image' => esc_html__( 'Image', 'shopbuilder' ), | |
| 197 | + 'title' => esc_html__( 'Title', 'shopbuilder' ), | |
| 198 | + 'price' => esc_html__( 'Price', 'shopbuilder' ), | |
| 199 | + 'quantity' => esc_html__( 'Quantity', 'shopbuilder' ), | |
| 200 | + 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ), | |
| 201 | + 'remove' => esc_html__( 'Remove', 'shopbuilder' ), | |
| 202 | + 'description' => esc_html__( 'Description', 'shopbuilder' ), | |
| 203 | + 'availability' => esc_html__( 'Availability', 'shopbuilder' ), | |
| 204 | + 'sku' => esc_html__( 'Sku', 'shopbuilder' ), | |
| 205 | + 'weight' => esc_html__( 'Weight', 'shopbuilder' ), | |
| 206 | + 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ), | |
| 207 | + ]; | |
| 208 | + | |
| 209 | + return apply_filters( 'rtsb/module/wishlist/list_default_fields', $fields ); | |
| 210 | + } | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * [get_cookie_name] Get cookie name | |
| 214 | + * | |
| 215 | + * @return string [string] | |
| 216 | + */ | |
| 217 | + public function get_cookie_name() { | |
| 218 | + $name = Wishlist::KEY; | |
| 219 | + if ( is_multisite() ) { | |
| 220 | + $name .= '_' . get_current_blog_id(); | |
| 221 | + } | |
| 222 | + | |
| 223 | + return $name; | |
| 224 | + } | |
| 225 | + | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * display_field | |
| 229 | + * | |
| 230 | + * @param string $field_id | |
| 231 | + * @param array $product | |
| 232 | + * | |
| 233 | + * @return void [html] | |
| 234 | + */ | |
| 235 | + public function display_field( $field_id, $product ) { | |
| 236 | + | |
| 237 | + $type = $field_id; | |
| 238 | + | |
| 239 | + if ( 'pa_' === substr( $field_id, 0, 3 ) ) { | |
| 240 | + $type = 'attribute'; | |
| 241 | + } | |
| 242 | + | |
| 243 | + switch ( $type ) { | |
| 244 | + case 'info': | |
| 245 | + ?> | |
| 246 | + <a href="<?php echo get_permalink( $product['id'] ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a> | |
| 247 | + <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product['title'] ); ?></a> | |
| 248 | + <?php | |
| 249 | + break; | |
| 250 | + | |
| 251 | + case 'remove': | |
| 252 | + ?> | |
| 253 | + <a href="#" class="rtsb-wishlist-remove" | |
| 254 | + data-product_id="<?php echo esc_attr( $product['id'] ); ?>"><i class="rtsb-icon-trash-empty"></i></a> | |
| 255 | + <?php | |
| 256 | + break; | |
| 257 | + | |
| 258 | + case 'image': | |
| 259 | + ?> | |
| 260 | + <a href="<?php echo get_permalink( $product['id'] ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a> | |
| 261 | + <?php | |
| 262 | + break; | |
| 263 | + | |
| 264 | + case 'title': | |
| 265 | + ?> | |
| 266 | + <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product[ $field_id ] ); ?></a> | |
| 267 | + <?php | |
| 268 | + break; | |
| 269 | + | |
| 270 | + case 'price': | |
| 271 | + echo wp_kses_post( $product[ $field_id ] ); | |
| 272 | + break; | |
| 273 | + | |
| 274 | + case 'quantity': | |
| 275 | + Fns::print_html( $product[ $field_id ] ); | |
| 276 | + break; | |
| 277 | + | |
| 278 | + case 'ratting': | |
| 279 | + echo '<span class="rtsb-wl-product-ratting">' . wp_kses_post( $product[ $field_id ] ) . '</span>'; | |
| 280 | + break; | |
| 281 | + | |
| 282 | + case 'add_to_cart': | |
| 283 | + echo apply_filters( 'rtsb/modules/wishlist/add_to_cart_btn', $product[ $field_id ] ); | |
| 284 | + break; | |
| 285 | + | |
| 286 | + case 'attribute': | |
| 287 | + echo wp_kses_post( $product[ $field_id ] ); | |
| 288 | + break; | |
| 289 | + | |
| 290 | + case 'weight': | |
| 291 | + if ( $product[ $field_id ] ) { | |
| 292 | + $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : ''; | |
| 293 | + echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ); | |
| 294 | + } | |
| 295 | + break; | |
| 296 | + | |
| 297 | + case 'description': | |
| 298 | + echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] ); | |
| 299 | + break; | |
| 300 | + | |
| 301 | + default: | |
| 302 | + echo wp_kses_post( $product[ $field_id ] ); | |
| 303 | + break; | |
| 304 | + } | |
| 305 | + | |
| 306 | + } | |
| 307 | + | |
| 308 | + /** | |
| 309 | + * Field name | |
| 310 | + * | |
| 311 | + * @param string $field | |
| 312 | + * | |
| 313 | + * @return string|void | |
| 314 | + */ | |
| 315 | + public function field_name( $field, $custom = false ) { | |
| 316 | + | |
| 317 | + if ( empty( $field ) ) { | |
| 318 | + return; | |
| 319 | + } | |
| 320 | + | |
| 321 | + if ( $custom === true ) { | |
| 322 | + return $field; | |
| 323 | + } | |
| 324 | + | |
| 325 | + $default = $this->get_default_fields(); | |
| 326 | + | |
| 327 | + $str = substr( $field, 0, 3 ); | |
| 328 | + if ( 'pa_' === $str ) { | |
| 329 | + $field_name = wc_attribute_label( $field ); | |
| 330 | + } else { | |
| 331 | + $field_name = $default[ $field ]; | |
| 332 | + } | |
| 333 | + | |
| 334 | + return $field_name; | |
| 335 | + | |
| 336 | + } | |
| 337 | + | |
| 338 | + | |
| 339 | + /** | |
| 340 | + * Get wishlist product ids | |
| 341 | + * | |
| 342 | + * @return array | |
| 343 | + */ | |
| 344 | + public function get_wishlist_ids() { | |
| 345 | + $ids = []; | |
| 346 | + if ( is_user_logged_in() ) { | |
| 347 | + $listIds = get_user_meta( get_current_user_id(), Wishlist::KEY, true ); | |
| 348 | + if ( is_array( $listIds ) && ! empty( $listIds ) ) { | |
| 349 | + $ids = array_map( 'absint', $listIds ); | |
| 350 | + } | |
| 351 | + } else { | |
| 352 | + $cookie_name = $this->get_cookie_name(); | |
| 353 | + if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { | |
| 354 | + $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + | |
| 358 | + return $ids; | |
| 359 | + } | |
| 360 | + | |
| 361 | + public function update_user_wishlist_ids( $product_ids ) { | |
| 362 | + $uid = get_current_user_id(); | |
| 363 | + update_user_meta( $uid, Wishlist::KEY, $product_ids ); | |
| 364 | + } | |
| 365 | + | |
| 366 | + /** | |
| 367 | + * @param $product_id | |
| 368 | + * | |
| 369 | + * @return bool | |
| 370 | + */ | |
| 371 | + public function is_exists_in_wishlist( $product_id ) { | |
| 372 | + $id = $product_id; | |
| 373 | + $list = $this->get_wishlist_ids(); | |
| 374 | + if ( is_array( $list ) ) { | |
| 375 | + return in_array( $id, $list, true ); | |
| 376 | + } else { | |
| 377 | + return false; | |
| 378 | + } | |
| 379 | + | |
| 380 | + } | |
| 381 | + | |
| 382 | + | |
| 383 | + /** | |
| 384 | + * @param WC_Product $product | |
| 385 | + * | |
| 386 | + * @return string | |
| 387 | + */ | |
| 388 | + public function availability_html( $product ) { | |
| 389 | + $html = ''; | |
| 390 | + $availability = $product->get_availability(); | |
| 391 | + | |
| 392 | + if ( empty( $availability['availability'] ) ) { | |
| 393 | + $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' ); | |
| 394 | + } | |
| 395 | + | |
| 396 | + if ( ! empty( $availability['availability'] ) ) { | |
| 397 | + ob_start(); | |
| 398 | + | |
| 399 | + wc_get_template( | |
| 400 | + 'single-product/stock.php', | |
| 401 | + [ | |
| 402 | + 'product' => $product, | |
| 403 | + 'class' => $availability['class'], | |
| 404 | + 'availability' => $availability['availability'], | |
| 405 | + ] | |
| 406 | + ); | |
| 407 | + | |
| 408 | + $html = ob_get_clean(); | |
| 409 | + } | |
| 410 | + | |
| 411 | + return apply_filters( 'woocommerce_get_stock_html', $html, $product ); | |
| 412 | + } | |
| 413 | + | |
| 414 | + | |
| 415 | + /** | |
| 416 | + * Generate Cart button | |
| 417 | + * | |
| 418 | + * @param WC_Product $product | |
| 419 | + */ | |
| 420 | + public function add_to_cart_html( $product ) { | |
| 421 | + if ( ! $product ) { | |
| 422 | + return; | |
| 423 | + } | |
| 424 | + | |
| 425 | + $defaults = [ | |
| 426 | + 'quantity' => 1, | |
| 427 | + 'class' => implode( | |
| 428 | + ' ', | |
| 429 | + array_filter( | |
| 430 | + [ | |
| 431 | + 'htcompare-cart-button button', | |
| 432 | + 'product_type_' . $product->get_type(), | |
| 433 | + $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', | |
| 434 | + $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', | |
| 435 | + ] | |
| 436 | + ) | |
| 437 | + ), | |
| 438 | + 'attributes' => [ | |
| 439 | + 'data-product_id' => $product->get_id(), | |
| 440 | + 'data-product_sku' => $product->get_sku(), | |
| 441 | + 'aria-label' => $product->add_to_cart_description(), | |
| 442 | + 'rel' => 'nofollow', | |
| 443 | + ], | |
| 444 | + ]; | |
| 445 | + | |
| 446 | + $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product ); | |
| 447 | + | |
| 448 | + if ( isset( $args['attributes']['aria-label'] ) ) { | |
| 449 | + $args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] ); | |
| 450 | + } | |
| 451 | + | |
| 452 | + return apply_filters( | |
| 453 | + 'woocommerce_loop_add_to_cart_link', | |
| 454 | + sprintf( | |
| 455 | + '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>', | |
| 456 | + esc_url( $product->add_to_cart_url() ), | |
| 457 | + esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), | |
| 458 | + esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), | |
| 459 | + isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', | |
| 460 | + esc_html( $product->add_to_cart_text() ) | |
| 461 | + ), | |
| 462 | + $product, | |
| 463 | + $args | |
| 464 | + ); | |
| 465 | + } | |
| 466 | +} | |