css
1 year ago
img
1 year ago
js
1 year ago
partials
1 year ago
class-email.php
1 year ago
class-eqw-advance.php
1 year ago
class-eqw-enquiry-cart.php
1 year ago
class-eqw-enquiry-shortcode.php
1 year ago
class-eqw-product.php
1 year ago
class-eqw-save-enquiry.php
1 year ago
class-pisol-enquiry-quotation-woocommerce-public.php
1 year ago
class-pisol-form.php
1 year ago
class-webhook.php
1 year ago
index.php
1 year ago
class-eqw-enquiry-cart.php
280 lines
| 1 | <?php |
| 2 | |
| 3 | class class_eqw_enquiry_cart{ |
| 4 | |
| 5 | function __construct(){ |
| 6 | add_action('wc_ajax_pi_add_to_enquiry', array($this, 'add_to_enquiry') ); |
| 7 | add_action('wp_ajax_pi_add_to_enquiry', array($this, 'add_to_enquiry') ); |
| 8 | add_action('wp_ajax_nopriv_pi_add_to_enquiry', array($this, 'add_to_enquiry') ); |
| 9 | |
| 10 | add_action('wc_ajax_pi_remove_product', array($this, 'pi_remove_product') ); |
| 11 | add_action('wp_ajax_pi_remove_product', array($this, 'pi_remove_product') ); |
| 12 | add_action('wp_ajax_nopriv_pi_remove_product', array($this, 'pi_remove_product') ); |
| 13 | |
| 14 | add_action('wc_ajax_pi_update_products', array($this, 'pi_update_products') ); |
| 15 | add_action('wp_ajax_pi_update_products', array($this, 'pi_update_products') ); |
| 16 | add_action('wp_ajax_nopriv_pi_update_products', array($this, 'pi_update_products') ); |
| 17 | |
| 18 | /** |
| 19 | * This is needed as wc session is not created for non-loged in users |
| 20 | */ |
| 21 | add_action( 'woocommerce_init', array($this, 'startSession') ); |
| 22 | } |
| 23 | |
| 24 | function startSession(){ |
| 25 | //self::deleteProductsFromEnquirySession(); |
| 26 | if(isset(WC()->session)){ |
| 27 | if ( !is_admin() && !WC()->session->has_session() ) { |
| 28 | WC()->session->set_customer_session_cookie( true ); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function add_to_enquiry(){ |
| 34 | if(isset($_POST['id']) && isset($_POST['variation_id'])){ |
| 35 | $id = absint($_POST['id']); |
| 36 | $quantity = (int)(isset($_POST['quantity']) ? $_POST['quantity'] : 1); |
| 37 | $variation = absint($_POST['variation_id']); |
| 38 | $variation_detail = $this->sanitizeVariationDetail($_POST['variation_detail']); |
| 39 | $products = $this->addProductToEnquirySession($id, $quantity, $variation, $variation_detail); |
| 40 | } |
| 41 | die; |
| 42 | } |
| 43 | |
| 44 | function sanitizeVariationDetail($variation_detail){ |
| 45 | if(is_array($variation_detail) && count($variation_detail) > 0){ |
| 46 | $sanitized_detail = array(); |
| 47 | foreach($variation_detail as $key => $val){ |
| 48 | $sanitized_detail[sanitize_text_field($key)] = sanitize_text_field($val); |
| 49 | } |
| 50 | return $sanitized_detail; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | function pi_remove_product(){ |
| 56 | $hash = sanitize_text_field($_POST['hash']); |
| 57 | $products = $this->removeProductFromEnquirySession($hash); |
| 58 | ob_start(); |
| 59 | pisol_table_row($products); |
| 60 | $cart = ob_get_contents(); // read ob2 ("b") |
| 61 | ob_end_clean(); |
| 62 | $data = array( |
| 63 | 'cart'=> $cart, |
| 64 | 'pisol_products'=> self::filter_message($products) |
| 65 | ); |
| 66 | echo json_encode($data, JSON_UNESCAPED_SLASHES); |
| 67 | die; |
| 68 | } |
| 69 | |
| 70 | static function filter_message($products){ |
| 71 | if(is_array($products) && count($products) > 0){ |
| 72 | foreach($products as $key => $product){ |
| 73 | $products[$key]['message'] = wp_unslash($products[$key]['message']); |
| 74 | } |
| 75 | } |
| 76 | return $products; |
| 77 | } |
| 78 | |
| 79 | static function get_image($product_id, $variation_id){ |
| 80 | if(empty($variation_id)){ |
| 81 | $product = wc_get_product($product_id); |
| 82 | }else{ |
| 83 | $product = wc_get_product($variation_id); |
| 84 | } |
| 85 | |
| 86 | $image_id = $product->get_image_id(); |
| 87 | |
| 88 | $placeholder = wc_placeholder_img_src( 'thumbnail' ); |
| 89 | |
| 90 | if(!empty($image_id)){ |
| 91 | $src = wp_get_attachment_image_src($image_id, 'thumbnail'); |
| 92 | $image_src = isset($src[0]) ? $src[0] : $placeholder; |
| 93 | }else{ |
| 94 | $image_src = $placeholder; |
| 95 | } |
| 96 | return sprintf('<img src="%s" class="pi-eqw-product-thumb">',esc_url($image_src)); |
| 97 | } |
| 98 | |
| 99 | function pi_update_products(){ |
| 100 | |
| 101 | $products = $this->addProductsToEnquirySession($_POST['products']); |
| 102 | ob_start(); |
| 103 | pisol_table_row($products); |
| 104 | $cart = ob_get_contents(); // read ob2 ("b") |
| 105 | ob_end_clean(); |
| 106 | $data = array( |
| 107 | 'cart'=> $cart, |
| 108 | 'pisol_products'=> self::filter_message($products) |
| 109 | ); |
| 110 | echo json_encode($data, JSON_UNESCAPED_SLASHES); |
| 111 | die; |
| 112 | } |
| 113 | |
| 114 | function addProductsToEnquirySession($products){ |
| 115 | $products = $this->sanitizeProducts($products); |
| 116 | if(isset(WC()->session)){ |
| 117 | WC()->session->set( 'pi_product_enquiries', $products ); |
| 118 | } |
| 119 | return self::getProductsInEnquirySession(); |
| 120 | } |
| 121 | |
| 122 | function sanitizeProducts($products){ |
| 123 | if(is_array($products)){ |
| 124 | foreach($products as $key =>$product){ |
| 125 | $products[$key]['id'] = (int)$products[$key]['id']; |
| 126 | $products[$key]['variation'] = (int)$products[$key]['variation']; |
| 127 | $products[$key]['variation_detail'] = $this->sanitizeVariationDetail($products[$key]['variation_detail']); |
| 128 | |
| 129 | $products[$key]['quantity'] = (int) $products[$key]['quantity']; |
| 130 | $products[$key]['message'] = sanitize_text_field($products[$key]['message']); |
| 131 | if($products[$key]['quantity'] <= 0){ |
| 132 | unset($products[$key]); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return $products; |
| 137 | } |
| 138 | |
| 139 | static function deleteProductsFromEnquirySession(){ |
| 140 | if(isset(WC()->session)){ |
| 141 | WC()->session->__unset( 'pi_product_enquiries'); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | function addProductToEnquirySession($id, $quantity, $variation, $variation_detail){ |
| 146 | $products = self::getProductsInEnquirySession(); |
| 147 | $message = ''; |
| 148 | |
| 149 | if(self::is_variable($id) && $variation == false){ |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | $new_product = array( |
| 154 | 'id'=>(int)$id, |
| 155 | 'quantity'=>(int)$quantity, |
| 156 | 'variation'=>(int)$variation, |
| 157 | 'variation_detail'=>$variation_detail, |
| 158 | 'message'=>strip_tags($message) |
| 159 | ); |
| 160 | |
| 161 | $hash = self::hashGenerator($new_product['id'], $variation_detail); |
| 162 | |
| 163 | if($this->checkProductPresentInEnquirySession($hash)){ |
| 164 | /** |
| 165 | * this will increment it by one, |
| 166 | * as we are not entering the new quantity variable |
| 167 | */ |
| 168 | $this->changeQuantityInEnquirySession($hash, $new_product['quantity']); |
| 169 | return; |
| 170 | |
| 171 | }else{ |
| 172 | |
| 173 | $products[$hash] = $new_product; |
| 174 | } |
| 175 | |
| 176 | return $this->addProductsToEnquirySession($products); |
| 177 | } |
| 178 | |
| 179 | static function is_variable($id){ |
| 180 | $product = wc_get_product($id); |
| 181 | if($product->is_type('variable')){ |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | static function hashGenerator($id, $variation_details){ |
| 188 | $variation_value = ""; |
| 189 | if(is_array($variation_details) && count($variation_details) > 0){ |
| 190 | foreach($variation_details as $key => $variation_detail){ |
| 191 | $variation_value .= $variation_detail; |
| 192 | } |
| 193 | } |
| 194 | $hash = md5($id.$variation_value); |
| 195 | return $hash; |
| 196 | } |
| 197 | |
| 198 | static function getProductsInEnquirySession(){ |
| 199 | $products = array(); |
| 200 | if(isset(WC()->session)){ |
| 201 | $products = WC()->session->get('pi_product_enquiries'); |
| 202 | $products = self::removeTrashedProduct($products); |
| 203 | if($products === null) return array(); |
| 204 | } |
| 205 | return $products; |
| 206 | } |
| 207 | |
| 208 | static function removeTrashedProduct($products){ |
| 209 | if($products === null) return array(); |
| 210 | |
| 211 | foreach($products as $key => $product){ |
| 212 | $product_status = get_post_status( $product['id'] ); |
| 213 | if('publish' !== $product_status){ |
| 214 | unset($products[$key]); |
| 215 | } |
| 216 | } |
| 217 | return $products; |
| 218 | } |
| 219 | |
| 220 | static function isThereProductsInEnquirySession(){ |
| 221 | $products = self::getProductsInEnquirySession(); |
| 222 | if(is_array($products) && count($products) > 0){ |
| 223 | return true; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * return true if product present in cart |
| 230 | */ |
| 231 | function checkProductPresentInEnquirySession($hash){ |
| 232 | $products = self::getProductsInEnquirySession(); |
| 233 | $present = false; |
| 234 | |
| 235 | if(isset($products[$hash])){ |
| 236 | $present = true; |
| 237 | } |
| 238 | |
| 239 | return $present; |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * If $new_quantity is false will increment the existing quantity |
| 245 | * if it is not false and is a number then will it will update existing quantity |
| 246 | * if new quantity is zero it will remove the product from list |
| 247 | */ |
| 248 | function changeQuantityInEnquirySession($hash, $new_quantity = false){ |
| 249 | $products = self::getProductsInEnquirySession(); |
| 250 | |
| 251 | if($new_quantity === 0){ |
| 252 | $this->removeProductFromEnquirySession($hash); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if(is_array($products) && count($products) > 0){ |
| 257 | if($new_quantity){ |
| 258 | $products[$hash]['quantity'] = $products[$hash]['quantity']+$new_quantity; |
| 259 | }else{ |
| 260 | $products[$hash]['quantity'] = $products[$hash]['quantity']+1; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | $this->addProductsToEnquirySession($products); |
| 265 | |
| 266 | } |
| 267 | |
| 268 | function removeProductFromEnquirySession($hash){ |
| 269 | $products = self::getProductsInEnquirySession(); |
| 270 | if(is_array($products) && count($products) > 0){ |
| 271 | |
| 272 | unset($products[$hash]); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | return $this->addProductsToEnquirySession($products); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | new class_eqw_enquiry_cart(); |