Tag Archives: Model(’catalog/product’)

Using Magento as a Checkout, Payment and Admin System

So, you want to use the Checkout, Payment and Admin facilities but not the catalog? Well here’s a quick and painless solution.

If you have a set list of products then build the product list in the Magento back office, a previous post covers creating Magento products dynamically.

Once you have the products, all that is required is to add the products to the cart. The simplest way to do this is to feed two variables into the cart, SKU and Quantity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
include_once '../app/Mage.php';
Mage::app();
 
Mage::getSingleton('core/session', array('name' => 'frontend'));
 
$sku = $_POST['sku'];
if (!isset($_POST['qty']) || (!is_int($_POST['qty']))) 
    { 
        $qty = 1; 
    } 
    else 
    {
        $qty = $_POST['qty']; 
    }
 
$productid = Mage::getModel('catalog/product')->getIdBySku($sku);
 
header("Location: /checkout/cart/add/product/".$productid."/qty/".$qty."/");

Your customer has then bypassed a Magento catalog but still checkouts using the Magento system and the admin can still make use of the excellent backoffice system.

Any comments or queries welcome.

Creating Magento Products On-The-Fly!

Sometimes it’s necessary to create products on-the-fly, typically in situations where there are so many product possibilities that even a configurable product isn’t flexible enough! In such situations the need arises to create products dynamically.

Below is the PHP code to produce a single Magento product and add it to the cart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 <?php
require_once 'app/Mage.php';
Mage::app();
 
// instatiate Product
$product = Mage::getModel('catalog/product');
 
$product->setWebsiteIds(array(1));
$product->setSku('rand-sku-' . rand());
$product->setPrice(rand(100,2000));
$product->setAttributeSetId(4); 
$product->setCategoryIds(array(3));
$product->setType('Simple Product');
$product->setName('Product Name'.rand(1,200000));
$product->setDescription('The Product Description');
$product->setShortDescription('Brief Description');
$product->setStatus(1);	
$product->setTaxClassId('2');
$product->setWeight(0);				
$product->setCreatedAt(strtotime('now'));
 
/* ADDITIONAL OPTIONS 
 
   $product->setCost();
   $product->setInDepth();
   $product->setKeywords();
 
*/
 
$product->save();
 
// "Stock Item" still required regardless of whether inventory
// control is used, or stock item error given at checkout!
 
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
 
header("Location: /checkout/cart/add/product/".$product->getId()."/); 
 
?>

This will automatically create a Magento product, with attributes and a price, add it to a number of categories, save the product, add the product to the cart and redirect the user to the Magento cart.

In a future post I will explore how to create multiple products using a set of classes and add all the products to the cart simultaneously.