Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get products sizes
- */
- function bis_get_sizes($continue_import, $post_id, $data, $import_id) {
- $transient_name = "bis_size_{$import_id}";
- // Get the product ID & size
- $product_id = $data['manufacturer_artno']; // A field that will be used for SKU
- $product_size = $data['size'];
- $sizes = (array) get_transient($transient_name);
- $sizes[$product_id][] = $product_size;
- set_transient($transient_name, $sizes);
- return $continue_import;
- }
- add_filter('wp_all_import_is_post_to_update', 'bis_get_sizes', 10, 4);
- /**
- * Save sizes as product attributes
- */
- function bis_save_sizes($import_id) {
- global $wpdb;
- $transient_name = "bis_size_{$import_id}";
- $all_sizes = (array) get_transient($transient_name);
- $taxonomy = 'pa_size';
- // Get products IDs from last import
- $product_ids = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}pmxi_posts WHERE import_id = {$import_id}");
- // Remove empty items from the array with sizes
- $all_sizes = array_filter($all_sizes);
- if(!empty($all_sizes)) {
- foreach($all_sizes as $sku => $product_sizes) {
- // Find the product with exact SKU in the imported products list
- $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));
- // Add product sizes again to the product
- if(!empty($product_id)) {
- $product_terms = array();
- foreach($product_sizes as $product_size) {
- $term = term_exists((string) $product_size, $taxonomy);
- if(empty($term)) {
- $term = wp_insert_term((string) $product_size, $taxonomy);
- }
- $product_terms[] = (int) $term['term_id'];
- }
- wp_set_post_terms((int) $product_id, $product_terms, $taxonomy);
- }
- }
- }
- }
- add_action('after_xml_import', 'bis_save_sizes');
- add_action('init', function() {
- if(isset($_GET['test']) && is_numeric($_GET['test'])) {
- $import_id = (int) $_GET['test'];
- bis_save_sizes($import_id);
- $transient_name = "bis_size_{$import_id}";
- $all_sizes = get_transient($transient_name);
- print_r($all_sizes);
- die();
- }
- });
Add Comment
Please, Sign In to add comment