Magento 2-如何用php或js更改产品的数量(在产品页面上)



我正在寻找一种方法来更改代码中数量框的值,而不是在产品页面上手动更改它。

提交表单后,$Uitkomst必须在输入中。

我一直没能找到问题的答案,所以如果能提供任何帮助,我将不胜感激。

提前谢谢。

我在数量输入中需要的值来自以下表格:

<form action="?" method="POST">
<table>
<tr>
<td>
<input name="breedte" type="text" maxlength="40" placeholder="Breedte (in millimeters)" required>
</td>
</tr>
<tr>
<td>
<input name="lengte" type="text" maxlength="40"  placeholder="Lengte (in millimeters)" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Toevoegen aan winkelwagen">
</td>
</tr>
</table>
</form>
<?php
if (isset($_POST["lengte"]))
{
$Lengte = $_POST["lengte"];
$Breedte = $_POST["breedte"];
$Uitkomst = $Lengte * $Breedte;
echo $Uitkomst;

}
?>     
<div class="control">
<input
name="qty"
id="qty"
value="<?= block->getProductDefaultQty() * 
1 ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
/>
</div>

更改了答案。

它不会刷新页面,这是你需要的吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form onsubmit="event.preventDefault();fillQty();" id="form_id">
<table>
<tr>
<td>
<input name="breedte" type="text" maxlength="40" placeholder="Breedte (in millimeters)" required>
</td>
</tr>
<tr>
<td>
<input name="lengte" type="text" maxlength="40"  placeholder="Lengte (in millimeters)" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Toevoegen aan winkelwagen">
</td>
</tr>
</table>
</form>    
<div class="control">
<input
type="text"
name="qty"
id="qty"
value="<?= block->getProductDefaultQty() * 
1 ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
/>
</div>
<script>
function fillQty() {
$('#qty').val($('input[name="breedte"]').val() * $('input[name="lengte"]').val());
return false;
};
</script>

我已经找到了问题的解决方案,

在CustomForm.phtml文件中,我放入

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var $block MagentoCatalogBlockProductView */
?>
<?php $_helper = $this->helper('MagentoCatalogHelperOutput');?>
<?php $_product = $block->getProduct(); ?>
<div>
<div class="CalculatorForm">
<form onsubmit="event.preventDefault();fillQty();" id="form_id">
<table>
<tr>
<td>
<input name="breedte" type="number" maxlength="40" placeholder="Breedte (in millimeters)" required>
</td>
</tr>
<tr>
<td>
<input name="lengte" type="number" maxlength="40"  placeholder="Lengte (in millimeters)" required>
</td>  
</tr>  
<tr>
<td>
<input type="submit" value="Bereken prijs">
</td>
</tr>
</table>
</form>
<div class="field qty">
<!--<?php #if ($block->shouldRenderQuantity()) :?>-->
<label class="label" for="qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
<div class="control">
<input type="number"
name="qty"
id="qty"
min="0"
value="<?= $block->getProductDefaultQty() * 1 ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
data-validate="<?= $block->escapeHtmlAttr(json_encode($block->getQuantityValidators())) ?>"
/>
</div>
<?php #endif; ?>
<script>
function fillQty(){
$('#qty').val($('input[name="breedte"]').val() * $('input[name="lengte"]').val());
return false;};
</script>
</div>
</div>
</div>

在最初的addtocart.phtml中,我放置了

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var $block MagentoCatalogBlockProductView */
?>
<?php $_helper = $this->helper('MagentoCatalogHelperOutput');?>
<?php $_product = $block->getProduct();?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()) :?>
<div class="box-tocart">
<div class="fieldset">
<div class="control">
<?php if ($block->shouldRenderQuantity()) :?>
<input type="number"
name="qty"
id="qty"
min="0"
value="<?= $block->getProductDefaultQty() * 1 ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
data-validate="<?= $block->escapeHtmlAttr(json_encode($block->getQuantityValidators())) ?>"
/>
<?php endif ?>
</div>
<div class="actions">
<button 
type="submit"
title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
class="action primary tocart"
id="product-addtocart-button">
<span>
<?= $block->escapeHtml($buttonTitle) ?>
</span>
</button>
<?= $block->getChildHtml('', true) ?>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/js/validate-product": {}
}
</script> 
</div>
</div>

我还将<script src="{{MEDIA_URL}}jQuery-3.3.1.js?v=1.x"></script>放在了magento后端的html头中。

这解决了我的问题。谢谢大家。

相关内容

  • 没有找到相关文章

最新更新