Using Jquery Ajax in Magento + Ajax Header Sidebar

127 篇文章 0 订阅
在topmenu.phtml文件里。增加以下JS.
(function($){  
    $.ajax({  
        url: "<?php echo $this->getUrl('checkout/cart/headcount');?>",  
        type: "POST",  
        dataType:"json",  
        success: function(data) {  
            $('.cart_header').html(data['html']);
        }  
    });  
    $('.cart_header').live('mouseenter',function(){
        $('.block-content').show();
    });
    $('.cart_header').live('mouseleave',function(){
        $('.block-content').hide();
    });   
})(jQuery); 

 顺便贡献下如果要用AJAX来加载Header部分的SIdebar可以用以下方法

    public function headcountAction() {
    	$html = 'hello';
    	$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
    	$block->setTemplate('checkout/cart/sidebar_header.phtml')
    	->addItemRender('simple','checkout/cart_item_renderer','checkout/cart/sidebar/default.phtml')
    	->addItemRender('grouped','checkout/cart_item_renderer_grouped','checkout/cart/sidebar/default.phtml')
    	->addItemRender('configurable','checkout/cart_item_renderer_configurable','checkout/cart/sidebar/default.phtml');
    	$html = $block->toHtml();
    	$arr = array('html'=>$html);
    	echo json_encode($arr);
    	return;
    }

 上面用php创建block并使用各种方法的代码是按照核心的layout翻译得到,这段layout在事后应该注释掉

                    	<block type="checkout/cart_sidebar" name="cart_header" template="checkout/cart/sidebar_header.phtml">
		                    <action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/sidebar/default.phtml</template></action>
		                    <action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/sidebar/default.phtml</template></action>
		                    <action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/sidebar/default.phtml</template></action>
		                    <block type="core/text_list" name="cart_sidebar.extra_actions" as="extra_actions" translate="label" module="checkout">
		                        <label>Shopping Cart Sidebar Extra Actions</label>
		                    </block>
		         </block>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值