• Hi there

    Since updating to 2.2 one of my plugins no longer works. I’ve pasted the problem function below. I’ve changed it slightly with no success – I used to just declare ‘global $product;’, i’ve changed that to $product = new WC_Product($theid);..

    Any suggestions?

    // ********* Get all products and variation and sort alphabetically, return in array (title, sku, id)*******
    function get_alphabetic_product_list() {
    
        $full_product_list = array();
    
        $loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) );
        while ( $loop->have_posts() ) : $loop->the_post();
    
            $theid = get_the_ID();
           // global $woocommerce, $post, $product;
    
            $product = new WC_Product($theid);
    
            // it's a variable product..
           if( $product->is_type( 'variable' ) ){
    
                $available_variations = $product->get_available_variations();
                $attributes = $product->get_attributes();
    
                foreach ($available_variations as $prod_variation) :
                    $post_id = $prod_variation['variation_id'];
                    $post_object = get_post($post_id);
                    $sku = $prod_variation['sku'];
                    $full_product_list[] = array(get_the_title(),$sku, $post_id);
                endforeach;
    
            } else {
    
                // it's a standard product...
                $sku = $product->get_sku();
    
                $full_product_list[] = array(get_the_title(), $sku, $theid);
            }
    
        endwhile; wp_reset_query();
    
        sort($full_product_list);
    
        return $full_product_list;
    }

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter mokummusic

    (@mokummusic)

    It was actually pretty simple once I thought about it..

    // ********* Get all products and variations and sort alphbetically, return in array (title, sku, id)*******
    function get_alphabetic_product_list() {
    
        $full_product_list = array();
    
        $loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
        while ( $loop->have_posts() ) : $loop->the_post();
    
            $theid = get_the_ID();
            $product = new WC_Product($theid);
            $sku = $product->get_sku();
    
             if( get_post_type() == 'product_variation' ){
                   $thetitle = get_the_title( wp_get_post_parent_id($theid ));
              } else {
                   $thetitle = get_the_title();
             }
             if (!empty($sku)) $full_product_list[] = array($thetitle, $sku, $theid); //don't add the parent of product variations
        endwhile; wp_reset_query();
    
        sort($full_product_list);
    
        return $full_product_list;
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Changes to variable products in 2.2’ is closed to new replies.