Advertisement
Guest User

Combine product sizes

a guest
Nov 7th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. /**
  2.  * Get products sizes
  3.  */
  4. function bis_get_sizes($continue_import, $post_id, $data, $import_id) {
  5.     // Get the product ID & size
  6.     $product_id = $data['manufacturer_artno'];
  7.     $product_size = $data['size'];
  8.    
  9.     $sizes = (array) get_transient('bis_sizes');
  10.     $sizes[$product_id][] = $product_size;
  11.     set_transient('bis_sizes', $sizes);
  12.  
  13.     return $continue_import;
  14. }
  15. add_filter('wp_all_import_is_post_to_update', 'bis_get_sizes', 10, 4);
  16.  
  17. /**
  18.  * Reset transient
  19.  */
  20. function bis_reset_sizes() {
  21.     set_transient('bis_sizes', '', 60*60);
  22. }
  23. add_action('pmxi_before_xml_import', 'bis_reset_sizes');
  24.  
  25. /**
  26.  * Save sizes as product attributes
  27.  */
  28. function bis_save_sizes() {
  29.     $all_sizes = (array) get_transient('bis_sizes');
  30.     $taxonomy = 'pa_size';
  31.    
  32.     // Remove empty items
  33.     $all_sizes = array_filter($all_sizes);
  34.    
  35.     if(!empty($all_sizes)) {
  36.         foreach($all_sizes as $sku => $product_sizes) {
  37.             $product_id = wc_get_product_id_by_sku($sku);
  38.             $product_terms = array();
  39.            
  40.             // Add product sizes again
  41.             foreach($product_sizes as $product_size) {
  42.                 $term = term_exists((string) $product_size, $taxonomy);
  43.                
  44.                 if(empty($term)) {
  45.                     $term = wp_insert_term((string) $product_size, $taxonomy);
  46.                 }
  47.                
  48.                 $product_terms[] = (int) $term['term_id'];
  49.             }
  50.            
  51.             wp_set_post_terms($product_id, $product_terms, $taxonomy);
  52.         }
  53.     }
  54. }
  55. add_action('after_xml_import', 'bis_save_sizes');
  56.  
  57. add_action('init', function() {
  58.     if(isset($_GET['test'])) {
  59.         bis_save_sizes();
  60.         die();
  61.     }
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement