How to get the Variation ID in a Woocommerce product

为君一笑 提交于 2019-12-07 04:42:31

问题


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:

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)


回答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:


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.



来源:https://stackoverflow.com/questions/32916601/how-to-get-the-variation-id-in-a-woocommerce-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!