简体   繁体   中英

How do i generate Remove from cart Url for a product ? (Magento)

是否可以为具有自定义功能的产品创建“从购物车移除”Url,以便我可以将该功能放在我的自定义块中,以便用户从购物车中删除商品...

What worked for me is going directly to the source:

add:

Mage::getUrl( 'checkout/cart/add', array( 'id' => $item->getId() ) );

edit:

Mage::getUrl( 'checkout/cart/configure', array( 'id' => $item->getId() ) );

delete:

Mage::getUrl( 'checkout/cart/delete', array( 'id' => $item->getId() ) );

If you examine, app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php and find the method: getDeleteUrl() will offer some insight on how its generated:

/**
 * Get item delete url
 *
 * @return string
 */
public function getDeleteUrl()
{
    return $this->getUrl(
        'checkout/cart/delete',
        array(
            'id'=>$this->getItem()->getId(),
            Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->helper('core/url')->getEncodedUrl()
        )
    );
}

You can find the call to this in template/checkout/cart/item/default.phtml:

<td class="a-center"><a href="<?php echo $this->getDeleteUrl()?>" title="<?php echo $this->__('Remove item')?>" class="btn-remove btn-remove2"><?php echo $this->__('Remove item')?></a></td>

With this info you should be able to create a new controller action for the cart to simply remove a specific ID to a product in the cart's index.

Hope this helps.

Where $_item is a Mage_Sales_Model_Quote_Item_Abstract object:

$renderer = new Mage_Checkout_Block_Cart_Item_Renderer();
$renderer->setItem($_item);
$renderer->getDeleteUrl();

The Simplest work out after combining different answers

Add this to the top of file lists.phtml(template/catlog/product)

<?php 
//getting cart count
$currentDelete = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
    foreach($quote->getAllItems() as $item){
        if($item->getProductId()){
            $currentDelete[$item->getProductId()] =  Mage::getUrl( 'checkout/cart/delete', array( 'id' => $item->getId() ) );
        }
    }
?>

After

<?php if($_product->isSaleable()): ?>

Add this

       <?php 
            if (isset($currentDelete) and array_key_exists($_product->getId(), $currentDelete)) { ?>
            <div class="curCart">
            <a href=" <?php echo $currentDelete[$_product->getId()]; ?>">Remove</a> 
            </div>
            <?php }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM