How to get Woocommerce Variation ID?

后端 未结 2 562
暗喜
暗喜 2020-12-11 07:06

I\'ve already had this code on functions.php

This code is to add a new field on each product variation to have datetime input field, so variations would automaticall

2条回答
  •  [愿得一人]
    2020-12-11 07:28

    Try this one:

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

    Hope this will helps you. For more information:

    • How To Get Product Variations & Meta – WooCommerce
    • How to get the Variation ID in a Woocommerce product

提交回复
热议问题