如何添加AJAX的通知时,该产品已成功添加到购物车 [英] how can I add ajax notice when the product was successfully added to cart

查看:159
本文介绍了如何添加AJAX的通知时,该产品已成功添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Magento的1.9.1和我安装了自动添加产品到购物车与阿贾克斯的延伸,之后,产品已成功添加到购物车这个扩展显示通知:您的产品NAME添加到购物车。我想改变这个通知与整个产品的详细信息,如产品形象,SKU,名称,描述......谁能告诉我从哪里开始?我的通知功能是这样的:

I have Magento 1.9.1 and I installed an extension that automatically add product to cart with ajax, after the product was successfully added to cart this extension display a notification: Your product "NAME" was added to cart. I want to change this notification with entire product details, like product image, sku, name, description ... Can anyone tell me where to start? My notification function look like this:

function notifyBar(msg) {      
    jQuery('.notification').show('fast');
    jQuery('.notification').html("<div id='note'>"+msg+"</div>");     
     setTimeout(function(){         
        jQuery(".notification").hide('slow');
      },1500);       
 } 

我将在这里添加整个Observer.php code:

I will add here the entire Observer.php code:

 public function addToCartEvent($observer)
{
    $request = Mage::app()->getFrontController()->getRequest();
    if (!$request->getParam('in_cart') && !$request->getParam('is_checkout'))
    {
        Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        $grandTotal = $quote->getGrandTotal();
        $subTotal = $quote->getSubtotal();
        $session = Mage::getSingleton('checkout/session');
        $shippingTaxamount = Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount');

        // get coupon discounted value            

        $totals = $quote->getTotals(); //Total object
        if (isset($totals['discount']) && $totals['discount']->getValue())
        {
            $discount = Mage::helper('core')->currency($totals['discount']->getValue()); //Discount value if applied
        }
        else
        {
            $discount = '';
        }

        //get discount value end     

        $xitems = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
        $max = 0;
        $lastItem = null;
        foreach ($xitems as $xitem)
        {
            if ($xitem->getId() > $max)
            {
                $max = $xitem->getId();
                $lastItem = $xitem;
                $attributeSetModel1 = Mage::getModel("eav/entity_attribute_set")->load($lastItem->getProduct()->getAttributeSetId());
                $attributeSetName1 = $attributeSetModel1->getAttributeSetName();
                $parentIds1 = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($lastItem->getProduct()->getId());
                if ($lastItem->getProduct()->getTypeId() == 'simple' && empty($parentIds1) && $attributeSetName1 == 'Default')
                {
                    $child = $lastItem;
                }
                else
                {
                    $child = $lastItem->getOptionByCode('simple_product')->getProduct();
                }
            }
        }



        $html = '';
        $productser = '';
        foreach ($session->getQuote()->getAllVisibleItems() as $item)
        {
            if ($lastItem)
            {
                $xproduct = $lastItem->getProduct();
                $xproductsku = $xproduct->getSku();
                $xproductname = $xproduct->getName();
                $xproductqty = $xproduct->getQty();
                $xproductprice = $xproduct->getPrice();
                $xproducturl = $xproduct->getUrl();
            }






            $productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
            if ($productOptions)
            {
                if (isset($productOptions['options']))
                {
                    foreach ($productOptions['options'] as $_option)
                    {
                        $productser = $_option['option_id'] . ',' . $_option['option_value'];
                    }
                }
                $superAttrString = '';
                if (isset($productOptions['info_buyRequest']['super_attribute']))
                {
                    foreach ($productOptions['info_buyRequest']['super_attribute'] as $key => $_superAttr)
                    {
                        $attr = Mage::getModel('catalog/resource_eav_attribute')->load($key);
                        $label = $attr->getSource()->getOptionText($_superAttr);
                        $superAttrString .= '<dt> ' . $attr->getAttributeCode() . ' </dt> <dd> ' . $label . ' </dd>';
                    }
                }
                if ($superAttrString):
                    $superAttrString . '&qty=1';
                endif;
            }

            $productid = $item->getId();
            $html .='<li id="li-' . $productid . '">';
            $product_id = $item->getProduct()->getId();
            $productsku = $item->getSku();
            $productname = $item->getName();
            $productqty = $item->getQty();
            $_product = Mage::getModel('catalog/product')->load($product_id);
            $url = Mage::getUrl(
                            'checkout/cart/deleteqty', array(
                        'id' => $productid,
                        Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => Mage::helper('core/url')->getEncodedUrl()
                            )
            );


            $count = $quote->getItemsCount();
            $html .='<div class="item-thumbnail">';
            if ($item->hasProductUrl()):
                $html .='<a href="' . $item->getUrl() . '" title="' . $item->getName() . '" class="product-image"><img src="' . Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail') . '" width="50" height="50" alt="' . $this->escapeHtml($item->getName()) . '" /></a>';
            else:
                $html .='<span><img src="' . Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail') . '" width="50" height="50" alt="' . $item->getName() . '" /></span>';
            endif;
            $html .='</div>';
            $html .='<div class="mini-basket-content-wrapper">
                    <div class="mini-cart-name">
                    <a class="item-name" href="' . $item->getUrl() . '">' . $productname . '</a> <span class="item-price"><span class="price">$' . $item->getPrice() . '</span></span>
            <div class="truncated_full_value">
    <dl class="item-options">
        <dd class="truncated">
            <div class="truncated_full_value">
                <dl class="item-options">
                    ' . $superAttrString . '
                </dl>
            </div>
        </dd>
    </dl>
 </div>

    <div class="product-cart-sku">SKU: # ' . $productsku . '</div>
        <div class="product-qty">Quantity: ' . $productqty . '</div>
                </div>  
               </div>
                    <div class="clearfix"></div>';
            $html .='</li>';
        }

        $_response = Mage::getModel('ajaxminicart/response')
                ->setProductName($observer->getProduct()->getName())
                ->setCarttotal($grandTotal)
                ->setCartsubtotal($subTotal)
                ->setCartcount($count)
                ->setDiscount($discount)
                ->setShippingtaxamount($shippingTaxamount)
                ->setCartitem($html)
                ->setMessage(Mage::helper('checkout')->__('%s was added into cart.', $observer->getProduct()->getName()));

        //append updated blocks

        $_response->addUpdatedBlocks($_response);
        $_response->send();
    }

    if ($request->getParam('is_checkout'))
    {
        Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
        $_response = Mage::getModel('ajaxminicart/response')
                ->setProductName($observer->getProduct()->getName())
                ->setMessage(Mage::helper('checkout')->__('%s was added into cart.', $observer->getProduct()->getName()));
        $_response->send();
    }
}

public function updateItemEvent($observer)
{
    $request = Mage::app()->getFrontController()->getRequest();
    if (!$request->getParam('in_cart') && !$request->getParam('is_checkout'))
    {
        Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        $grandTotal = $quote->getGrandTotal();
        $subTotal = $quote->getSubtotal();
        $shippingTaxamount = Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount');

        // get coupon discounted value            

        $totals = $quote->getTotals(); //Total object
        if (isset($totals['discount']) && $totals['discount']->getValue())
        {
            $discount = Mage::helper('core')->currency($totals['discount']->getValue()); //Discount value if applied
        }
        else
        {
            $discount = '';
        }

        //get discount value end                

        $_response = Mage::getModel('ajaxminicart/response')
                ->setCarttotal($grandTotal)
                ->setCartsubtotal($subTotal)
                ->setShippingtaxamount($shippingTaxamount)
                ->setDiscount($discount)
                ->setMessage(Mage::helper('checkout')->__('Item was updated'));

        //append updated blocks
        $_response->addUpdatedBlocks($_response);
        $_response->send();
    }

    if ($request->getParam('is_checkout'))
    {
        Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
        $_response = Mage::getModel('ajaxminicart/response')
                ->setMessage(Mage::helper('checkout')->__('Item was updated'));
        $_response->send();
    }
}

public function getConfigurableOptions($observer)
{

    $is_ajax = Mage::app()->getFrontController()->getRequest()->getParam('ajax');
    if ($is_ajax)
    {
        $_response = Mage::getModel('ajaxminicart/response');
        $_response = Mage::getModel('ajaxminicart/response');
        $product = Mage::registry('current_product');
        if (!$product->isConfigurable() && !$product->getTypeId() == 'bundle')
        {
            return false;
            exit;
        }

        //append configurable options block

        $_response->addConfigurableOptionsBlock($_response);
        $_response->send();
    }

    return;
}

public function getGroupProductOptions()
{

    $id = Mage::app()->getFrontController()->getRequest()->getParam('product');
    $options = Mage::app()->getFrontController()->getRequest()->getParam('super_group');
    if ($id)
    {

        $product = Mage::getModel('catalog/product')->load($id);
        if ($product->getData())
        {
            if ($product->getTypeId() == 'grouped' && !$options)
            {
                $_response = Mage::getModel('ajaxminicart/response');
                Mage::register('product', $product);
                Mage::register('current_product', $product);
                //add group product's items block
                $_response->addGroupProductItemsBlock($_response);
                $_response->send();
            }
        }
    }
}

感谢您

推荐答案

请尝试这种

Mage::getModel('ajaxminicart/response')    
   ->setProductName($observer->getProduct()->getName())  
   ->setMessage(Mage::helper('checkout')
   ->__('%s was added into cart. Description : %s', $observer->getProduct()->getName(),$observer->getProduct()->getDescription()));

下面列表可以被用于不同的产品属性

below list can be used for different product attributes

getShortDescription(); //product's short description
getDescription(); // product's long description
getName(); //product name
getPrice(); //product's regular Price
getSpecialPrice(); //product's special Price
getProductUrl(); //product url
getImageUrl(); //product's image url
getSmallImageUrl(); //product's small image url
getThumbnailUrl(); //product's thumbnail image url   

让我知道它的工作原理或不..关于

let me know it works or not.. regards

这篇关于如何添加AJAX的通知时,该产品已成功添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆