Thursday, 5 September 2013

Add to Cart Not Working 404 error Custom .phtml

Add to Cart Not Working 404 error Custom .phtml

I am trying to figure out why my ItemController is not working properly. I
built a custom extension and custom .phtml file. I am trying to figure out
why I am getting a 404 error.
Here is the relevant code in my custom view.phtml file
<script type="text/javascript">
//<!CDATA[
var registryItemsForm = new Validation($('registry-items'));
var registryProductsCheckFlag = false;
function addSelectedGItemsToCart() {
var url = '<?php echo $this->getUrl('*/*/addselected'); ?>';
var cels = new Array();
$$('.items-checkbox').each(function(el){
if( $(el).checked ){
cels.push($(el).value);
}
});
var separator = (url.indexOf('?') >= 0) ? '&' : '?';
$$('#registry-items .qty').each(
function (input, index) {
for(var i=0; i<cels.length; i++){
if(input.name=='qty['+cels[i]+']'){
url += separator + input.name + '=' +
encodeURIComponent(input.value);
separator = '&';
}
}
}
);
setLocation(url);
}
//]]>
</script>
<div class="buttons-set">
<button type="button" title="<?php echo $this->__('Add Selected to
Cart') ?>" onclick="addSelectedGItemsToCart()" class="button
btn-add"><span><span><?php echo $this->__('Add Selected to Cart')
?></span></span></button>
</div>
Now this seems to be doing its job because when I get the 404 the url
looks like this
view/addselected/?qty[1]=3
However, my ItemController is not doing anything with it. Here is what I
have for that. Note, I have other code in this controller for update and
delete product and all works fine. I just can't add to cart.
<?php
class Beckin_Registry_ItemController extends
Mage_Core_Controller_Front_Action
{
protected function _getCart() {
return Mage::getSingleton('checkout/cart');
}
public function addselectedAction() {
$productIds = $this->getRequest()->getParam('qty');
$indexUrl = Mage::getUrl('registry');
if (!is_array($productIds)) {
$this->_redirectUrl($indexUrl);
return;
}
$cart = $this->_getCart();
foreach ($productIds as $k => $v) {
try {
$productId = $k;
$qty = $v;
if ($qty <= 0)
continue;
$product = $this->_getProduct($productId);
$cart->addProduct($product, $qty);
$message = $this->__('%s was successfully added to your
shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
unset($product);
} catch (Mage_Core_Exception $e) {
if
(Mage::getSingleton('checkout/session')->getUseNotice(true))
{
Mage::getSingleton('checkout/session')->addNotice($product->getName()
. ': ' . $e->getMessage());
} else {
Mage::getSingleton('checkout/session')->addError($product->getName()
..
': ' . $e->getMessage());
}
} catch (Exception $e) {
Mage::getSingleton('checkout/session')->addException($e,
$this->__('Can not add item to shopping cart.'));
}
}
$cart->save();
if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
$redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
} else if ($this->_getRefererUrl()) {
$redirectUrl = $this->_getRefererUrl();
} else {
$redirectUrl = $indexUrl;
}
$this->_redirectUrl($redirectUrl);
}
}

No comments:

Post a Comment