3

I use this code to create and add an item into the cart.

Here it is:

<?php
    error_reporting(E_ALL | E_STRICT);
    $mageFilename = 'app/Mage.php';
    require_once $mageFilename;
    $app = Mage::app('default'); 
    Mage::app();

Mage::getSingleton('core/session', array('name' => 'frontend'));

    ini_set('display_errors', 1);

$TITLE = $_POST["TITLE"];
$SKU = $_POST["SKU"];
$MANUFACTURER = $_POST["MANUFACTURER"];
$RNUMBER = substr(number_format(time() * rand(),0,'',''),0,4);
$SKUNUMBER = "$SKU-$RNUMBER";
$DESCRIPTION = $_POST["DESCRIPTION"];
$SHORTDESCRIPTION = $_POST["SHORTDESCRIPTION"];
$IMAGE = $_POST["IMAGE"];
$PRICE = $_POST["PRICE"];
$COUNT = $_POST["COUNT"];
$PRODUCTLINK = $_POST["PRODUCTLINK"];
$PRODUCTCOLOR = $_POST["PRODUCTCOLOR"];
$SizeOfProduct = $_POST["SizeOfProduct"];
if($SizeOfProduct == ""){
$SizeProduct = $_POST["SizeOfProductSD"];
}
else {
$SizeProduct = $_POST["SizeOfProduct"];
}

if($SizeProduct == ""){
$SizeProduct = "Само един е размера";
}

$ADDEDPRICE = $_POST["ADDEDPRICE"];

    $api = new Mage_Catalog_Model_Product_Api();

    $attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
    $attribute_sets = $attribute_api->items();

    $productData = array(); 
    $productData['website_ids'] = array(1); 
    $productData['categories'] = array(2,3);

    $productData['status'] = 1;

    $productData['name'] = utf8_encode($TITLE);
    $productData['description'] = $DESCRIPTION;
    $productData['short_description'] = "$SHORTDESCRIPTION<br>Посочен размер: <b>$SizeProduct</b></b>";
    $productData['ext_image'] = $IMAGE;
    $productData['product_link'] = $PRODUCTLINK;
    $productData['product_color'] = $PRODUCTCOLOR;
    $productData['product_size'] = $SizeProduct;
    $productData['product_add_price'] = $ADDEDPRICE;
    $productData['url_key'] = $SKU;

    $productData['price'] = $PRICE;
    $productData['weight'] = 0.50;
    $productData['tax_class_id'] =2;
    $productData['page_layout'] ='one_column';

    $new_product_id = $api->create('simple',$attribute_sets[0]['set_id'],$SKUNUMBER,$productData);


    $stockItem = Mage::getModel('cataloginventory/stock_item');
    $stockItem->loadByProduct( $new_product_id );

    $stockItem->setData('use_config_manage_stock', 1);
    $stockItem->setData('qty', 10);
    $stockItem->setData('min_qty', 3);
    $stockItem->setData('use_config_min_qty', 1);
    $stockItem->setData('min_sale_qty', 3);
    $stockItem->setData('use_config_max_sale_qty', 1);
    $stockItem->setData('max_sale_qty', 0);
    $stockItem->setData('use_config_max_sale_qty', 1);
    $stockItem->setData('is_qty_decimal', 0);
    $stockItem->setData('backorders', 0);
    $stockItem->setData('notify_stock_qty', 0);
    $stockItem->setData('is_in_stock', 1);
    $stockItem->setData('tax_class_id', 0);

    $stockItem->save();



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

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

$hm1 = array('SignProduct.php/');
$ym1   = array('');
$adress = str_replace($hm1, $ym1, $adress);

header('Location: '.$adress.'');


?>

Take a look: enter image description here

All items that i include in the cart by this script has selected quantity of 1.

How can i add an item into the cart with selected quantity of 3 for example ?

Thanks in advance!

2

First of all you have used the following line twice please remove second one

 $stockItem->setData('use_config_max_sale_qty', 1);

Now set

 $stockItem->setData('use_config_max_sale_qty', 3);

let me know if you have any query

3

I used this code to make it all work:

    Mage::getSingleton('core/session', array('name' => 'frontend'));
    try {
        $product_id = $new_product_id; // Replace id with your product id
        $qty = $PRODUCTQTY; // Replace qty with your qty
        $product = Mage::getModel('catalog/product')->load($product_id);
        $cart = Mage::getModel('checkout/cart');
        $cart->init();
        $cart->addProduct($product, array('qty' => $qty));
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        Mage::getSingleton('core/session')->addSuccess('Product added successfully');
        header('Location: ' . 'index.php/checkout/cart/');
    } catch (Exception $e) {
        echo $e->getMessage();
    }

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.