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

Magento 2 - How can I change the quantity of a product (on product page) with php or 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 \Magento\Catalog\Block\Product\View */
?>
<?php $_helper = $this->helper('Magento\Catalog\Helper\Output');?>
<?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 \Magento\Catalog\Block\Product\View */
?>
<?php $_helper = $this->helper('Magento\Catalog\Helper\Output');?>
<?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 头部。

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