Toggle dropdown mini-cart when product added to basket

穿精又带淫゛_ 提交于 2020-01-15 05:38:53

问题


I have a jQuery dropdown mini cart placed in the header of my Magento store. When the "Your Cart" link is hovered the menu dropsdown to display recently added items.

I have now integrated the Ajax add to cart extension which allows customers to add to cart without refreshing the page. The problem I have now is that when products are added there is no way of telling without hoevering over the "Your Cart" link.

I would like to be able to have the mini-cart drop down automatically when a product is added but I am not sure how to do this?

Could somebody give me some advice please?

Thanks in advance!

Code for Ajax add to cart:

    <script type="text/javascript">
    //<![CDATA[
        var productAddToCartForm = new VarienForm('product_addtocart_form');
        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;
            // Start of our new ajax code
            if (!url) {
                url = jQuery('#product_addtocart_form').attr('action');
            }
            url = url.replace("checkout/cart","ajax/index"); // New Code
            var data = jQuery('#product_addtocart_form').serialize();
            data += '&isAjax=1';
            jQuery('#ajax_loader').show();
            try {
                jQuery.ajax( {
                    url : url,
                    dataType : 'json',
                    type : 'post',
                    data : data,
                    success : function(data) {
                        jQuery('#ajax_loader').show();
                        //alert(data.status + ": " + data.message);
                        if(jQuery('.block-cart')){
                            jQuery('.block-cart').replaceWith(data.sidebar);
                        }
                        if(jQuery('.header .links')){
                            jQuery('.header .links').replaceWith(data.toplink);
                        }
                    }
                });
            } catch (e) {
            }
            // End of our new ajax code
            this.form.action = oldUrl;
            if (e) {
                throw e;
            }
        }
    }.bind(productAddToCartForm);
    productAddToCartForm.submitLight = function(button, url){
            if(this.validator) {
                var nv = Validation.methods;
                delete Validation.methods['required-entry'];
                delete Validation.methods['validate-one-required'];
                delete Validation.methods['validate-one-required-by-name'];
                if (this.validator.validate()) {
                    if (url) {
                        this.form.action = url;
                    }
                    this.form.submit();
                }
                Object.extend(Validation.methods, nv);
            }
        }.bind(productAddToCartForm);
    //]]>
    </script>

And code for Mini-Cart:

function slideUp()
{
    jQuery('#topCartContent:visible').slideUp(1000);
    jQuery('.top-link-cart').addClass('mini-cart-layer-up');
    jQuery('.top-link-cart').removeClass('mini-cart-layer-down');
}

function slideDown()
{
    jQuery('#topCartContent:hidden').slideDown(1000);
    /*startTimer()*/ /* optional*/
    jQuery('.top-link-cart').addClass('mini-cart-layer-down');
    jQuery('.top-link-cart').removeClass('mini-cart-layer-up');
}

function toggleTopCart()
{
    if(jQuery('#topCartContent').is(':visible'))
    {
        slideUp();
    } else {
        slideDown();
    }
}

var timer;
function startTimer()
{
    timer = setTimeout(function(){
        slideUp();
    }, 5000);
}

jQuery(document).ready(function(){
    jQuery('.top-link-cart').mouseover(function(){
        toggleTopCart();
    });

    jQuery('.top-link-cart').mouseover(function(){
        clearTimeout(timer);
    }).mouseout(function(){
        startTimer();
    });

    jQuery("#topCartContent").mouseover(function() {
        clearTimeout(timer);
    }).mouseout(function(){
        startTimer();
    });
});

回答1:


This is amazingly simple, but I learn this after wasting a few of my hours (they were not waste actually)

Create an observer to observe when a product is added to cart.

Config.xml

<events>
<checkout_cart_product_add_after>
    <observers>
        <namespace_triggerevent>
            <type>singleton</type>       
            <class>NameSpace_TriggerEvent_Model_Observer</class>
            <method>opencartafteradd</method>
        </namespace_triggerevent>
    </observers>
</checkout_cart_product_add_after>
</events>

Now, Observer.php

<?php

class NameSpace_TriggerEvent_Model_Observer
{
public function opencartafteradd($observer) {
    $event = $observer->getEvent();
    $product = $event->getProduct(); 
    Mage::getSingleton('core/session')->setOpenMinicart('ON');
 }
}

Note that we are just setting a session variable ON. After this rest part would be handled using a small amount of javascript on preferably header.phtml or footer.phtml (available to every single page on site :P). I just toggle down the mini cart contents part and toggle up again after few seconds

<script type="text/javascript">
var $k = jQuery.noConflict();
$k(document).ready(function() {
    var screen_width = $k(window).width();
    var openminicart = '<?php echo Mage::getSingleton('core/session')->getOpenMinicart();?>';
    if (screen_width > 780) {
        if (openminicart == 'ON') {
                jQuery("#header-cart").slideToggle('slow');
                jQuery("#header-cart").addClass('skip-active');


                setTimeout(function() {
                    jQuery("#header-cart").slideUp('fast');
                    $k('#header-cart').removeClass('skip-active');
                }, 4000);


        <?php Mage::getSingleton('core/session')->unsOpenMinicart();?>

        }
    }
});
</script>



回答2:


If you use jQuery slideDown(),-Up() or -Toggle(), you'll notice an animation glitch (aka a vertically moving layer for a sec).

So what I did was rather simply:

if ( $('.messages .success-msg').length ) {

    $('.messages').css('display','none'); // 'disable' default messages on top of content

    function showCartForASecOrThreeAndAHalf() {
        $('#mini-cart .menu').css('display','block');
        setTimeout(function () { $('#mini-cart .menu').css('display',''); }, 3500); // display:'' to restore previous settings
        }

        showCartForASecOrThreeAndAHalf();
}

Hope this helps you any further ;-)




回答3:


you say '... when products are added there is no way of telling without hoevering over the "Your Cart" link.'. There must be a way. What is the code that triggers the the 'drop-down' action of of your cart?

Most likely it is a jQuery.sliedeDown or similar. In your ajax call's success function, you need to execute the same. Alternatively, you may programatically trigger a JavaScript event, e.g. in the success function: $('#cart').mouseover().



来源:https://stackoverflow.com/questions/13841390/toggle-dropdown-mini-cart-when-product-added-to-basket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!