我已经在functions.php上有了此代码
该代码将在每个产品变体上添加一个新字段,以具有日期时间输入字段,因此,当当前日期超过输入日期时,变体将自动过期.
// 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的每个变体上添加一个新字段.
我想做的是获取每个“ _text_field_date_expire”元,以便为每个产品删除过期的版本.
我如何在functions.php上获取每个woocommerce变体ID,以便可以添加一条规则,即当它已经超过当前日期时,它将过期?
var_dump(get_post_meta( $variation_id , '_text_field_date_expire', true ));
这段代码是我将用来获取datetime字段的代码,但是不确定如何获取每个$variation_id字段吗?
谢谢!
解决方法:
试试这个:
$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 );
}
希望这对您有帮助.欲获得更多信息:
> How To Get Product Variations & Meta – WooCommerce
> How to get the Variation ID in a Woocommerce product