0

I have a singlepage (php) with any items (loaded in Magento), the idea is:

  1. The user access to singlepage.magentodomain.com.
  2. User press click on "buy now".
  3. User sees the magento checkout page, with item selected.

The HTML can contain the ID product and send it to Magento from HTTP verbs (POST, GET, etc.) or something else, but I not found information about this.

My Magento version is: 1.9.2.4.


Update (2016-03-9)

I found the next questions:

Based on this and @St0iK's answer, I developed the following:

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();

$_product = Mage::getModel('catalog/product')->load(11);
$formKey = Mage::getSingleton('core/session')->getFormKey();

The next code get the "submit url":

Option 1

$submitUrl = Mage::app()
                ->getLayout()
                ->createBlock('catalog/product_view')
                ->getSubmitUrl($_product);

Option 2

$submitUrl = Mage::helper('checkout/cart')->getAddUrl($_product);

HTML

<form action="<?php echo $submitUrl; ?>" method="post">
    <input type="hidden" name="related_product" id="related-products-field" value=""/>
    <input type="hidden" name="form_key" value="<?php echo $formKey; ?>">
    <input type="hidden" name="product" value="11">
    <input type="hidden" name="qty" value="1">
    <button type="submit">Buy now</button>
</form>

Request information (after submit)

Request information

  • Request URL: http://domain.dev/checkout/cart/add/uenc/aHR0cDovL21kci5kZXYvc2luZ2xlLnBocA,,/product/11/form_key/Md2JmzgTZLzWiX9l/
  • Request Method: POST
  • Status Code: 302 Found
  • Remote Address: 127.0.53.53:80
  • Response Headers
  • Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  • Connection: Keep-Alive
  • Content-Length: 0
  • Content-Type: text/html; charset=UTF-8
  • Date: Wed, 09 Mar 2016 16:21:29 GMT
  • Expires: Thu, 19 Nov 1981 08:52:00 GMT
  • Keep-Alive: timeout=5, max=100
  • Location: http://domain.dev/checkout/cart/
  • Pragma: no-cache
  • Server: Apache/2.4.12 (Ubuntu)
  • Set-Cookie: frontend=5umonnllqprpt57a6vd6lapvb2; expires=Wed, 09-Mar-2016 17:21:30 GMT; Max-Age=3600; path=/; domain=domain.dev; HttpOnly
  • X-Frame-Options: SAMEORIGIN
  • Request Headers
  • Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
  • Accept-Encoding: gzip, deflate
  • Accept-Language: es-ES,es;q=0.8,en;q=0.6
  • Cache-Control: no-cache
  • Connection: keep-alive
  • Content-Length: 59
  • Content-Type: application/x-www-form-urlencoded
  • Cookie: __atuvc=17%7C10; PHPSESSID=cn17rnrbf1h2va76edb1iqmoq5
  • Host: domain.dev
  • Origin: http://domain.dev
  • Pragma: no-cache
  • Referer: http://domain.dev/single.php
  • Upgrade-Insecure-Requests: 1
  • User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36
  • Form Data
  • related_product:
  • form_key: Md2JmzgTZLzWiX9l
  • product: 11
  • qty: 1
0

You can try something like this to build the 'Buy now' button for your product

<?php
$formKey = Mage::getSingleton('core/session')->getFormKey();
?>
<form action="/checkout/cart/add/product/<?php echo $productid; ?>" method="post">
<input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />
    <input type="text" name="qty"> QTY
    <input type="submit" value="Add to basket" />
</form>

You might need to load Mage.php first in your script.

<?php 
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

// Now you can run ANY Magento code you want
// You can find some information here. Get the form key and build the form

http://fishpig.co.uk/magento/tutorials/run-magento-code-externally/

  • I have the code working in my file single.php and it print the formkey and submit URL, but when I submit the form, the Magento page shows me "SHOPPING CART IS EMPTY". – Chofoteddy Mar 9 '16 at 16:23

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.