2

I currently have a website where users can design their products, then check out. Checking out redirects to the main Magento website, where the product is added to their cart (the custom product exists in our Catalog with custom options). However, I'm not able to update the cart after redirecting from the external site. It redirects to the cart, but the item is not listed in the cart.

As I understand it, I need to pass information to Magento from the external site, where Magento can then take this information and add the product to the cart. I'm not sure how or where to send this information, though.

I have tried adding through URL by redirecting an HTTP form, as in:

$cart_url = "site.com/checkout/cart/add/product=" . $product_id . "&qty=1";
<form method="post" name="cart_form" action="<?php echo $cart_url; ?>">
    <input> product id</input>
    <input> custom option 1</input>
   ....
</form>

However this doesn't seem to update the cart. Looking around on the net has shown that this no longer works in Magento 1.8+, and I'll need to create either an Observer or other Controller.

How do I create an observer in Magento that I can pass the form data from an external site to in order to add to the cart? I'm fairly new to Magento, so I'm not entirely sure how to link the bits together.

2

You will have to create a custom module in magento.

Create a file app/etc/MyExtension_AddProductFromUrl.xml

<config>
    <modules>
        <MyExtension_AddProductFromUrl>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout/>
            </depends>
        </MyExtension_AddProductFromUrl>
    </modules>
</config>

Create a file app/code/local/MyExtension/AddProductFromUrl/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyExtension_AddProductFromUrl>
            <version>0.1.0</version>
        </MyExtension_AddProductFromUrl>
    </modules>

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <MyExtension_AddProductFromUrl before="Mage_Checkout">MyExtension_AddProductFromUrl</MyExtension_AddProductFromUrl>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Create a file app/code/local/MyExtension/AddProductFromUrl/controllers/CartController.php

<?php
    require_once 'Mage/Checkout/controllers/CartController.php';
    class MyExtension_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
        # overloaded addAction
        public function addAction() {        
            // generate form_key if missing or invalid
            if (!($formKey = $this->getRequest()->getParam('form_key', null)) || $formKey != Mage::getSingleton('core/session')->getFormKey()) {
                $this->getRequest()->setParams(array('form_key' =>Mage::getSingleton('core/session')->getFormKey()));
            }        

            // do parent actions
            parent::addAction();
        }
    }
?>

Also see

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.