0

So I need to include an add to cart button on a cms page so I'm trying to do this through a template block file in php. I can't figure out how to do this for just one static product though.

So the code for the usual add to cart button on product pages looks like this:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="btn-add-box">
        <button type="button" title="<?php echo $buttonTitle ?>" class="btn btn-warning add-to-cart" style="margin-top:6px;width:200px;margin-left:20px;" data-url="<?php echo $this->getAddToCartUrl($_product, ['_secure' => true]) ?>" onclick="ga('ec:setAction', 'add'); ga('send', 'event', 'UX', 'click', 'add to cart');"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>

But how do I do this for just the product with the ID 33? So I only want to display an add to cart button that adds the product with this ID on a static page. I've tried using a url on the cms page so like http://testing.co.uk/checkout/cart/add?product=[33]&qty=[1] but this goes to the cart, but doesn't add the product.

1 Answer 1

0

Try to use this:

<script type="text/javascript">
  function update_qty(id){
      var qty = document.getElementById('qty').value;
      document.getElementById('qty'+id).value = qty;
  }
</script>

<button type="submit"  onclick="update_qty(<?php echo $_product->getEntityId(); ?>);"
    title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
    class="action tocart primary">
    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?>
    </span>
</button>

for show this only for a specific product do something like this : if($_product->getId() == 'x') { show button }

Your Answer

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

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