如何在产品成功添加到购物车时添加ajax通知

时间:2015-02-21 06:22:51

标签: php jquery ajax magento

我有Magento 1.9.1并且我安装了一个扩展程序,使用ajax自动将产品添加到购物车,产品成功添加到购物车后,此扩展程序显示通知:您的产品“NAME”已添加到购物车。我想用整个产品详细信息更改此通知,例如产品图片,sku,名称,描述......任何人都可以告诉我从哪里开始?我的通知功能如下所示:

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

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

 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();
            }
        }
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

请试试这个

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()));

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

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   

让我知道它是否有效..问候