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:


所有评论(0)