2

How to find Product URL in Magento database and how assign given URL for add to cart. please guide me.

2 Answers 2

1

Here is how magento create add-to-cart button:

<a href="<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>">Add to Cart</a>

You can't find url in database, because this url creates on fly, and depends on some base information, like form_key, product_id etc.

Here is an example how to get product URL:

require_once("app/Mage.php");
umask(0);
Mage::app();

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
Mage::init();

$product = Mage::getModel('catalog/product');
$product->load(6);
echo Mage::helper('checkout/cart')->getAddUrl($product);
0
1

There are no relation between magento product and addtocart url and magento did not save cart url in database.

Cart url generated by a logic at magento,Here helper function of checkout module is responsible for that

Function: Mage::helper('checkout/cart')->getAddUrl($_productObject). On this function product id and form key etc important.

  public function getAddUrl($product, $additional = array())
    {
        $routeParams = array(
            Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->_getHelperInstance('core')
                ->urlEncode($this->getCurrentUrl()),
            'product' => $product->getEntityId(),
            Mage_Core_Model_Url::FORM_KEY => $this->_getSingletonModel('core/session')->getFormKey()
        );

        if (!empty($additional)) {
            $routeParams = array_merge($routeParams, $additional);
        }

        if ($product->hasUrlDataObject()) {
            $routeParams['_store'] = $product->getUrlDataObject()->getStoreId();
            $routeParams['_store_to_url'] = true;
        }

        if ($this->_getRequest()->getRouteName() == 'checkout'
            && $this->_getRequest()->getControllerName() == 'cart') {
            $routeParams['in_cart'] = 1;
        }

        return $this->_getUrl('checkout/cart/add', $routeParams);
    }

see at class Mage_Checkout_Helper_Cart

1
  • Now i can understand thank you sir @AmitBera
    – Mild Milly
    Apr 27, 2015 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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