How to get url to remove product from cart in Magento?

后端 未结 2 1066
刺人心
刺人心 2021-02-06 18:44

I looked at default/template/checkout/cart.html and found this code:

        getItems() as $_item): ?>
            

        
相关标签:
2条回答
  • 2021-02-06 19:12

    To get the return URL as well what I did is:

    In the Block

    protected function getDeleteUrl($item)
    {
        $params = array(
            'id'=>$item->getId(),
            Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => Mage::helper('core')->urlEncode($this->getUrl('checkout/cart'))
        );
        return $this->getUrl('checkout/cart/delete', $params);
    }
    

    In the PHTML

    <?php foreach($this->getItems() as $_item): ?>
        <?php echo $this->getDeleteUrl($_item) ?>
    <?php endforeach; ?>
    
    0 讨论(0)
  • 2021-02-06 19:13

    It seems that these are the questions, which I asked again & again (some 14 months back). Some basics need to be cleared now.

    In Checkout Cart, the main entity is the "Quote" (whose class is "Mage_Sales_Model_Quote") and it is not at all related to the "Catalog Product" entity (whose class is "Mage_Catalog_Model_Product"). So, products have their own unique (numeric) IDs, but when they get added to the Shopping Cart, their IDs are no more relevant in the Shopping Cart page (except for the Product URL, since the user may want to view the Product details page again).

    In the Checkout Cart page, the IDs available are specifically the Quote Item IDs for each of the items added there, and the Checkout Cart itself has a Quote ID, which is the parent of all the items added to the Cart during that particular Checkout Session.

    This Quote details can be found in the database table "sales_flat_quote", and its items can be found in "sales_flat_quote_items".

    Now coming back to the question, the view page which you are seeing is "default/template/checkout/cart.phtml" (There is no page with extension "html", it should be "phtml"). This is the view page of the whole Cart. Since you are interested in the items, so you should be looking at "default/template/checkout/cart/item/default.phtml". The Block class, which it refers to, is "Mage_Checkout_Block_Cart_Item_Renderer". This PHTML page serves for each item, so whatever you need to do for each item can be done in this page.

    To remove the Product from the Cart, you need to know that product's Quote Item ID, and write the following statement:-

    $this->getUrl('checkout/cart/delete', array('id' => 'xxxx'));
    

    where xxxx refers to the to-be-deleted product's Quote Item ID.

    To remove the Product from the Cart, in the Checkout Cart page, you need to call the following statement in "default/template/checkout/cart/item/default.phtml":-

    $this->getDeleteUrl();
    

    When this statement will get executed, it will take in that product's Quote Item ID from internal automatically (that's the beauty of Magento).

    To get the Product ID from the Cart, in the Checkout Cart page, you need to call the following statement in "default/template/checkout/cart/item/default.phtml":-

    $this->getProduct()->getId();
    

    The method "getProduct()" will return & load that Product's Model object (with all the details), which the next method "getId()" will use it for fetching that Product's ID.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题