0

I have the following selector with options:

<select name="" id="product-selector" class="list_item">
    <?php

    if( have_rows('section_a') ):
    while( have_rows('section_a') ): the_row();
    if( have_rows('product_item') ):
    while( have_rows('product_item') ): the_row();

    $product_item_quantity = get_sub_field('product_item_quantity');
    $product_item_price = get_sub_field('product_item_price');
    $product_item_id = get_sub_field('product_item_id');
    $product_item_variation_id = get_sub_field('product_item_variation_id');
        ?>
        <option value="<?php echo $product_item_variation_id; ?>" data-id="<?php echo $product_item_variation_id; ?>" data-content="<?php echo $product_item_quantity; ?> <span class='price' data-count='<?php echo $product_item_quantity; ?>' data-price='<?php echo $product_item_price; ?>'><?php echo $product_item_price; ?>"> </option>
    <?php endwhile; endif; endwhile; endif; ?>
</select>

And I have the following button:

<div class="submit">
<a class="btn btn-warning" id="purchase" href="?add-to-cart=<?php echo $product_item_id; ?>&variation_id=<?php echo $product_item_variation_id; ?>">Order Now</a>
</div>

My javascript is:

<script>
document.getElementById("product-selector").onchange = function() {
    document.getElementById("purchase").href = "?add-to-cart="+"<?php echo $product_item_id; ?>"+"&"+"variation_id="+this.value+"/";
}
</script>

When user click by the Order button the link changes dynamically - it gets the value of and should get the <?php echo $product_item_variation_id; ?>" via the "variation_id="+this.value+" and it works fine, but when the page loads, the <?php echo $product_item_variation_id; ?> value of the button get the LAST option value, but not a first (default) one.

Much appreciate any help.

2
  • Can you share the code where $product_item_variation_id is set? Commented Apr 10, 2020 at 2:44
  • Sure, I've updated my first post Commented Apr 10, 2020 at 2:47

1 Answer 1

1

I think that you should make a variable to save the first value. Because you already loop. so default value is last value of the select.

<?php $product_item_variation_id_first = ""; ?>
<select name="" id="product-selector" class="list_item">
    <?php

    if( have_rows('section_a') ):
    while( have_rows('section_a') ): the_row();
    if( have_rows('product_item') ):
    while( have_rows('product_item') ): the_row();

    $product_item_quantity = get_sub_field('product_item_quantity');
    $product_item_price = get_sub_field('product_item_price');
    $product_item_id = get_sub_field('product_item_id');
    $product_item_variation_id = get_sub_field('product_item_variation_id');
    if ($product_item_variation_id_first == "")
        $product_item_variation_id_first = $product_item_variation_id;
        ?>
        <option value="<?php echo $product_item_variation_id; ?>" data-id="<?php echo $product_item_variation_id; ?>" data-content="<?php echo $product_item_quantity; ?> <span class='price' data-count='<?php echo $product_item_quantity; ?>' data-price='<?php echo $product_item_price; ?>'><?php echo $product_item_price; ?>"> </option>
    <?php endwhile; endif; endwhile; endif; ?>
</select>

<div class="submit">
<a class="btn btn-warning" id="purchase" href="?add-to-cart=<?php echo $product_item_id; ?>&variation_id=<?php echo $product_item_variation_id_first; ?>">Order Now</a>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

please let me know the result. I will be online in 30 mins.
Just started adding the variable and can't get how it would be better to add it. I use ACF PRO for this solution as you can see. I don't think that it's a good idea to place the new var in the repeater. Please, let me know your thoughts.
If you think that it's not good, we can use the javascript. When page is loaded, we can trigger the dropdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.