comparison.php
148 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 = ' |
| 100 | <svg xmlns="http://www.w3.org/2000/svg" width="20" viewBox="0 0 15 18"> |
| 101 | <g transform="translate(-4 -2.4)"><g ><path d="M15.25,9.9a.75.75,0,0,1-.53-1.28l2.469-2.47L14.72,3.68a.75.75,0,1,1,1.06-1.06l3,3a.749.749,0,0,1,0,1.06l-3,3A.744.744,0,0,1,15.25,9.9Z"/> </g> <g ><path d="M4.75,11.4A.75.75,0,0,1,4,10.65V9.15A3.754,3.754,0,0,1,7.75,5.4h10.5a.75.75,0,0,1,0,1.5H7.75A2.252,2.252,0,0,0,5.5,9.15v1.5A.75.75,0,0,1,4.75,11.4Z"/> </g> <g ><path d="M7.75,20.4a.744.744,0,0,1-.53-.22l-3-3a.749.749,0,0,1,0-1.06l3-3a.75.75,0,0,1,1.06,1.06L5.811,16.65,8.28,19.12a.75.75,0,0,1-.53,1.28Z"/></g> <g> <path id="Path_14" data-name="Path 14" d="M15.25,17.4H4.75a.75.75,0,0,1,0-1.5h10.5a2.252,2.252,0,0,0,2.25-2.25v-1.5a.75.75,0,0,1,1.5,0v1.5A3.754,3.754,0,0,1,15.25,17.4Z"/> </g></g> |
| 102 | </svg> |
| 103 | '; |
| 104 | |
| 105 | echo $left_text; ?> |
| 106 | |
| 107 | <a |
| 108 | data-payload='{"pid":<?php echo intval($pid) ?>}' |
| 109 | class="<?php echo self::ONCLICK_SELECTOR_CLS ?> shopengine-comparison badge <?php echo esc_attr($cls) ?>" |
| 110 | > <?php echo $compare_icon ?> </a><?php |
| 111 | |
| 112 | echo $right_text; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | function print_button_in_shop($add_to_cart_html, $product, $args = []) { |
| 117 | |
| 118 | $left_text = apply_filters('shopengine/module/wishlist/optional_text_left', ''); |
| 119 | $right_text = apply_filters('shopengine/module/wishlist/optional_text_right', ''); |
| 120 | |
| 121 | $pid = $product->get_id(); |
| 122 | $exist = $this->is_exists_in_list($pid); |
| 123 | $cls = $exist ? 'active' : 'inactive'; |
| 124 | $compare_icon = ' |
| 125 | <svg xmlns="http://www.w3.org/2000/svg" width="13" viewBox="0 0 15 18"> |
| 126 | <g transform="translate(-4 -2.4)"><g ><path d="M15.25,9.9a.75.75,0,0,1-.53-1.28l2.469-2.47L14.72,3.68a.75.75,0,1,1,1.06-1.06l3,3a.749.749,0,0,1,0,1.06l-3,3A.744.744,0,0,1,15.25,9.9Z"/> </g> <g ><path d="M4.75,11.4A.75.75,0,0,1,4,10.65V9.15A3.754,3.754,0,0,1,7.75,5.4h10.5a.75.75,0,0,1,0,1.5H7.75A2.252,2.252,0,0,0,5.5,9.15v1.5A.75.75,0,0,1,4.75,11.4Z"/> </g> <g ><path d="M7.75,20.4a.744.744,0,0,1-.53-.22l-3-3a.749.749,0,0,1,0-1.06l3-3a.75.75,0,0,1,1.06,1.06L5.811,16.65,8.28,19.12a.75.75,0,0,1-.53,1.28Z"/></g> <g> <path id="Path_14" data-name="Path 14" d="M15.25,17.4H4.75a.75.75,0,0,1,0-1.5h10.5a2.252,2.252,0,0,0,2.25-2.25v-1.5a.75.75,0,0,1,1.5,0v1.5A3.754,3.754,0,0,1,15.25,17.4Z"/> </g></g> |
| 127 | </svg> |
| 128 | '; |
| 129 | |
| 130 | $btn = $left_text . |
| 131 | '<a data-payload=\'{"pid":' . $product->get_id() . '}\''. |
| 132 | ' class="' . self::ONCLICK_SELECTOR_CLS . ' shopengine-comparison badge se-btn ' . esc_attr($cls) . '"> '. $compare_icon .' </a>' . |
| 133 | $right_text; |
| 134 | |
| 135 | |
| 136 | if($this->get_where_to() == 'after') { |
| 137 | $before = ''; |
| 138 | $after = $btn; |
| 139 | } else { |
| 140 | |
| 141 | $before = $btn; |
| 142 | $after = ''; |
| 143 | } |
| 144 | |
| 145 | return $before . $add_to_cart_html . $after; |
| 146 | } |
| 147 | } |
| 148 |