编程语言
首页 > 编程语言> > php – 如何获取Woocommerce产品中的Variation ID

php – 如何获取Woocommerce产品中的Variation ID

作者:互联网

我正在尝试插入一个插件,我正在编写产品的变体ID.这是我写的:

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);
    }

我只收到一个错误:

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)

解决方法:

试试这个.

 <?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;

标签:wordpress,php,woocommerce,variation
来源: https://codeday.me/bug/20190516/1116801.html