John Davidson

php - how to get variation id by attributes in woocommerce

0 comments
Message:


I want to find the ID and verifiable when the user clicks on my attributes, please help.
For example, we have two attributes:
Region = ars
Charge amount = 100ars


When both are selected, a valid ID must be returned


my code:


<?php $attributes = $product->get_attributes() // GET ALL ATRIBUTES ?>
<?php foreach ($attributes as $key => $value): ?>
<?php $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


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

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


$atribute_list = [];
if ($attribute_name_ == 'regions') {

$atribute_list['attribute_pa_regions'] = $slug['0'];
}
if ($attribute_name_ == 'charge-amount') {
$atribute_list['attribute_pa_charge-amount'] = $slug['0'];
}

// echo find_matching_product_variation_id($product, $atribute_list);


?>


<?php
if ($attribute_name_ == 'regions') {


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

<img src="https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg"
alt="">
<span>
<?php

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

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

}


if ($attribute_name_ == 'charge-amount') {

$atribute_list['attribute_pa_charge-amount'] = $slug['0'];
?>

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

<?php

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

?>

</div>
<?php
}


?>


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

I managed to display only the attributes separately here, and now I want the ID and verifiable to be displayed or enclosed in a variable when the related attributes are clicked.


This is how my code output is:




My Answer:
To get the variation ID based on selected attributes in WooCommerce, you can use the following code:

php
$attributes = $product->get_attributes(); // Get all attributes

$selected_attributes = []; // Array to store selected attributes

foreach ($attributes as $key => $value) {
$attribute_name = preg_replace('/pa_/', '', $key); // Get attribute name

$attribute_terms = wc_get_product_terms(get_the_ID(), $key); // Get attribute terms

if (!empty($_POST[$attribute_name])) {
$selected_attributes[$key] = $_POST[$attribute_name]; // Store selected attribute value
}
}

$variation_id = find_matching_product_variation_id($product, $selected_attributes); // Function to find matching variation ID

echo $variation_id; // Display or store the variation ID
?>


In this code snippet, we loop through all attributes and check if any attribute value is selected by the user. If a value is selected, we store it in the `$selected_attributes` array. Then, we use a custom function `find_matching_product_variation_id` to find the variation ID based on the selected attributes. Finally, we display or store the variation ID.

Make sure to replace `find_matching_product_variation_id` with your actual function that finds the matching variation ID based on the selected attributes.

Rate this post

3 of 5 based on 4871 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us