0

I´m not a programmer but I'm running my shop with Magento, I tried to use the suggestion below to create a 'Buy Now' button and works fine.

But add only 1 unit of the product.

How can I do to add the quantities as the client needs?

Suggestion of code:

<button type="button" title="<?php echo $this->__('Check out') ?>" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo Mage::getUrl('onestepcheckout/index/') ?>')"><span><span><?php echo $this->__('Buy Now') ?></span></span></button>
 <script>
 function setcheckoutLocation(location,chkout)
 {
  jQuery.ajax({
                type:"GET",
                url:location,
                success:function(data){
                     window.location.href = chkout;
                }
             });
}
</script>

Today my original code is bellow.

Thanks!

?>
<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
    <div class="add-to-cart left-side">
        <?php if(!$_product->isGrouped()): ?>
            <div class="qty-wrapper">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" />
            </div>
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
    </div>
    <div class="paypal-wrapper"><?php echo $this->getChildHtml('', true, true) ?></div>
<?php endif; ?>

So, I tried the code bellow and did not work with the quantities:

    ?>
    <?php $_product = $this->getProduct(); ?>
    <?php $buttonTitle = $this->__('Add to Cart'); ?>
    <?php if ($_product->isSaleable()): ?>
        <div class="add-to-cart left-side">
            <?php if(!$_product->isGrouped()): ?>
                <div class="qty-wrapper">
                    <label for="qty"><?php echo $this->__('Qty:') ?></label>
                    <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" />
                </div>
            <?php endif; ?>
            <button type="button" title="<?php echo $this->__('Check out') ?>" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo Mage::getUrl('onestepcheckout/index/') ?>')"><span><span><?php echo $this->__('Buy Now') ?></span></span></button>
        </div>
        <div class="paypal-wrapper"><?php echo $this->getChildHtml('', true, true) ?></div>
    <?php endif; ?>

<script>
 function setcheckoutLocation(location,chkout)
 {
  jQuery.ajax({
                type:"GET",
                url:location,
                success:function(data){
                     window.location.href = chkout;
                }
             });
}
</script>
3
  • After adding the <input type="text" name="qty" /> it must work. Are you still facing the same error? Oct 17, 2018 at 10:08
  • Sorry did not understand. Where I need to add this? The last code is the original, I changed the line <button type="button" for the suggestion, and paste the script. But did not add the quantity right.
    – Smarley
    Oct 17, 2018 at 10:27
  • I edit the original post, and put the code I was at the end. Can you check please?
    – Smarley
    Oct 17, 2018 at 10:30

1 Answer 1

0

You have to add the qty parameter in your getAddToCartUrl($_product) function.

Update your code as given below.

<?php $data['qty'] = 3; ?>
<button type="button" title="<?php echo $this->__('Check out') ?>" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product, $data) ?>','<?php echo Mage::getUrl('onestepcheckout/index/') ?>')"><span><span><?php echo $this->__('Buy Now') ?></span></span></button>

 <script>
 function setcheckoutLocation(location,chkout)
 {
  jQuery.ajax({
                type:"GET",
                url:location,
                success:function(data){
                     window.location.href = chkout;
                }
             });
}
</script>

It will add 3 quantity. You can enter your desired quantity to $data['qty']

1
  • Okay, thank you. I will try it. But to add the quantity the client needs, the correct code should be the code below? To get the amount from the box. <?php $data['qty'] = qty; ?>
    – Smarley
    Oct 18, 2018 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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