Category Archives: Magento

How To Generate Magento Coupon Code Programmatically

How can I create or auto generate Magento coupon codes programmatically.

First, you need to create a sales rule or load an existing sales rule and then use “salesrule/coupon_massgenerator” model to create the coupons.

$generator = Mage::getModel('salesrule/coupon_massgenerator');

$data = array(
    'max_probability'   => .25,
    'max_attempts'      => 10,
    'uses_per_customer' => 1,
    'uses_per_coupon'   => 1,
    'qty'               => 2, //number of coupons to generate
    'length'            => 10, //length of coupon string
    'to_date'           => '2017-9-11', //ending date of generated promo
    'format'          => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
    'rule_id'         => 1//the id of the rule you will use as a template
);

$generator->validateData($data);
$generator->setData($data);
$generator->generatePool();

How Can I Reindex A Magento Index Programmatically

How can I reindex a Magento index programmatically, like what I did via command line – php indexer.php –reindex catalog_product_price

You need to use model – “index/process” and load the index by ID, then call “reindexAll” function


$process = Mage::getModel('index/process')->load(2);

// 1 - catalog_product_attribute
// 2 - catalog_product_price
// 3 - catalog_url
// 4 - catalog_product_flat
// 5 - catalog_category_flat
// 6 - catalog_category_product
// 7 - catalogsearch_stock
// 8 - cataloginventory_stock
// 9 - tag_summary

$process->reindexAll();

How To Add Magento Product To Cart Programmatically

How can I add a Magento product to current cart programmatically

You need to use checkout/cart and catalog/product models to add Magento product to the current cart

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$options = array('1'=>'3','2'=>'6'); // product options
$product = Mage::getModel('catalog/product')->load(2); // product id - 2

$paramater = array('product' => '2', // product id - 2
    'qty' => '1',
    'form_key' => Mage::getSingleton('core/session')->getFormKey(),
    'options' => $options 
);       

$request = new Varien_Object();
$request->setData($paramater);
$cart->addProduct($product, $request);
$cart->save();

How Can I Set Canonical URL For Magento CMS Page

How can I set canonical url for magento cms page manually from Magento backend

You can set canonical url from design tab of CMS page.

* add/edit a CMS page
* click design tab
* then add follow codes to the field of Layout Update XML

<reference name=”head”>
    <action method=”addLinkRel”>
        <rel>canonical</rel>
        <href>http://softwarehtec.com/</href>
    </action>
</reference>

How To Add Magento Bundle To Current Cart Programmatically

Currently, I am building my own extension, I would like to know how to add Magento bundle product to current cart programmatically

The following is example of how to add a Magento bundle product to current cart programmatically

$params = array(
    'product' => 1,
    'related_product' => null,
    'bundle_option' => array( // your bundle option
        2=> 4,
        2=> array(
            0 => 5,
        ),
        13 => array(
            0 => 8,
            1 => 9,
        ),
    ),
    'options' => array( // your custom option
        2 => 'Test',
    ),
    'qty' => 2,
);
 
$cart = Mage::getSingleton('checkout/cart');
 
$product = new Mage_Catalog_Model_Product();
$product->load(1);
 
$cart->addProduct($product, $params);
$cart->save();
 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
 

Magento Error – Failed To Open Stream: No Such File

After installed my new Magento, I have got error like failed to open stream : No such file or directory

Warning: include_once(Mage/Core/functions.php): failed to open stream: No such file or directory in /public_html/app/Mage.php on line 49

Warning: include_once(): Failed opening ‘Mage/Core/functions.php’ for inclusion (include_path=’/public_html/app/code/local:/public_html/app/code/community:/public_html/app/code/core:/public_html/lib:.:/usr/lib/php:/usr/local/lib/php’) in /public_html/app/Mage.php on line 49

Warning: include(Mage/Core/Model/App.php): failed to open stream: No such file or directory in /public_html/lib/Varien/Autoload.php on line 94

Warning: include(Mage/Core/Model/App.php): failed to open stream: No such file or directory in /public_html/lib/Varien/Autoload.php on line 94

Warning: include(): Failed opening ‘Mage/Core/Model/App.php’ for inclusion (include_path=’/public_html/app/code/local:/public_html/app/code/community:/public_html/app/code/core:/home/hoytoynl/public_html/lib:.:/usr/lib/php:/usr/local/lib/php’) in /public_html/lib/Varien/Autoload.php on line 94

Fatal error: Class ‘Mage_Core_Model_App’ not found in /public_html/app/Mage.php on line 66

To solve this Magento error,
1. double check those files exist on your server
2. clear cache
3. check those files’ permission is readable