How to get the Variation ID in a Woocommerce product

I’m trying to get in a plugin I’m writing the variation ID of products. Here’s what I wrote:

class mass {

    public function __construct()
    {
        add_action('woocommerce_product_after_variable_attributes',array($this,'thfo_mass'));


    }
public function thfo_mass()
    {
        $id = WC_Product_Variation::get_variation_id();
        //$lenght = get_post_meta($id,'_length');
        //$dimensions = wc_get_dimension(24750, 'cm');
        var_dump($id);
    }

I only get an error:

Read More

Deprecated: Non-static method WC_Product_Variation::get_variation_id() should not be called statically, assuming $this from incompatible context in path/to/plugins/thfo-raw-material-for-woocommerce/class/mass.php on line 19

Notice: Undefined property: mass::$variation_id in path/to/wp-content/plugins/woocommerce/includes/class-wc-product-variation.php on line 257
int(0)

Related posts

4 comments

  1. try this.

     <?php 
        $product_obj = new WC_Product_Factory();
        $product = $product_obj->get_product($product);                             
        if ($product->product_type == 'variable'):
            $children   = $product->get_children( $args = '', $output = OBJECT ); 
            foreach ($children as $key=>$value) {
                $product_variatons = new WC_Product_Variation($value);
                if ( $product_variatons->exists() && $product_variatons->variation_is_visible() ) {
                    $variations[$value] = $product_variatons->get_variation_attributes();
                }
            }
       endif;
    
  2. get_variation_id is not a static method at all.

    You need to create an instance/object of WC_Product_Variation, then you can use it.

    Like —

    $variable_product = new WC_Product_Variation($value);
    $variable_product->get_variation_id();
    
  3. Please check this result is here: How to get Woocommerce Variation ID?

    $args = array(
        'post_type'     => 'product_variation',
        'post_status'   => array( 'private', 'publish' ),
        'numberposts'   => -1,
        'orderby'       => 'menu_order',
        'order'         => 'asc',
        'post_parent'   => get_the_ID() // get parent post-ID
    );
    $variations = get_posts( $args );
    
    foreach ( $variations as $variation ) {
    
        // get variation ID
        $variation_ID = $variation->ID;
    
        // get variations meta
        $product_variation = new WC_Product_Variation( $variation_ID );
    
        // get variation featured image
        $variation_image = $product_variation->get_image();
    
        // get variation price
        $variation_price = $product_variation->get_price_html();
    
        get_post_meta( $variation_ID , '_text_field_date_expire', true );
    
    }
    
  4. Here’s what I’ve managed to figure out so far:

    Firstly, to get the colour:

    $colour = isset( $_REQUEST[ 'attribute_' . sanitize_title( 'pa_colour' ) ] ) ? wc_clean( stripslashes( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( 'pa_colour' ) ] ) ) ) : $product->get_variation_default_attribute( 'pa_colour' );
    

    Then in my foreach loop that is generating the checkboxes:

    $variation_id = searchForVariants($colour, $size, $available_variations);
    $product_shipping_classes = get_the_terms( $variation_id, 'product_shipping_class' );
    $product_shipping_class_name = ( $product_shipping_classes && ! is_wp_error( $product_shipping_classes ) ) ? current( $product_shipping_classes )->name : '';
    

    Then you can use current( $product_shipping_classes )->term_id or current( $product_shipping_classes )->slug or current( $product_shipping_classes )->name.

    The searchForVariants function is in my functions.php file:

    function searchForVariants($colour, $size, $array) {
    
        foreach ($array as $key => $sub_array) {
    
            if ($sub_array['attributes']['attribute_pa_colour'] === $colour && $sub_array['attributes']['attribute_pa_size'] === $size) {
               return $sub_array['variation_id'];
            } else {
                continue;
            }
        }
    }
    

    The $available_variations array is already in the storefront theme in the variable.php but I am using a child theme so copied the template file to make these changes.

Comments are closed.