- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
woo-bulk-editor/trunk/ext/bulkoperations/bulkoperations.php
r2844667 r2970262 1 1 <?php 2 2 if (!defined('ABSPATH')) { 3 3 exit; // Exit if accessed directly 4 4 } 5 5 6 6 //any operations with variations only 7 7 final class WOOBE_BULKOPERATIONS extends WOOBE_EXT { 8 8 9 9 protected $slug = 'bulkoperations'; //unique 10 10 11 11 //protected $is = 'external'; 12 12 13 13 public function __construct() { 14 14 add_action('woobe_ext_scripts', array($this, 'woobe_ext_scripts'), 1); 15 15 add_action('woobe_tools_panel_buttons', array($this, 'woobe_tools_panel_buttons'), 1); 16 16 add_action('woobe_page_end', array($this, 'woobe_page_end'), 1); 17 17 18 18 //ajax 19 19 add_action('wp_ajax_woobe_bulkoperations_get_att_terms', array($this, 'woobe_bulkoperations_get_att_terms'), 1); 20 20 add_action('wp_ajax_woobe_bulkoperations_get_possible_combos', array($this, 'woobe_bulkoperations_get_possible_combos'), 1); 21 21 add_action('wp_ajax_woobe_bulkoperations_get_prod_count', array($this, 'woobe_bulkoperations_get_prod_count'), 1); 22 22 add_action('wp_ajax_woobe_bulkoperations_apply_combinations', array($this, 'woobe_bulkoperations_apply_combinations'), 1); 23 23 add_action('wp_ajax_woobe_bulkoperations_apply_default_combination', array($this, 'woobe_bulkoperations_apply_default_combination'), 1); 24 24 add_action('wp_ajax_woobe_bulkoperations_get_product_variations', array($this, 'woobe_bulkoperations_get_product_variations'), 1); 25 25 add_action('wp_ajax_woobe_bulkoperations_delete', array($this, 'woobe_bulkoperations_delete'), 1); 26 26 add_action('wp_ajax_woobe_bulkoperations_ordering', array($this, 'woobe_bulkoperations_ordering'), 1); 27 27 add_action('wp_ajax_woobe_bulkoperations_swap', array($this, 'woobe_bulkoperations_swap'), 1); 28 28 add_action('wp_ajax_woobe_bulkoperations_attaching', array($this, 'woobe_bulkoperations_attaching'), 1); 29 29 add_action('wp_ajax_woobe_bulkoperations_visibility', array($this, 'woobe_bulkoperations_visibility'), 1); 30 30 } 31 31 32 32 public function woobe_ext_scripts() { 33 33 wp_enqueue_script('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/js/' . $this->slug . '.js', array(), WOOBE_VERSION); 34 34 wp_enqueue_script('woobe_ext_' . $this->slug . '-2', $this->get_ext_link() . 'assets/js/' . $this->slug . '-2.js', array(), WOOBE_VERSION); 35 35 wp_enqueue_script('woobe_ext_' . $this->slug . '-3', $this->get_ext_link() . 'assets/js/' . $this->slug . '-3.js', array(), WOOBE_VERSION); 36 36 wp_enqueue_script('woobe_ext_' . $this->slug . '-4', $this->get_ext_link() . 'assets/js/' . $this->slug . '-4.js', array(), WOOBE_VERSION); 37 37 wp_enqueue_script('woobe_ext_' . $this->slug . '-5', $this->get_ext_link() . 'assets/js/' . $this->slug . '-5.js', array(), WOOBE_VERSION); 38 38 wp_enqueue_script('woobe_ext_' . $this->slug . '-6', $this->get_ext_link() . 'assets/js/' . $this->slug . '-6.js', array(), WOOBE_VERSION); 39 39 wp_enqueue_script('woobe_ext_' . $this->slug . '-7', $this->get_ext_link() . 'assets/js/' . $this->slug . '-7.js', array(), WOOBE_VERSION); 40 40 wp_enqueue_style('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/css/' . $this->slug . '.css', array(), WOOBE_VERSION); 41 41 ?> 42 42 <script> 43 43 lang.<?php echo $this->slug ?> = {}; 44 44 lang.<?php echo $this->slug ?>.going = '<?php esc_html_e('ATTENTION: Variations Advanced Bulk Operation is going', 'woo-bulk-editor') ?>'; 45 45 lang.<?php echo $this->slug ?>.finished = '<?php esc_html_e('Variations Advanced Bulk Operation is finished', 'woo-bulk-editor') ?>'; 46 46 lang.<?php echo $this->slug ?>.finished2 = '<?php esc_html_e('Attaching of the default combination for the products variations is finished!', 'woo-bulk-editor') ?>'; 47 47 lang.<?php echo $this->slug ?>.finished3 = '<?php esc_html_e('Deleting of the products variations is finished', 'woo-bulk-editor') ?>'; 48 48 lang.<?php echo $this->slug ?>.finished4 = '<?php esc_html_e('Ordering of the products variations is finished', 'woo-bulk-editor') ?>'; 49 49 lang.<?php echo $this->slug ?>.finished5 = '<?php esc_html_e('Swap of variations is finished', 'woo-bulk-editor') ?>'; 50 50 lang.<?php echo $this->slug ?>.finished6 = '<?php esc_html_e('Attaching of the products variations is finished', 'woo-bulk-editor') ?>'; 51 51 lang.<?php echo $this->slug ?>.generating = '<?php esc_html_e('Generating possible combinations', 'woo-bulk-editor') ?>'; 52 52 lang.<?php echo $this->slug ?>.generated = '<?php esc_html_e('Possible combinations been generated.', 'woo-bulk-editor') ?>'; 53 53 lang.<?php echo $this->slug ?>.no_combinations = '<?php esc_html_e('Combination(s) not selected!', 'woo-bulk-editor') ?>'; 54 54 lang.<?php echo $this->slug ?>.not_selected_var = '<?php esc_html_e('variation is not selected', 'woo-bulk-editor') ?>'; 55 55 lang.<?php echo $this->slug ?>.no_vars = '<?php esc_html_e('the product has no variations', 'woo-bulk-editor') ?>'; 56 56 </script> 57 57 <?php 58 58 } 59 59 60 60 public function woobe_tools_panel_buttons() { 61 61 ?> 62 62 <a href="#" class="button button-secondary woobe_tools_panel_newvars_btn" title="<?php esc_html_e('Variations Advanced Bulk Operations', 'woo-bulk-editor') ?>"></a> 63 63 <?php 64 64 } 65 65 66 66 public function woobe_page_end() { 67 67 $data = array(); 68 68 $data['attributes'] = wc_get_attribute_taxonomies(); 69 69 echo WOOBE_HELPER::render_html($this->get_ext_path() . 'views/panel.php', $data); 70 70 } 71 71 72 72 //ajax 73 73 public function woobe_bulkoperations_get_att_terms() { 74 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 75 die('0'); 76 } 74 77 die(json_encode(WOOBE_HELPER::get_taxonomies_terms_hierarchy(sanitize_text_field($_REQUEST['attribute'])))); 75 78 } 76 79 77 80 //ajax 78 81 public function woobe_bulkoperations_get_possible_combos() { 82 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 83 die('0'); 84 } 79 85 try { 80 86 $res = ""; 81 87 if (isset($_REQUEST['arrays'])) { 82 88 //no db writing, ajax to DOM 83 89 $res = json_encode($this->generate_combinations($_REQUEST['arrays'])); 84 90 } 85 91 } catch (Exception $e) { 86 92 print_r($e); 87 93 } 88 94 die($res); 89 95 } 90 96 91 97 //https://gist.github.com/fabiocicerchia/4556892 92 98 private function generate_combinations($data, &$all = array(), array $group = array(), $value = null, $i = 0) { 99 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 100 die('0'); 101 } 93 102 if (!is_array($data)) { 94 103 $data = array(); 95 104 } 96 105 $keys = array_keys($data); 97 106 if (isset($value) === true) { 98 107 array_push($group, intval($value)); 99 108 } 100 109 101 110 if ($i >= count($data)) { 102 111 array_push($all, $group); 103 112 } else { 104 113 $currentKey = $keys[$i]; 105 114 $currentElement = $data[$currentKey]; 106 115 foreach ($currentElement as $val) { 107 116 $this->generate_combinations($data, $all, $group, $val, $i + 1); 108 117 } 109 118 } 110 119 111 120 return $all; 112 121 } 113 122 114 123 //ajax 115 124 public function woobe_bulkoperations_get_prod_count() { 116 125 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 126 die('0'); 127 } 117 128 if (!current_user_can('manage_woocommerce')) { 118 129 die('0'); 119 130 } 120 131 121 132 //*** 122 133 123 134 $products = $this->products->gets(array( 124 135 'fields' => 'ids', 125 136 'no_found_rows' => true 126 137 )); 127 138 echo json_encode($products->posts); 128 139 129 140 exit; 130 141 } 131 142 132 143 //ajax 133 144 public function woobe_bulkoperations_apply_combinations() { 134 145 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 146 die('0'); 147 } 135 148 if (!isset($_REQUEST['products_ids'])) { 136 149 die('0'); 137 150 } 138 151 139 152 //*** 140 153 141 154 $products_ids = array_map(function($item) { 142 155 return intval($item); //sanitize intval 143 156 }, $_REQUEST['products_ids']); 144 157 145 158 $combinations = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combinations']); 146 159 147 160 $possible_attributes = array(); 148 161 //for set_product_attributes 149 162 $set_product_attributes = array(); 150 163 151 164 //*** 152 165 153 166 if (!empty($products_ids) AND!empty($combinations)) { 154 167 155 168 //lets prepare combinations for applying 156 169 $terms = array(); 157 170 foreach ($combinations as $comb) { 158 171 if (!empty($comb)) { 159 172 foreach ($comb as $term_id) { 160 173 $terms[$term_id] = get_term($term_id, '', ARRAY_A); 161 174 } 162 175 } 163 176 } 164 177 165 178 //*** 166 179 167 180 foreach ($terms as $t) { 168 181 $set_product_attributes[$t['taxonomy']][] = $t['term_id']; 169 182 } 170 183 171 184 foreach ($combinations as $k => $comb) { 172 185 if (!empty($comb)) { 173 186 foreach ($comb as $term_id) { 174 187 $possible_attributes[$k][strtolower(urlencode($terms[$term_id]['taxonomy']))] = $terms[$term_id]['slug']; 175 188 } 176 189 } 177 190 } 178 191 179 192 //*** 180 193 //wp-content\plugins\woocommerce\includes\class-wc-ajax.php -> public static function link_all_variations 181 194 wc_maybe_define_constant('WC_MAX_LINKED_VARIATIONS', 50); 182 195 wc_set_time_limit(0); 183 196 184 197 185 198 /* 186 199 * Leaved as an example of data structure 187 200 $possible_attributes = array( 188 201 'pa_color' => array('black', 'blue', 'green'), 189 202 'pa_size' => array('xl', '2xxl') 190 203 ); 191 204 192 205 $set_product_attributes = array( 193 206 'pa_color' => array(1, 2, 3), 194 207 'pa_size' => array(4, 5) 195 208 ); 196 209 */ 197 210 198 211 199 212 if (!empty($possible_attributes)) { 200 213 201 214 foreach ($products_ids as $product_id) { 202 215 203 216 $product_id = intval($product_id); 204 217 $product = $this->products->get_product($product_id); 205 218 206 219 //if product is not variable - no variations!! 207 220 if (!$product->is_type('variable')) { 208 221 continue; 209 222 } 210 223 211 224 //attach attributes if they not been attached 212 225 foreach ($set_product_attributes as $field_key => $value) { 213 226 $this->products->set_product_attributes($product_id, $field_key, $value, 'append', array('set_variation')); 214 227 215 228 } 216 229 217 230 //*** 218 231 // Get existing variations so we don't create duplicates. 219 232 $existing_variations = array_map('wc_get_product', $product->get_children()); 220 233 $parent_sku = $product->get_sku(); 221 234 $existing_attributes = array(); 222 235 223 236 foreach ($existing_variations as $existing_variation) { 224 237 if ($existing_variation) { 225 238 $existing_attributes[] = $existing_variation->get_attributes(); 226 239 } 227 240 } 228 241 229 242 230 243 $regular_price = get_post_meta($product_id, '_regular_price', true); 231 244 $sale_price = get_post_meta($product_id, '_sale_price', true); 232 245 //*** 233 246 234 247 foreach ($possible_attributes as $possible_attribute) { 235 248 236 249 if (in_array($possible_attribute, $existing_attributes)) { 237 250 continue; 238 251 } 239 252 240 253 $variation = new WC_Product_Variation(); 241 254 $variation->set_parent_id($product_id); 242 255 $variation->set_attributes($possible_attribute); 243 256 244 257 $variation->set_status(apply_filters('woobe_new_variation_product_status', 'publish')); 245 258 246 259 247 260 if ($regular_price) { 248 261 $variation->set_regular_price($regular_price); 249 262 } 250 263 if ($sale_price) { 251 264 $variation->set_sale_price($sale_price); 252 265 } 253 266 254 267 255 268 //do_action('product_variation_linked', $variation->save()); 256 269 $variation->save(); 257 270 258 271 //to avoid the same SKU as in the parent 259 272 if (empty($parent_sku)) { 260 273 $variation->set_sku('sku-' . $variation->get_id()); 261 274 } else { 262 275 $variation->set_sku($parent_sku . '-' . $variation->get_id()); 263 276 } 264 277 $variation->set_manage_stock(0); 265 278 $variation->set_stock_quantity(0); 266 279 $variation->save(); 267 280 268 281 //clean_post_cache($variation->get_id()); 269 282 //wp_cache_flush(); 270 283 } 271 284 272 285 273 286 //*** 274 287 //set order of variations 275 288 $data_store = $product->get_data_store(); 276 289 $data_store->sort_all_product_variations($product->get_id()); 277 290 } 278 291 } 279 292 } 280 293 281 294 die('done'); 282 295 } 283 296 284 297 //************ TAB 2 285 298 //ajax 286 299 public function woobe_bulkoperations_apply_default_combination() { 287 300 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 301 die('0'); 302 } 288 303 if (!isset($_REQUEST['products_ids'])) { 289 304 die('0'); 290 305 } 291 306 292 307 //*** 293 308 294 309 $products_ids = array_map(function($item) { 295 310 return intval($item); //sanitize intval 296 311 }, $_REQUEST['products_ids']); 297 312 298 313 $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']); 299 314 300 315 if (!empty($combination) AND!empty($products_ids)) { 301 316 foreach ($products_ids as $product_id) { 302 317 $product = $this->products->get_product($product_id); 303 318 $product->set_props(array( 304 319 'default_attributes' => $combination 305 320 )); 306 321 $product->save(); 307 322 308 323 //*** also lets set order of attributes which customer will see on the product page 309 324 $meta = get_post_meta($product_id, '_product_attributes', true); 310 325 311 326 $new_meta = array(); 312 327 $counter = 0; 313 328 foreach (array_keys($combination) as $meta_key) { 314 329 if (isset($meta[$meta_key])) { 315 330 $new_meta[$meta_key] = $meta[$meta_key]; 316 331 $new_meta[$meta_key]['position'] = $counter; 317 332 unset($meta[$meta_key]); 318 333 $counter++; 319 334 } 320 335 } 321 336 322 337 //if we have more attributes than trying to save in order 323 338 if (!empty($meta)) { 324 339 foreach ($meta as $meta_key => $value) { 325 340 $new_meta[$meta_key] = $value; 326 341 $new_meta[$meta_key]['position'] = $counter; 327 342 $counter++; 328 343 } 329 344 } 330 345 331 346 update_post_meta($product_id, '_product_attributes', $new_meta); 332 347 333 348 //woocommerce\includes\admin\meta-boxes\class-wc-meta-box-product-data.php 334 349 //public static function save($post_id, $post) 335 350 do_action('woocommerce_process_product_meta_' . $product->get_type(), $product_id); 336 351 } 337 352 } 338 353 339 354 exit; 340 355 } 341 356 342 357 //************ TAB 3 343 358 //ajax 344 359 public function woobe_bulkoperations_delete() { 345 360 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 361 die('0'); 362 } 346 363 if (!isset($_REQUEST['products_ids'])) { 347 364 die('0'); 348 365 } 349 366 350 367 //*** 351 368 352 369 $removed_ids = array(); 353 370 $products_ids = array_map(function($item) { 354 371 return intval($item); //sanitize intval 355 372 }, $_REQUEST['products_ids']); 356 373 357 374 $combination = array(); 358 375 if (isset($_REQUEST['combination'])) { 359 376 $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']); 360 377 } 361 378 362 379 if (!empty($products_ids)) { 363 380 foreach ($products_ids as $product_id) { 364 381 $product = $this->products->get_product($product_id); 365 382 366 383 if (method_exists($product, 'is_type') AND $product->is_type('variable')) { 367 384 $vars = $product->get_children(); 368 385 369 386 if (!empty($vars)) { 370 387 foreach ($vars as $var_id) { 371 388 372 389 $var = $this->products->get_product($var_id); 373 390 $av = $product->get_available_variation($var); 374 391 375 392 if ($_REQUEST['delete_how'] == 'combo') { 376 393 377 394 if (!empty($combination)) { 378 395 if (count($av['attributes']) === count($combination)) { 379 396 $is = FALSE; 380 397 $attributes = array(); 381 398 382 399 //fix for non-latin symbols 383 400 if (!empty($av['attributes'])) { 384 401 foreach ($av['attributes'] as $k => $v) { 385 402 $attributes[urldecode($k)] = urldecode($v); 386 403 } 387 404 } 388 405 389 406 390 407 foreach ($combination as $comb_key => $comb_value) { 391 408 if (isset($attributes['attribute_' . urldecode($comb_key)]) AND $attributes['attribute_' . urldecode($comb_key)] == urldecode($comb_value)) { 392 409 $is = TRUE; 393 410 } else { 394 411 $is = FALSE; 395 412 break; 396 413 } 397 414 } 398 415 399 416 //removing 400 417 if ($is) { 401 418 $var_prod = $this->products->get_product($av['variation_id']); 402 419 $removed_ids[] = $av['variation_id']; 403 420 $var_prod->delete(true); 404 421 } 405 422 } 406 423 } 407 424 } else { 408 425 //all 409 426 $var_prod = $this->products->get_product($av['variation_id']); 410 427 $removed_ids[] = $av['variation_id']; 411 $var_prod->delete(true); 428 if ($var_prod){ 429 $var_prod->delete(true); 430 } 412 431 } 413 432 } 414 433 } 415 434 } 416 435 417 436 //*** 418 437 } 419 438 } 420 439 421 440 //*** 422 441 $removed_ids = json_encode($removed_ids); 423 442 die($removed_ids); 424 443 } 425 444 426 445 //************ TAB 4 427 446 //ajax 428 447 public function woobe_bulkoperations_get_product_variations() { 448 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 449 die('0'); 450 } 429 451 $product_id = intval($_REQUEST['product_id']); 430 452 $available_variations = array(); 431 453 432 454 if ($product_id > 0) { 433 455 try { 434 456 $product = $this->products->get_product($product_id); 435 457 436 458 if (is_object($product) AND $product->is_type('variable')) { 437 459 $vars = $product->get_children(); 438 460 439 461 if (!empty($vars)) { 440 462 foreach ($vars as $var_id) { 441 463 $var = $this->products->get_product($var_id); 442 464 $av = $product->get_available_variation($var); 443 465 $available_variations[$var_id]['title'] = str_replace($product->get_title(), '', $this->products->generate_product_title($var)); 444 466 $available_variations[$var_id]['attributes'] = $av['attributes']; 445 467 } 446 468 } 447 469 448 470 //print_r($available_variations); 449 471 } 450 472 } catch (Exception $e) { 451 473 //*** 452 474 } 453 475 } 454 476 455 477 die(json_encode($available_variations)); 456 478 } 457 479 458 480 //ajax 459 481 public function woobe_bulkoperations_ordering() { 460 482 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 483 die('0'); 484 } 461 485 $products_ids = array_map(function($item) { 462 486 return intval($item); //sanitize intval 463 487 }, $_REQUEST['products_ids']); 464 488 465 489 $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']); 466 490 467 491 //*** 468 492 469 493 if (!empty($products_ids) AND!empty($combination)) { 470 494 foreach ($products_ids as $product_id) { 471 495 $available_variations = array(); 472 496 $product = $this->products->get_product($product_id); 473 497 $childrens = $product->get_children(); 474 498 475 499 if (!empty($childrens)) { 476 500 foreach ($childrens as $child_id) { 477 501 $variation = $this->products->get_product($child_id); 478 502 $available_variations[] = $product->get_available_variation($variation); 479 503 } 480 504 481 505 //*** 482 506 483 507 if (!empty($available_variations)) { 484 508 foreach ($available_variations as $var) { 485 509 $att = $var['attributes']; 486 510 $num = -1; 487 511 foreach ($combination as $n => $comb_var) { 488 512 //lets look is it the same set of attributes as in $var 489 513 $ak_att = array_keys($att); 490 514 $ak_cv = array_keys($comb_var); 491 515 sort($ak_att); 492 516 sort($ak_cv); 493 517 if ($ak_att === $ak_cv) { 494 518 $av_att = array_values($att); 495 519 $av_cv = array_values($comb_var); 496 520 sort($av_att); 497 521 sort($av_cv); 498 522 if ($av_att === $av_cv) { 499 523 $num = $n; 500 524 break; 501 525 } 502 526 } 503 527 } 504 528 505 529 //*** 506 530 if ($num > -1) { 507 531 $this->products->update_page_field(intval($var['variation_id']), 'menu_order', $num); 508 532 } 509 533 } 510 534 } 511 535 } 512 536 } 513 537 } 514 538 515 539 516 540 exit; 517 541 } 518 542 519 543 //************ TAB 5 swap 520 544 //ajax 521 545 public function woobe_bulkoperations_swap() { 522 546 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 547 die('0'); 548 } 523 549 $do = true; 524 550 if (!empty($_REQUEST['from']) AND!empty($_REQUEST['to'])) { 525 551 if ($_REQUEST['from']['attribute'] == $_REQUEST['to']['attribute']) { 526 552 if ($_REQUEST['from']['term'] == $_REQUEST['to']['term']) { 527 553 $do = false; 528 554 } 529 555 } 530 556 } else { 531 557 $do = false; 532 558 } 533 559 534 560 //*** 535 561 536 562 if ($do) { 537 563 $from_att = sanitize_text_field($_REQUEST['from']['attribute']); 538 564 $to_att = sanitize_text_field($_REQUEST['to']['attribute']); 539 565 540 566 //*** 541 567 542 568 if (!empty($_REQUEST['products_ids'])) { 543 569 foreach ($_REQUEST['products_ids'] as $product_id) { 544 570 $product_id = intval($product_id); 545 571 $product = $this->products->get_product($product_id); 546 572 547 573 //*** 548 574 549 575 if (!in_array($product->get_type(), array('variable', 'variation'))) { 550 576 continue; 551 577 } 552 578 553 579 //*** 554 580 555 581 if ($product->is_type('variable')) { 556 582 $childrens = $product->get_children(); 557 583 $parent_id = $product_id; 558 584 $parent_product = $product; 559 585 } else { 560 586 $childrens = array($product_id); 561 587 $parent_id = $product->get_parent_id(); 562 588 $parent_product = $this->products->get_product($parent_id); 563 589 } 564 590 565 591 566 592 $parent_terms = wc_get_product_terms($parent_id, $to_att, array('fields' => 'slugs')); 567 593 568 594 if (!empty($childrens)) { 569 595 foreach ($childrens as $child_id) { 570 596 $variation = $this->products->get_product($child_id); 571 597 $available_variations = $parent_product->get_available_variation($variation); 572 598 573 599 //*** 574 600 575 601 if (isset($available_variations['attributes']) AND!empty($available_variations['attributes'])) { 576 602 if (isset($available_variations['attributes']['attribute_' . $from_att])) { 577 603 if ($available_variations['attributes']['attribute_' . $from_att] === sanitize_text_field($_REQUEST['from']['term'])) { 578 604 579 605 $possible_attributes = $available_variations['attributes']; 580 606 581 607 if ($from_att !== $to_att) { 582 608 unset($possible_attributes['attribute_' . $from_att]); 583 609 } 584 610 585 611 $possible_attributes['attribute_' . $to_att] = sanitize_text_field($_REQUEST['to']['term']); 586 612 587 613 //if such attribute not selected in the parent product Attributes tab lets attach it here 588 614 if (!in_array($_REQUEST['to']['term'], $parent_terms)) { 589 615 $p_terms = []; 590 616 foreach($parent_terms as $t_slug){ 591 617 $t_term= get_term_by('slug', sanitize_text_field($t_slug), $to_att); 592 618 if($t_term){ 593 619 $p_terms[] = $t_term->term_id; 594 620 } 595 621 596 622 } 597 623 $t = get_term_by('slug', sanitize_text_field($_REQUEST['to']['term']), $to_att); 598 624 $p_terms[] = $t->term_id; 599 625 600 626 $this->products->update_page_field($parent_id, $to_att, $p_terms); 601 627 } 602 628 603 629 $variation->set_attributes($possible_attributes); 604 630 $variation->save(); 605 631 } 606 632 } 607 633 } 608 634 } 609 635 } 610 636 } 611 637 } 612 638 } 613 639 614 640 die('done'); 615 641 } 616 642 617 643 //************ TAB 6 attaching 618 644 //ajax 619 645 public function woobe_bulkoperations_attaching() { 620 646 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 647 die('0'); 648 } 621 649 $selected_attribute = sanitize_text_field($_REQUEST['selected_attribute']); 622 650 $attaching_att = $_REQUEST['attaching_att']; //sanitizing in cycle 623 651 $products_ids = $_REQUEST['products_ids']; //sanitizing in cycle 624 652 //*** 625 653 626 654 if (!empty($products_ids)) { 627 655 if (!empty($selected_attribute) AND $attaching_att) { 628 656 foreach ($products_ids as $product_id) { 629 657 $product_id = intval($product_id); //sanitizing 630 658 631 659 $product = $this->products->get_product($product_id); 632 660 633 661 if ($product->is_type('variable')) { 634 662 $childrens = $product->get_children(); 635 663 636 664 //*** 637 665 if (!empty($childrens)) { 638 666 foreach ($childrens as $child_id) { 639 667 $variation = $this->products->get_product($child_id); 640 668 $available_variations = $product->get_available_variation($variation); 641 669 642 670 //*** 643 671 644 672 if (isset($available_variations['attributes']) AND!empty($available_variations['attributes'])) { 645 673 646 674 //if its empty - will be filled up, if not empty will be replaced if not selected 'ignore' 647 675 $possible_attributes = $available_variations['attributes']; 648 676 649 677 //*** 650 678 651 679 foreach ($attaching_att as $set) { 652 680 653 681 if (isset($possible_attributes['attribute_' . $selected_attribute])) { 654 682 unset($possible_attributes['attribute_' . $selected_attribute]); 655 683 } 656 684 657 685 if (isset($set['attributes']['attribute_' . $selected_attribute])) { 658 686 unset($set['attributes']['attribute_' . $selected_attribute]); 659 687 } 660 688 661 689 //*** 662 690 663 691 $att_set_now_keys = array_keys($possible_attributes); 664 692 $set_keys = array_keys($set['attributes']); 665 693 sort($att_set_now_keys); 666 694 sort($set_keys); 667 695 668 696 if ($att_set_now_keys === $set_keys) { 669 697 $att_set_now_vals = array_values($possible_attributes); 670 698 $set_vals = array_values($set['attributes']); 671 699 sort($att_set_now_vals); 672 700 sort($set_vals); 673 701 if ($att_set_now_vals === $set_vals AND $set['value'] !== 'woobe-ignore') { 674 702 $possible_attributes['attribute_' . $selected_attribute] = sanitize_text_field($set['value']); 675 703 676 704 //if such attribute not selected in the parent product Attributes tab lets attach it here 677 705 $parent_terms = wc_get_product_terms($product_id, $selected_attribute, array('fields' => 'ids')); 678 706 if (!in_array($set['value'], $parent_terms)) { 679 707 $t = get_term_by('slug', sanitize_text_field($set['value']), $selected_attribute); 680 708 $parent_terms[] = $t->term_id; 681 709 $this->products->update_page_field($product_id, $selected_attribute, $parent_terms, '', array('set_variation')); 682 710 683 711 } 684 712 685 713 686 714 $variation->set_attributes($possible_attributes); 687 715 $variation->save(); 688 716 break; 689 717 } 690 718 } 691 719 } 692 720 } 693 721 } 694 722 } 695 723 696 724 //fix visibility 697 725 $meta = get_post_meta($product_id, '_product_attributes', true); 698 726 if (is_array($meta)) { 699 727 foreach ($meta as $pa_key => $item) { 700 728 if ($item['name'] == 'pa_size') { 701 729 $meta[$pa_key]['is_visible'] = 1; 702 730 $meta[$pa_key]['is_variation'] = 1; 703 731 } 704 732 } 705 733 } 706 734 update_post_meta($product_id, '_product_attributes', $meta); 707 735 } 708 736 } 709 737 } 710 738 } 711 739 712 740 //*** 713 741 714 742 die('done'); 715 743 } 716 744 717 745 //************ TAB 7 visibility 718 746 //ajax 719 747 public function woobe_bulkoperations_visibility() { 720 748 if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) { 749 die('0'); 750 } 721 751 if (isset($_REQUEST['products_ids'])) { 722 752 $products_ids = $_REQUEST['products_ids']; //sanitizing in cycle 723 753 $vis_data = $_REQUEST['vis_data']; //sanitizing in cycle 724 754 725 755 if (!empty($products_ids) AND!empty($vis_data)) { 726 756 foreach ($products_ids as $product_id) { 727 757 $product_id = intval($product_id); 728 758 729 759 $product = $this->products->get_product($product_id); 730 760 if ($product->is_type('variable')) { 731 761 $meta = get_post_meta($product_id, '_product_attributes', true); 732 762 733 763 734 764 foreach ($vis_data as $vis) { 735 765 if (is_array($meta)) { 736 766 foreach ($meta as $pa_key => $item) { 737 767 if ($item['name'] == $vis['attribute']) { 738 768 $meta[$pa_key]['is_visible'] = intval($vis['is_visible']); 739 769 $meta[$pa_key]['is_variation'] = intval($vis['is_variation']); 740 770 } 741 771 } 742 772 } 743 773 } 744 774 update_post_meta($product_id, '_product_attributes', $meta); 745 775 } 746 776 } 747 777 } 748 778 } 749 779 750 780 die('done'); 751 781 } 752 782 753 783 }
Note: See TracChangeset
for help on using the changeset viewer.