Magento Produkt in den Warenkorb von der Produktliste aus (add to cart from product list and redirect back)

Gepostet am Oktober 24, 2013

Ich möchte hier zeigen, wie man einen Warenkorbbutton in die Produkt Liste einfügt und zusätzlich nach dem Klicken zur Liste zurück geleitet wird.

Schritt 1 – button in list.phtml einfügen

1
2
3
4
5
6
7
8
9
10
11
<?php if ($_product->isSaleable() && !$this->hasOptions()):?>
<?php ######### CART BUTTON START ########## ?>
    <form action="http://kugellagerdirekt.schmidtmarcel.de/checkout/cart/add/product/<?php echo $_product->getId() ?>/" method="post">
        <div class="addtocart">
            <input type="text" name="qty" maxlength="12" value="1" title="Menge" class="input-text qty" />
            <input type="hidden" name="backtolist" value="<?php echo Mage::helper('core/url')->getCurrentUrl() ?>" />
            <button type="submit" title="In den Warenkorb" class="button btn-cart"><span><span>In den Warenkorb</span></span></button>
        </div>
    </form>
<?php ######### CART BUTTON END ########## ?>
<?php endif; ?>

Schritt 2 – cart controller überschreiben

Dazu müssen wir ein neues Modul anlegen:
app/code/local/M28visions/Rewrite
app/code/local/M28visions/Rewrite/controllers
app/code/local/M28visions/Rewrite/controllers/CartController.php
app/code/local/M28visions/Rewrite/etc
app/code/local/M28visions/Rewrite/etc/config.xml
app/etc/modules/M28visions_Rewrite.xml

Inhalt: app/code/local/M28visions/Rewrite/etc/config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0"?>
<config>
  <modules>
    <M28visions_Rewrite>
      <version>0.1.0</version>
    </M28visions_Rewrite>
  </modules>
  <frontend>
    <routers>
      <rewrite>
        <use>standard</use>
        <args>
          <module>M28visions_Rewrite</module>
          <frontName>rewrite</frontName>
        </args>
      </rewrite>
    </routers>
  </frontend>
  <global>
    <rewrite>
 
      <m28visions_rewrite1>
        <from>
          <![CDATA[#checkout/cart/add#]]>
        </from>
        <to>/rewrite/cart/add</to>
      </m28visions_rewrite1>
 
    </rewrite>
  </global>
</config>

Inhalt: app/code/local/M28visions/Rewrite/controllers/CartController.php
hier muss nur die Funktion „function addAction()“ aus der Originaldatei (app/code/code/checkout/controllers/CartController.php) kopiert und wie folgt modifiziert werden:

1
$this->_goBack();

in

1
2
3
4
5
6
//######## BACK TO LIST ########
if (isset($params['backtolist'])) {
    $this->_getSession()->getMessages(true);
    $this->getResponse()->setRedirect($params['backtolist']);
    return $this;
} else $this->_goBack();

Gesamt bei Magento 1.7.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
include_once "Mage/Checkout/controllers/CartController.php";
 
 
class M28visions_Rewrite_CartController extends Mage_Checkout_CartController
{
    public function addAction()
    {
        $cart   = $this->_getCart();
        $params = $this->getRequest()->getParams();
        try {
            if (isset($params['qty'])) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                    array('locale' => Mage::app()->getLocale()->getLocaleCode())
                );
                $params['qty'] = $filter->filter($params['qty']);
            }
 
            $product = $this->_initProduct();
            $related = $this->getRequest()->getParam('related_product');
 
            /**
             * Check product availability
             */
            if (!$product) {
                //######## BACK TO LIST ########
                if (isset($params['backtolist'])) {
                    $this->getResponse()->setRedirect($params['backtolist']);
                    return $this;
                } else $this->_goBack();
                return;
            }
 
            $cart->addProduct($product, $params);
            if (!empty($related)) {
                $cart->addProductsByIds(explode(',', $related));
            }
 
            $cart->save();
 
            $this->_getSession()->setCartWasUpdated(true);
 
            /**
             * @todo remove wishlist observer processAddToCart
             */
            Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );
 
            if (!$this->_getSession()->getNoCartRedirect(true)) {
                if (!$cart->getQuote()->getHasError()){
                    $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                    $this->_getSession()->addSuccess($message);
                }
                //######## BACK TO LIST ########
                if (isset($params['backtolist'])) {
                    $this->getResponse()->setRedirect($params['backtolist']);
                    return $this;
                } else $this->_goBack();
            }
        } catch (Mage_Core_Exception $e) {
            if ($this->_getSession()->getUseNotice(true)) {
                $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
                }
            }
 
            $url = $this->_getSession()->getRedirectUrl(true);
            if ($url) {
                $this->getResponse()->setRedirect($url);
            } else {
                $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
            }
        } catch (Exception $e) {
            $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
            Mage::logException($e);
            //######## BACK TO LIST ########
            if (isset($params['backtolist'])) {
                $this->getResponse()->setRedirect($params['backtolist']);
                return $this;
            } else $this->_goBack();
        }
    }
 
}

Schritt 3 – modul aktivieren

Inhalt: app/etc/modules/M28visions_Rewrite.xml

1
2
3
4
5
6
7
8
<config>
	<modules>
		<M28visions_Rewrite>
			<active>true</active>
			<codePool>local</codePool>
		</M28visions_Rewrite>
	</modules>
</config>

am Ende noch den Cache löschen

Tags: ,

Leave a Reply

 

Posts of Current Category



Facebook: