mbis

Sync product sizes

Nov 7th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Get products sizes
  3.  */
  4. function bis_get_sizes($continue_import, $post_id, $data, $import_id) {
  5.     $transient_name = "bis_size_{$import_id}";
  6.    
  7.     // Get the product ID & size
  8.     $product_id = $data['manufacturer_artno']; // A field that will be used for SKU
  9.     $product_size = $data['size'];
  10.    
  11.     $sizes = (array) get_transient($transient_name);
  12.     $sizes[$product_id][] = $product_size;
  13.     set_transient($transient_name, $sizes);
  14.  
  15.     return $continue_import;
  16. }
  17. add_filter('wp_all_import_is_post_to_update', 'bis_get_sizes', 10, 4);
  18.  
  19. /**
  20.  * Save sizes as product attributes
  21.  */
  22. function bis_save_sizes($import_id) {
  23.     global $wpdb;
  24.    
  25.     $transient_name = "bis_size_{$import_id}";
  26.     $all_sizes = (array) get_transient($transient_name);
  27.     $taxonomy = 'pa_size';
  28.    
  29.     // Get products IDs from last import
  30.     $product_ids = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}pmxi_posts WHERE import_id = {$import_id}");
  31.    
  32.     // Remove empty items from the array with sizes
  33.     $all_sizes = array_filter($all_sizes);
  34.    
  35.     if(!empty($all_sizes)) {
  36.         foreach($all_sizes as $sku => $product_sizes) {        
  37.             // Find the product with exact SKU in the imported products list
  38.             $product_id = $wpdb->get_var(sprintf("SELECT p.ID FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON (p.ID = pm.post_ID) WHERE p.ID IN (%s) AND pm.meta_key = '_sku' AND pm.meta_value = '%s'", implode(", ", $product_ids), $sku));
  39.            
  40.             // Add product sizes again to the product
  41.             if(!empty($product_id)) {
  42.                 $product_terms = array();
  43.                
  44.                 foreach($product_sizes as $product_size) {
  45.                     $term = term_exists((string) $product_size, $taxonomy);
  46.  
  47.                     if(empty($term)) {
  48.                         $term = wp_insert_term((string) $product_size, $taxonomy);
  49.                     }
  50.  
  51.                     $product_terms[] = (int) $term['term_id'];
  52.                 }
  53.  
  54.                 wp_set_post_terms((int) $product_id, $product_terms, $taxonomy);
  55.             }
  56.         }
  57.     }
  58. }
  59. add_action('after_xml_import', 'bis_save_sizes');
  60.  
  61. add_action('init', function() {
  62.     if(isset($_GET['test']) && is_numeric($_GET['test'])) {
  63.         $import_id = (int) $_GET['test'];
  64.        
  65.         bis_save_sizes($import_id);
  66.        
  67.         $transient_name = "bis_size_{$import_id}";
  68.         $all_sizes = get_transient($transient_name);
  69.         print_r($all_sizes);
  70.         die();
  71.     }
  72. });
Add Comment
Please, Sign In to add comment