comparison.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Comparison; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | class Comparison |
| 8 | { |
| 9 | const COOKIE_KEY = 'shopengine_comparison_id_list'; |
| 10 | const ONCLICK_SELECTOR_CLS = 'shopengine_comparison_add_to_list_action'; |
| 11 | const COOKIE_TIME_IN_DAYS = 1; |
| 12 | |
| 13 | use Singleton; |
| 14 | |
| 15 | public function init() { |
| 16 | |
| 17 | new Route(); |
| 18 | |
| 19 | if($this->get_where_to() == 'after') { |
| 20 | |
| 21 | add_action('woocommerce_after_add_to_cart_button', [$this, 'print_the_button_in_single_page']); |
| 22 | |
| 23 | } else { |
| 24 | |
| 25 | add_action('woocommerce_before_add_to_cart_button', [$this, 'print_the_button_in_single_page']); |
| 26 | } |
| 27 | |
| 28 | $show_in_archive = apply_filters('shopengine/module/comparison/show_icon_in_shop_page', true); |
| 29 | |
| 30 | if($show_in_archive === true) { |
| 31 | |
| 32 | add_filter('woocommerce_loop_add_to_cart_link', [$this, 'print_button_in_shop'], 10, 3); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | public function enqueue() { |
| 41 | |
| 42 | wp_enqueue_style('shopengine-comparison', plugin_dir_url(__FILE__) . '/assets/css/comparison.css'); |
| 43 | |
| 44 | wp_enqueue_script( |
| 45 | 'shopengine-comparison', |
| 46 | plugin_dir_url(__FILE__) . '/assets/js/comparison.js', |
| 47 | ['jquery'] |
| 48 | ); |
| 49 | |
| 50 | wp_localize_script('shopengine-comparison', 'shopEngineComparison', [ |
| 51 | 'product_id' => get_the_ID(), |
| 52 | 'resturl' => get_rest_url(), |
| 53 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 54 | ]); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function get_where_to() { |
| 59 | |
| 60 | return apply_filters('shopengine/module/comparison/put_icon_in_side', 'after'); |
| 61 | //return apply_filters('shopengine/module/comparison/put_icon_in_side', 'before'); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | private function is_exists_in_list($idd) { |
| 66 | |
| 67 | if(empty($_COOKIE[Comparison::COOKIE_KEY])) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | $content = explode(',', $_COOKIE[Comparison::COOKIE_KEY]); |
| 72 | |
| 73 | |
| 74 | return in_array($idd, $content); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | public function add_to_menu($menu_links) { |
| 79 | |
| 80 | $logout = $menu_links['customer-logout']; |
| 81 | |
| 82 | unset($menu_links['customer-logout']); |
| 83 | |
| 84 | $menu_links['wishlist'] = 'Wishlist'; |
| 85 | $menu_links['customer-logout'] = $logout; |
| 86 | |
| 87 | return $menu_links; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | public function print_the_button_in_single_page() { |
| 92 | |
| 93 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 94 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 95 | |
| 96 | $pid = get_the_ID(); |
| 97 | $exist = $this->is_exists_in_list($pid); |
| 98 | $cls = $exist ? 'active' : 'inactive'; |
| 99 | $compare_icon = '<i class="shopengine-icon-product_compare_1"></i>'; |
| 100 | |
| 101 | echo $left_text; ?> |
| 102 | |
| 103 | <a |
| 104 | data-payload='{"pid":<?php echo intval($pid) ?>}' |
| 105 | class="<?php echo self::ONCLICK_SELECTOR_CLS ?> shopengine-comparison badge <?php echo esc_attr($cls) ?>" |
| 106 | > <?php echo $compare_icon ?> </a><?php |
| 107 | |
| 108 | echo $right_text; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | function print_button_in_shop($add_to_cart_html, $product, $args = []) { |
| 113 | |
| 114 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 115 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 116 | |
| 117 | $pid = $product->get_id(); |
| 118 | $exist = $this->is_exists_in_list($pid); |
| 119 | $cls = $exist ? 'active' : 'inactive'; |
| 120 | $compare_icon = '<i class="shopengine-icon-product_compare_1"></i>'; |
| 121 | |
| 122 | $btn = $left_text . |
| 123 | '<a data-payload=\'{"pid":' . $product->get_id() . '}\''. |
| 124 | ' class="' . self::ONCLICK_SELECTOR_CLS . ' shopengine-comparison badge se-btn ' . esc_attr($cls) . '"> '. $compare_icon .' </a>' . |
| 125 | $right_text; |
| 126 | |
| 127 | |
| 128 | if($this->get_where_to() == 'after') { |
| 129 | $before = ''; |
| 130 | $after = $btn; |
| 131 | } else { |
| 132 | |
| 133 | $before = $btn; |
| 134 | $after = ''; |
| 135 | } |
| 136 | |
| 137 | return $before . $add_to_cart_html . $after; |
| 138 | } |
| 139 | } |
| 140 |