简体   繁体   中英

how to add a product in cart from external php file in magento?

i've an external php file called addtocart.php who is located in my magento root folder. with this file i want to add a product to the cart. the product should have the following attributes:

sku name options price(!!) qty

EDIT: this is a screenshot of my magento folder structure: https://www.dropbox.com/s/vmli0973iflfski/Screenshot%202014-02-12%2009.44.39.png

this is my code: (only with qty and sku)

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


    $image = "uploads/52f7857f039b2.jpg";
    // the ID of the product
    $product_id  = 149;

    $product     = Mage::getModel('catalog/product')->load($product_id);



    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $params = array(
        'product' => $product_id,
        'qty' => 1,
        'options' => array(
            149 => array(
                    'quote_path' => $image,
                    'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . $image)), 0, 20)),
        )
    );

    $cart->addProduct($product, $params);
    $cart->save();


    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

it does not work! can anybody tell me how to do this? Thank you! :)

Hello check below code may be help you

$cart = Mage::getSingleton('checkout/cart'); 
                  $product = new Mage_Catalog_Model_Product();
                  $product->load($params['product']);

                  $cart->addProduct($product, $params);
                  $cart->save(); 
                  Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM