I would like to change the value of action attribute of form tag when stock is "0". When the stock is 0, it should show go to the next product ID and set new URL of next product through foreach loop. I am generating dynamic value by using this variable - $value.
First time my code worked perfectly but after inventory is 0 it has gone to last item and never rotating to the first item. I just did the task as trial so want to start from first again. 12, 944, 993 are the IDs of the products I added using woo commerce. 93 is the last product for this URL - http://localhost/wordpress/product/22q2-prime993 . I just want to rotate the loop from first but it's not rotating and always opening the last product when I click on button. Here is my full code -
function btn_shortcode(){
$link_url = array (12, 944, 993);
foreach ($link_url as $value) {
$increment = 0;
$productt = wc_get_product( $value);
$qtyy = $productt->get_stock_quantity();
if($qtyy == 0) return;
if($increment == 1)return;
$increment++;
}
$url = "http://localhost/wordpress/product/22q2-prime";
?>
<form class="cart" action="<?php echo $url . $value; ?>" method="post">
<div class="quantity">
<label class="screen-reader-text" for="quantity_6332f81bdfee0">22Q2 - Prime - 2 quantity</label>
<div class="bs-quantity">
<div class="qty-nav">
<div class="quantity-button quantity-down limit">-</div>
</div>
<input type="number" id="quantity_6332f81bdfee0" class="input-text qty text" step="1" min="1" max="2" name="quantity" value="1" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric" aria-labelledby="22Q2 - Prime - 2 quantity">
<div class="qty-nav">
<div class="quantity-button quantity-up ">+</div>
</div>
</div>
</div>
<button type="submit" name="add-to-cart" value="12" class="single_add_to_cart_button button alt">Join Now</button>
</form>
<?php
}
add_shortcode('sc_btn','btn_shortcode');
I can see your $increment variable is initially set to 0. There's no way it'll ever turn to a 1 for the IF condition to work. You could use the for function and count() to know number of records in array, that way it would be easy to loop with the increment variable too. But there's not enough clue on your issue to me on what you want exactly. My observation is with the $increment variable
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.