Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 469 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get Woocommerce Variation ID?

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

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

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..

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

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

#2
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
](

[To see links please register here]

)
* [How to get the Variation ID in a Woocommerce product](

[To see links please register here]

)
Reply

#3
In WooCommerce 3+, it's **`$variation->get_id()`** from `$variation` function argument, which is an instance of the [`WC_Product_Variation`][1].

The method `get_id()` is inherited from [`WC_Data`][2] class.

So in your code it should be instead:

'id' => '_text_field_date_expire[' . $variation->get_id() . ']',

> Since WooCommerce 3, All `WC_Product` properties can't be accessed directly. Instead, you need to use the available methods.

----

Also in your hooked function `save_variation_settings_fields()` you are declaring 2 arguments, so **there is one missing**. It should be:

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

function save_variation_settings_fields( $variation_id, $i ) {
// Text Field
$text_field = $_POST['_text_field_date_expire'][ $variation_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $variation_id, '_text_field_date_expire', esc_attr( $text_field ) );
}
}

See [the source code for **`woocommerce_save_product_variation`** action hook][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through