Advertisement
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) {
- // Get the product ID & size
- $product_id = $data['manufacturer_artno'];
- $product_size = $data['size'];
- $sizes = (array) get_transient('bis_sizes');
- $sizes[$product_id][] = $product_size;
- set_transient('bis_sizes', $sizes);
- return $continue_import;
- }
- add_filter('wp_all_import_is_post_to_update', 'bis_get_sizes', 10, 4);
- /**
- * Reset transient
- */
- function bis_reset_sizes() {
- set_transient('bis_sizes', '', 60*60);
- }
- add_action('pmxi_before_xml_import', 'bis_reset_sizes');
- /**
- * Save sizes as product attributes
- */
- function bis_save_sizes() {
- $all_sizes = (array) get_transient('bis_sizes');
- $taxonomy = 'pa_size';
- // Remove empty items
- $all_sizes = array_filter($all_sizes);
- if(!empty($all_sizes)) {
- foreach($all_sizes as $sku => $product_sizes) {
- $product_id = wc_get_product_id_by_sku($sku);
- $product_terms = array();
- // Add product sizes again
- 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($product_id, $product_terms, $taxonomy);
- }
- }
- }
- add_action('after_xml_import', 'bis_save_sizes');
- add_action('init', function() {
- if(isset($_GET['test'])) {
- bis_save_sizes();
- die();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement