Showing posts with label Cart. Show all posts
Showing posts with label Cart. Show all posts

Saturday 23 November 2019

How to add/update multiple products in cart in shopify

jQuery(function () {
   
jQuery('body').find(".couponproduct .gf_add-to-cart").click(function (e) {
e.preventDefault();
values = {}
                values[30370850865245] = 0;
                values[31289364840541] = 1;
                jQuery.ajax({
                      type: 'POST',
                      url: '/cart/update.js',
                      data: { updates: values},
                      dataType: 'json',
                      success: function() {
                        location.href = '/checkout';
                     }
               });
});
   
});

Friday 29 April 2016

Adding products to cart via url in magento

If your Planning to do a website that opens directly on the cart page, in the cart it needs to have some products already selected and it should be ready to go to the checkout page.

Every time the user goes to the page, some products.

<a href="http://example.com/index.php/checkout/cart/add/product/1/form_key/<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>/">My cart</a>

Thursday 20 August 2015

Simple Ajax add to cart in magento

list.phtml page in template

<form class="product_addtocart_form" action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
 <?php if(!$_product->isGrouped()): ?>
                         <label for="qty" class="qtylabel" style=""><?php echo $this->__('Qty') ?>:</label>
 <input type="text" class="form-control qtybox" name="qty">
 <?php endif; ?>
 <button type="submit" class="link_cart"><span><i class="fa fa-shopping-cart" style="margin-right: 5px;"></i><?php echo $this->__('Add to Cart') ?></span></button>
 </form>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('.product_addtocart_form').submit(function(event){
            event.preventDefault();
            var $this = jQuery(this);
            jQuery($this).parents('li').append('<div class="ajax_loader"></div>');
            url = jQuery(this).attr('action');
            console.log(jQuery(this).serialize());
            url += '&isAjax=1';
            var data = jQuery(this).serialize();
            data += '&isAjax=1';
            jQuery.ajax({
                    url : url,
                    dataType : 'json',
                    type : 'post',
                    data : data,
                    success : function(data) {
                        getcartdata($this);
                    }
                });
            setTimeout(function(){
                //getcartdata($this);
            },1000);
        });
    });






    function getcartdata($this){
        jQuery.ajax( {
            url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
            dataType : 'html',
            type : 'get',
            success : function(data) {
                jQuery('.ajax_loader').hide();
                jQuery('.block-cart-header').html(data);
            }
        });
    }
</script>

Make in controllers/IndexController.php

public function getcartdetailAction() {
        $block = $this->getLayout()->createBlock('checkout/cart_sidebar');
        $block->setTemplate('checkout/cart/sidebar_header_ajax.phtml');
        echo $block->toHtml();exit;
 }


view.phtml page in template
 
productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;
                try {
                    this.form.submit();
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }

                if (button && button != 'undefined') {
                    button.disabled = true;
                }
            }
           
        }.bind(productAddToCartForm);

Replace above code To below code


productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;
                if(!url){
                    url = jQuery('#product_addtocart_form').attr('action');
                }
                var data = jQuery('#product_addtocart_form').serialize();
                data += '&isAjax=1';
                try {
                    jQuery('.product-shop').prepend('<div class="ajax_loader">&nbsp;</div>');
                    jQuery.ajax({
                          url: url,
                          dataType: 'json',
                          type : 'post',
                          data: data,
                          success: function(data){
                                getcartdata(this);
                          }
                    });
                    setTimeout(function(){
                        getcartdata(this.form);
                    },1000);
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }
            }
        }.bind(productAddToCartForm);

Write Bottom in view page

<script type="text/javascript">
    function getcartdata($this){
        jQuery.ajax( {
            url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
            dataType : 'html',
            type : 'get',
            success : function(data) {
                jQuery('.ajax_loader').hide();
                jQuery('.block-cart-header').html(data);
            }
        });
    }
</script>

Tuesday 18 August 2015

How to get only cart sidebar Layout block in magento

In your controller you could try something like this:

$block = $this->getLayout()->createBlock('checkout/cart_sidebar');

$block->setTemplate('checkout/cart/sidebar.phtml');
 
Depending on your configuration (Config -> Checkout -> Shopping Cart Sidebar),
you can render the template with  
 
$block->toHtml(); 
If you use a custom template, you could ignore the config value so it renders anytime.