View difference between Paste ID: 9GFawPAN and qmkHnqtv
SHOW: | | - or go back to the newest paste.
1
/**
2
 * Get products sizes
3
 */
4-
function bis_get_sizes($continue_import, $post_id, $data, $import_id) {	
4+
function bis_get_sizes($continue_import, $post_id, $data, $import_id) {
5-
	// Get the product ID & size
5+
	$transient_name = "bis_size_{$import_id}";
6-
	$product_id = $data['manufacturer_artno'];
6+
7-
	$product_size = $data['size'];
7+
    // Get the product ID & size
8
    $product_id = $data['manufacturer_artno']; // A field that will be used for SKU
9-
	$sizes = (array) get_transient('bis_sizes');
9+
    $product_size = $data['size'];
10-
	$sizes[$product_id][] = $product_size;
10+
   
11-
	set_transient('bis_sizes', $sizes);
11+
    $sizes = (array) get_transient($transient_name);
12
    $sizes[$product_id][] = $product_size;
13-
	return $continue_import;
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-
 * Reset transient
18+
 
19
/**
20-
function bis_reset_sizes() {
20+
21-
	set_transient('bis_sizes', '', 60*60);
21+
22
function bis_save_sizes($import_id) {
23-
add_action('pmxi_before_xml_import', 'bis_reset_sizes');
23+
	global $wpdb;
24
	
25
	$transient_name = "bis_size_{$import_id}"; 
26
    $all_sizes = (array) get_transient($transient_name);
27
    $taxonomy = 'pa_size';
28-
function bis_save_sizes() {
28+
29-
	$all_sizes = (array) get_transient('bis_sizes');
29+
	// Get products IDs from last import
30-
	$taxonomy = 'pa_size';
30+
	$product_ids = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}pmxi_posts WHERE import_id = {$import_id}");
31
   
32-
	// Remove empty items
32+
    // Remove empty items from the array with sizes
33-
	$all_sizes = array_filter($all_sizes);
33+
    $all_sizes = array_filter($all_sizes);
34
   
35-
	if(!empty($all_sizes)) {
35+
    if(!empty($all_sizes)) {
36-
		foreach($all_sizes as $sku => $product_sizes) {
36+
        foreach($all_sizes as $sku => $product_sizes) {			
37-
			$product_id = wc_get_product_id_by_sku($sku);
37+
			// Find the product with exact SKU in the imported products list
38-
			$product_terms = array();
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-
			
39+
           
40-
			// Add product sizes again
40+
            // Add product sizes again to the product
41-
			foreach($product_sizes as $product_size) {
41+
			if(!empty($product_id)) {
42-
				$term = term_exists((string) $product_size, $taxonomy);
42+
				$product_terms = array();
43
				
44-
				if(empty($term)) {
44+
				foreach($product_sizes as $product_size) {
45-
					$term = wp_insert_term((string) $product_size, $taxonomy);
45+
					$term = term_exists((string) $product_size, $taxonomy);
46
47
					if(empty($term)) {
48-
				$product_terms[] = (int) $term['term_id'];
48+
						$term = wp_insert_term((string) $product_size, $taxonomy);
49
					}
50-
			
50+
51-
			wp_set_post_terms($product_id, $product_terms, $taxonomy);
51+
					$product_terms[] = (int) $term['term_id'];
52-
		}
52+
53-
	}
53+
54
				wp_set_post_terms((int) $product_id, $product_terms, $taxonomy);
55
			}
56
        }
57
    }
58-
	if(isset($_GET['test'])) {
58+
59-
		bis_save_sizes();
59+
60-
		die();
60+
 
61-
	}
61+
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
});