1

How can I get submit url in form action for products with custom options or configurable options like the form below.I don't get the submit url with this code.

<form data-role="tocart-form" id="product_addtocart_form" action="<?php /* @escapeNotVerified */ echo $block->getSubmitUrl($_product) ?>" method="post" enctype="multipart/form-data">

I get form action url with the following , but it only works for products without custom options.

<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post" enctype="multipart/form-data">

I have a custom phtml file , where I have fetched products with custom options: the custom options do appear on the page along with add to cart options. When I click on the add to cart for products with custom options, the product is not added , instead the page is redirected to the product view page. Can anyone explain why my custom options are not sent in the post values along with the other post data.If the product is without custom options it is added successfully, also quantity is sent correct too. The problem is with custom options.

<?php  $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>      
<?php foreach ($_productCollection as $_product): ?>

<?php $customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);?>                  
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<strong> <?php echo $_product->getName()?></strong></br>
</br>
   <?php echo $block->getProductPrice($_product)?>

<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
    <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">

<div class="box-tocart">
<div class="fieldset">
    <div class="field qty">
        <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
        <div class="control">
            <input type="number"
                   name="qty"
                   id="qty"
                   maxlength="12"
                   value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                   title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                   data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                   />
        </div>
    </div>
     </div>
</div>
<?php if($customOptions):?>     

<?php 
  foreach($customOptions as $_option):?>
<?php
//print_r($options->getData());exit;
//$_option = $options->getOption();
$class = 'require';
?>

<div class="field<?php if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_AREA) {
echo ' textarea';
} ?><?php /* @escapeNotVerified */ echo $class ?>">
<label class="label" for="options_<?php /* @escapeNotVerified */ echo $_option->getId() ?>_text">
    <span><?php echo $block->escapeHtml($_option->getTitle()) ?></span>
    <?php /* @escapeNotVerified */ echo $block->getFormatedPrice() ?>
</label>

<div class="control">
    <?php if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_FIELD): ?>
        <?php $_textValidate = null;
        if ($_option->getIsRequire()) {
            $_textValidate['required'] = true;
        }
        if ($_option->getMaxCharacters()) {
            $_textValidate['maxlength'] = $_option->getMaxCharacters();
        }
        ?>
        <input type="text"
               id="options_<?php /* @escapeNotVerified */ echo $_option->getId() ?>_text"
               class="input-text product-custom-option"
               <?php if (!empty($_textValidate)) {?>
               data-validate="<?php echo  $block->escapeHtml(json_encode($_textValidate));?>"
               <?php } ?>
               name="options[<?php /* @escapeNotVerified */ echo $_option->getId() ?>]"
               data-selector="options[<?php /* @escapeNotVerified */ echo $_option->getId() ?>]"
               value="<?php echo $block->escapeHtml($block->getDefaultValue()) ?>"/>
    <?php elseif ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_AREA): ?>
        <?php $_textAreaValidate = null;
        if ($_option->getIsRequire()) {
            $_textAreaValidate['required'] = true;
        }
        if ($_option->getMaxCharacters()) {
            $_textAreaValidate['maxlength'] = $_option->getMaxCharacters();
        }
        ?>
        <textarea id="options_<?php /* @escapeNotVerified */ echo $_option->getId() ?>_text"
                  class="product-custom-option"
                <?php if (!empty($_textAreaValidate)) {?>
                    data-validate="<?php echo $block->escapeHtml(json_encode($_textAreaValidate));?>"
                <?php } ?>
                  name="options[<?php /* @escapeNotVerified */ echo $_option->getId() ?>]"
                  data-selector="options[<?php /* @escapeNotVerified */ echo $_option->getId() ?>]"
                  rows="5"
                  cols="25"><?php echo $block->escapeHtml($block->getDefaultValue()) ?></textarea>
    <?php endif; ?>
    <?php if ($_option->getMaxCharacters()): ?>
        <p class="note"><?php /* @escapeNotVerified */ echo __('Maximum number of characters:') ?>
            <strong><?php /* @escapeNotVerified */ echo $_option->getMaxCharacters() ?></strong></p>
    <?php endif; ?>
</div>
</div>
<?php endforeach;?>
<?php endif;?>
    <?php echo $block->getBlockHtml('formkey')?>
    <?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface'); ?>
        <button type="submit"
                title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                class="action tocart primary">
            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
        </button></br></br>

</form>


1

you can check the method getAddToCartUrl in `Magento\Catalog\Block\Product\View.php' File

   protected $_cartHelper;

   public function __construct(
        .....
        \Magento\Checkout\Helper\Cart $cartHelper,,
        .....

    ) {


        $this->_cartHelper = $_cartHelper;

    }
    public function getAddToCartUrl($product)
    {   
        $additional=array();
        return $this->_cartHelper->getAddUrl($product, $additional);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.