【发布时间】:2018-10-30 08:20:15
【问题描述】:
我正在尝试使用一段代码来显示表中的数据,将数据保存在一个复选框中,如果选中该复选框,则会将其转发。但是我在尝试选择与原始数量不同的数量时遇到问题,由于在加载页面时复选框被设置为石头,它总是会选择最大数量,我可以以某种方式更新复选框值吗?复选框代码如下所示:
$order = wc_get_order( $ordernumber );
foreach ($order->get_items() as $item ){
$unitprice = $item->get_total() / $item->get_quantity();
echo "<input type='checkbox' name='productinfo[]' value='" . " " . $item->get_name() . " , " . $item->get_quantity() . " units" . " | " . $item->get_total() ."'>";
这会显示数量为 2 或更多的数量选择:
if($item->get_quantity() > 1) {
echo "Amount: " . "<input type='number' name='numberqty' value='" . $item->get_quantity() . "'max='" .$item->get_quantity() . "' min='1' > " . "<br/>";
}
这是它在网站上的显示方式:(注意,从上到下分别表示“产品名称”“数量”“单价”“产品总价”)
在选择所需的复选框后,用户将按下一个按钮,将他们引导到下一个站点,其中包含上一页中选定的产品
如果用户在这里选择了2,我希望下一页的数字是2,如果选择了1,那么它就是1。但是现在即使选择了2,它也会在下一页显示3而不是想要的 2.
有没有办法通过将给定数量更新为之前设置的复选框值来完成这项工作?
编辑:根据要求,清除我想要它做的是。为了允许我在使用 foreach 制作的已定义复选框中选择具有输入类型编号的数量,该复选框手动保存复选框内的数据,目标是允许将复选框的值更新为具有新数量的值,该数量已设置原来在这段代码中:
$quant = $item->get_quantity();
echo "<input type='checkbox' name='productinfo[]' value='" . " " . $item->get_name() . " , " . $quant . " kpl" . " | " . $item->get_total() ."'>";
这是第 2 页和第 3 页的完整代码。
<div class="Etusivu">
<form action="" method="post" style="border:0px solid #ccc">
<fieldset><legend><b>Tuotteiden palautus</b></legend>
<div class="step">
<legend>Askel 2/3</legend>
</div>
<br />
<p class="important">Tilauksen tiedot</p>
<br />
<div class="valitse">
<p class="important">Valitse kaikki tuotteet, jotka haluat palauttaa</p>
</div>
<hr>
<script>
//function for making a checkbox to check all checkboxes
function toggle(source) {
checkboxes = document.getElementsByName('productinfo[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
function checkboxupdate {
document.getElementById("numberqty").value = "<?php " " . $item->get_name() . " , " . "2" . " kpl" . " | " . $item->get_total() ?>"
}
</script>
<input type='checkbox' onClick='toggle(this)' />
<p class="selectall">Valitse kaikki tuotteet</p>
<label>Tilauksessa tulleet tuotteet:</p>
<br />
<?php
//Gets and displays data based on certain variables from the database, connects quantity and product name into one string in array
$order = wc_get_order( $ordernumber );
foreach ($order->get_items() as $item ){
$max = $item->get_quantity();
$quant = $item->get_quantity();
$unitprice = $item->get_total() / $item->get_quantity();
echo "<input type='checkbox' name='productinfo[]' value='" . " " . $item->get_name() . " , " . $quant . " kpl" . " | " . $item->get_total() ."'>";
echo '<p>';
echo __('Tuotteen nimi: ' ) . $item->get_name() . '<br>';
if($item->get_quantity() > 1) {
echo "Määrä: " . "<input type='number' name='numberqty' value='" . $quant . "'max='" . $quant . "' min='1' > " . "<br/>";
}
else {
echo __('Määrä: ' ) . $quant . '<br>';
}
if ($item->get_quantity() > 1) {
echo __('Tuotteen kappalehinta: ') . $unitprice . '€' . '<br/>';
}
echo __('Tuotteiden kokonaishinta: ' ) . wc_price($item->get_total()) . '</p>' . '<br/>';
}
echo '<p>'. __('Tilauksen yhteishinta: ') . $order->get_total() . '</p>';
echo "<button onclick='checkboxupdate()'>kokeile</button>";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" id='check' value="Seuraava"/>
<script>
//Checks if any checkboxes have been checked, if not, displays an error
function checkBoxCheck() {
if($('input[name="productinfo[]"]:checked').length) {
console.log("at least one checked");
return true;
}
else {
alert("Valitse vähintään yksi tuote.");
return false;
}
}
//runs the script for checking the checkboxes
$('#check').on('click', checkBoxCheck);
</script>
</label>
<div class="clearfix">
<input type="hidden" name="page" value="2">
</div>
</fieldset>
</form>
<br />
</div>
</body>
</html>
<?php
//checks if the 2nd page submit button has been pressed successfully, if so continues to the next page
} if ($page == 2) {
global $wbdb;
//prints errors, for debugging
if($wpdb->last_error !== ''){
$wpdb->print_error();
//creates a new variable for later input of a comment within a textarea
$comments = $_SESSION['commentone'] = $_POST['comments'];
}
?>
<body>
<div class="Etusivu">
<form action="" method="post" style="border:0px solid #ccc">
<fieldset><legend><b>Tuotteiden palautus</b></legend>
<div class="step">
<legend>Askel 3/3</legend>
</div>
<br />
<p class="important">Palautuksen varmistus</p>
<br />
<div class="valitse">
<p class="important">Haluatko varmasti palauttaa seuraavat tuotteet?</p>
</div>
<hr>
<?php
//Makes an array from given data from the previous page
$test = $_POST['productinfo'];
//variable for counting total price of the chosen items
$total2 = 0;
//variable for counting the total quantity of the selected items
$totalquantity = 0;
//Loop for displaying every selected product individually and their prices and quantities and adding each price and quantity to eachother to allow calculation of total price and quantity
for($i=0; $i < sizeof($test); $i++) {
list($name, $total) = explode("|", $test[$i]);
echo "Nimi: ".$name;
echo "<br>";
echo "Hinta: ".$total . "€";
echo "<br>";
echo "<br/>";
$total2 += $total;
$totalquantity += $quantity;
$names[] = $name;
}
//Sets the total price, quantity into a session
$_SESSION['totalprice'] = $total2;
$_SESSION['totalquantity'] = $totalquantity;
//Uses Json encoding to set the array including the displayed names into session data to allow the displayment of each within the secondary form to insert the data into the database
$_SESSION['product'] = json_encode($names, JSON_UNESCAPED_UNICODE );
?>
<br />
<br />
<h4>Kirjoita alas, miksi haluat palauttaa tuotteen/tuotteet?</h4>
<?php
//variable for textarea comment to allow insertion into database
$comments = $_POST['comments'];
//Inside textarea $comments displays the data inside it to save it and insert it into database that way?>
<textarea id='commenter' name='comments' cols='30' rows='4' placeholder='Kirjoita tähän:'><?php echo $comments; ?></textarea>
<div class="refundprice">
<?php //inside label is a $total2 variable which is the total price of everything that was checked and taken to this page ?>
<?php echo '<label>Palautettavien tuotteiden yhteishinta: ' . $total2 . '€' . '</label>'
?>
</div>
<div class="clearfix">
<input type="hidden" name="page" value="3">
<input type="submit" class="signupbtn" name="sendrqst" value="Lähetä">
</div>
</fieldset>
</form>
</div>
</body>
</html>
<?php
}
?>
【问题讨论】:
-
你能简单描述一下你想要什么吗??
-
你能把完整的代码贴在这里吗?我不明白你的要求。
-
简而言之,我希望能够更改已创建的复选框@bhanusengar 中的值
-
将第 2 页和第 3 页的完整代码(重要的)添加到原始帖子@OutsourceWordPress
-
是的,它会为您提供为订单中相应产品/项目选择的适当数量。因此,如果您将数量字段的名称更改为
numberqty[],在下一页(即第 3 页)上,在list($name, $total) = explode("|", $test[$i]);之后,您可以添加例如$quantity = $_POST['numberqty'][$i];。试一试,看看效果如何..
标签: php wordpress woocommerce