如何在Woocommerce产品中获取变体ID [英] How to get the Variation ID in a Woocommerce product
问题描述
我试图插入一个插件,我正在写产品的版本ID.这是我写的:
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);
}
我只得到一个错误:
不建议使用:假定从$ path/to/plugins/thof-raw-material-for-woocommerce/class/mass.php中的不兼容上下文中假定$ this,则不应静态调用非静态方法WC_Product_Variation :: get_variation_id().第19行
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
通知:未定义的属性:mass :: $ variation_id在第257行的path/to/wp-content/plugins/woocommerce/includes/class-wc-product-variation.php中 int(0)
Notice: Undefined property: mass::$variation_id in path/to/wp-content/plugins/woocommerce/includes/class-wc-product-variation.php on line 257 int(0)
推荐答案
尝试一下.
<?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;
这篇关于如何在Woocommerce产品中获取变体ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!