如何获取 Woocommerce 变体 ID? [英] How to get Woocommerce Variation ID?

查看:24
本文介绍了如何获取 Woocommerce 变体 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在functions.php 上有了这段代码

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 automatically expired when the current date is past the input date.

// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

//Create New fields for Variations
function variation_settings_fields( $loop, $variation_data, $variation ) {
    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field_date_expire[' . $variation->ID . ']',
            'class'       => '_text_field_date_expire',
            'type'        => 'datetime-local',  
            'label'       => __( 'Date of Expiration', 'woocommerce' ), 
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Expiration of Each Product Variation', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field_date_expire', true )
        )
    );
}


//Save New Fields for Variation
function save_variation_settings_fields( $post_id ) {
    // Text Field
    $text_field = $_POST['_text_field_date_expire'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field_date_expire', esc_attr( $text_field ) );
    }
}

这是在 Wocommerce 的每个变体上添加一个新字段.

This is to add a new field on each variation of Wocommerce.

我想要做的是获取每个_text_field_date_expire"元数据,以便我可以删除每个产品的过期变体..

What I am trying to do is to get each "_text_field_date_expire" meta so I could delete expired variation for each of the products..

如何在functions.php 上获取每个woocommerce 变体ID,以便我可以添加一个规则,当它现在已经超过当前日期时它会过期?

How do I get each woocommerce variation ID on functions.php so I could add a rule that it will expire when it is already past the current date right now?

var_dump(get_post_meta( $variation_id , '_text_field_date_expire', true ));

此代码是我将用于获取日期时间字段的代码,但不确定如何获取每个 $variation_id 字段?

This code is the one that I will use to get the datetime field, but not sure how I could get each $variation_id field?

谢谢!

推荐答案

试试这个:

$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:

这篇关于如何获取 Woocommerce 变体 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆