2

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

Murtuza Zabuawala
  • 14,814
  • 9
  • 44
  • 75
Mild Milly
  • 103
  • 1
  • 10

2 Answers2

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);
zhartaunik
  • 3,848
  • 4
  • 23
  • 52
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

Amit Bera
  • 77,456
  • 20
  • 123
  • 237