How to Get Woocommerce Variation Id

How to get Woocommerce Variation ID?

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

how to get variation id by attributes in woocommerce

I solved the problem this way
I created a function that with the product ID and country attribute ID pulls out the attributes of its charge amount and is called with Ajax.
And then with another function I send the price and information about the amount of charge and finally it is OK

my cods:

<?php

foreach ($attributes as $key => $value):
$attribute_name_ = preg_replace('/pa_/', '', $key); // GET ATTRIBUTE NAME

?>

<div class="<?= ($attribute_name_ == 'regions') ? 'region' : 'charge'; ?>">

<?php $attribute_name = wc_get_product_terms(get_the_ID(), $key);

$attribute_slug = wc_get_product_terms(get_the_ID(), $key, array('fields' => 'slugs')); // GET ATTRIBUTE SLUG
$attribute_name = wc_get_product_terms(get_the_ID(), $key); // GET ATTRIBUTE SLUG
$attribute_thumbnail_id = wc_get_product_terms(get_the_ID(), $key, array('fields' => 'term_id')); // GET ATTRIBUTE SLUG

for ($i = 0; $i < count($attribute_name); $i++): // array_slice BECAUSE ARRAY INDEX IS NOT SEQUENCIAL

$slug = array_slice($attribute_slug, $i, 1);

if ($attribute_name_ == 'regions') {

$thumb_id = get_term_meta($attribute_name[$i]->term_id);

$thumb_id_ = get_term_meta($attribute_name[$i]->term_id, 'product_attribute_image', true);

$img_src = wp_get_attachment_thumb_url($thumb_id_);

?>
<div id="<?= $slug['0'] ?>"
class="region-item">

<img src="<?= $img_src ?>"
alt="">
<span>
<?php

$name = array_slice($attribute_name, $i, 1);
echo $name[0]->name;

?>
</span>
</div>
<?php

}
?>

<?php endfor ?>
</div>
<?php endforeach ?>

<div class="buy-btn">
<div class="price-section">
<div class="price-lable">
price :
</div>
<div class="price">

</div>
</div>

<a class="add-to-cart" value="">add to cart</a>
</div>

my functions :

function get_varible_data()
{

$producit_id = $_POST['p_id'];
$region_id = $_POST['region_id'];

$product = wc_get_product($producit_id);

$attributes = $product->get_attributes();
$get_variation_attributes = $product->get_variation_attributes();
$available_variations = $product->get_available_variations();

foreach ($available_variations as $available_variation) {
$regions = $available_variation['attributes']['attribute_pa_regions'];
$varible_id = $available_variation['variation_id'];
$charge = $available_variation['attributes']['attribute_pa_charge-amount'];
$display_price = $available_variation['display_price'];

$pa_charge_amount_ = 'pa_charge-amount';
$pa_charge_amount_meta = get_post_meta($available_variation['variation_id'], 'attribute_' . $pa_charge_amount_, true);
$pa_charge_amount_term = get_term_by('slug', $pa_charge_amount_meta, $pa_charge_amount_);

if ($region_id === $regions) {

$vari_g[] = array(
'group' => $region_id,
'price' => $display_price,
'slug' => $charge,
'id' => $varible_id,
'name' => $pa_charge_amount_term->name,
);

$html = '<div id="' . $varible_id . '" class="charge-item">' . $pa_charge_amount_term->name . '</div>';

echo $html;
}
}

wp_die();

}

add_action("wp_ajax_get_varible_data", "get_varible_data");
add_action("wp_ajax_nopriv_get_varible_data", "get_varible_data");

function get_varible_ch_data()
{

$variation_id = $_POST['charge_id'];
$price = get_post_meta($variation_id, '_price', true);

echo $price;

wp_die();

}

add_action("wp_ajax_get_varible_ch_data", "get_varible_ch_data");
add_action("wp_ajax_nopriv_get_varible_ch_data", "get_varible_ch_data");

and my ajax :

<script>

function request_ajax_call_varible(metodth, id) {

$('.card-buy').css('cursor', 'wait');
var hastag = '#';
var ajaxurl = "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php";

if (metodth === 'get_var_id') {

jQuery.ajax({

type: "POST",
url: ajaxurl,
dataType: 'html',
data: {
action: "get_varible_data",
p_id: <?= get_the_id() ?>,
region_id: id,

},

success: function (response) {

jQuery(hastag + id).addClass('active');
jQuery('.charge').html(response);

$('.card-buy').css('cursor', 'default');

}
})
}

if (metodth === 'get_price') {

var before_link = '<?php bloginfo('url'); ?>/cart/?add-to-cart=';
var link = before_link + id;
jQuery.ajax({

type: "POST",
url: ajaxurl,
dataType: 'html',
data: {
action: "get_varible_ch_data",
charge_id: id,
},

success: function (response) {

jQuery('.price').html(toFarsiNumber(numberWithCommas(response)) + '<span class="price-symbole">تومان </span>');

jQuery(hastag + id).addClass('active');
jQuery("a.add-to-cart").attr("href", link)
$('.card-buy').css('cursor', 'default');

}
})
}
}

jQuery('.region-item').click(function (e) {

$('div.region-item').removeClass('active');
request_ajax_call_varible('get_var_id', jQuery(this).attr('id'))

});

$('div.charge').on('click', 'div.charge-item', function () {

$('div.charge-item').removeClass('active');
request_ajax_call_varible('get_price', jQuery(this).attr('id'))

})

function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function toFarsiNumber(n) {
const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

return n
.toString()
.replace(/\d/g, x => farsiDigits[x]);
}

var first_item = $($('.region-item')[0]);
first_item.ready(function () {

first_item.addClass('active');
request_ajax_call_varible('get_var_id', first_item.attr('id'))
});

$(document).ready(function () {
setTimeout(function () {
var first_cha = $($('.charge-item')[0]);
first_cha.ready(function () {

first_cha.addClass('active');

request_ajax_call_varible('get_price', first_cha.attr('id'))
});
}, 1000);
})

</script>

I hope this method works for you.
If you have another method, please tell me
thank you

Get variations IDs from a variable product in Woocommerce 3

To get the children variation Ids for a variable product, use WC_product get_children() method (that doesn't have/allow any arguments):

// (if needed) Get an instance of the WC_product object (from a dynamic product ID)
$product = wc_get_product($product_id);

// Get children product variation IDs in an array
$children_ids = $product->get_children();

// Get the first ID value
$children_id = reset($children_ids);
// or
$children_id = $children_ids[0];

Tested and works.

Woocommerce - get attribute value by variation ID

  • As is often the case, there are different ways, this is one of them.
  • Note that the use of a global variable is not necessarily needed, because you have access to the $product_id you can get the product object via wc_get_product
function custom_product_name( $text, $product_id, $vars_id ) {
// Get WC_Product_Variation Object
$variation = wc_get_product( $vars_id );

// Is a WC_Product
if ( is_a( $variation, 'WC_Product' ) ) {
// Variation name
$variation_name = implode( ' / ', $variation->get_variation_attributes() );

// Output
$text = 'Name: ' . $variation_name;
}

return $text;
}
add_filter( 'prefix_xml_feeds_productname_variant', 'custom_product_name', 10, 3 );

How to get Variation ID of Variable Product in WooCommerce?

You can simply use $product->get_id();

add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );

function custom_price( $price, $product )
{
$variation_id = $product->get_id();

return get_post_meta( $variation_id, 'custom_field', true );
}

Get product variation id by their product attributes slug values pairs in Woocommerce

The following function uses a very light SQL query to get the variation ID for from a variable product for defined product attributes "color" and "size" term slugs:

function get_product_variation_id( $size, $color, $product_id = 0 ) {
global $wpdb;

if ( $product_id == 0 )
$product_id = get_the_id();

return $wpdb->get_var( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
WHERE pm.meta_key = 'attribute_pa_color'
AND pm.meta_value LIKE '$color'
AND pm2.meta_key = 'attribute_pa_size'
AND pm2.meta_value LIKE '$size'
AND p.post_parent = $product_id
" );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


USAGE (2 cases examples):

  1. From a defined variable product ID (where 746 is the parent variable product ID):

    $variation_id = get_product_variation_id( 'large', 'blue', 746 );

  2. On the variable product single product page (No need to define the variable product ID):

    $variation_id = get_product_variation_id( 'large', 'blue' );

Get Product ID and Variation ID from an Order ID from outside wordpress

Using various aspects of the WooCommerce API, this can be accomplished using some version of the following.

$order_id = XXX;

$order = wc_get_order( $order_id ); //returns WC_Order if valid order
$items = $order->get_items(); //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)

foreach( $items as $item ) {

//returns the type (i.e. variable or simple)
$type = $item->get_type();

//the product id, always, no matter what type of product
$product_id = $item->get_product_id();

//a default
$variation_id = false;

//check if this is a variation using is_type
if( $item->is_type('variable') ) {
$variation_id = $item->get_variation_id();
}

//more code
}

Note the documentation,

wc_get_order();

WC_Order();

WC_Order_Item();

WC_Order_Item_Product();



Related Topics



Leave a reply



Submit