6
public function createcustomerproduct($data)
{
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $product = Mage::getModel('catalog/product');
    try {
        $LastProduct=Mage::getResourceModel('catalog/product_collection')->getLastItem();
        $custname = $this->getcustomerdata() ;
        $rnd = (int)$LastProduct->getId() + 1;
        $newsku = "$custname$rnd";

        $product
            ->setStoreId(0) //you can set data in store scope
            ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
            ->setAttributeSetId(47) //ID of a attribute set named 'default'
            ->setTypeId('simple') //product type
            ->setCreatedAt(strtotime('now')) //product creation time
            ->setSku($newsku) //SKU
            ->setName($newsku) //product name
            ->setWeight(4.0000)
            ->setStatus(1) //product status (1 - enabled, 2 - disabled)
            ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
            ->setDescription('This is a long description')
            ->setShortDescription('This is a short description')
            ->setSelected_category_id($data['selected_category_id'])
            ->setSelected_product_id($data['selected_product_id'])
            ->setselected_raid_hdd_qty($data['selected_raid_hdd_qty'])
            ->setPrice($data['totalamount']) //price in form 11.22
            ->setStockData(array(
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock' => 1, //manage stock
                'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                'is_in_stock' => 1, //Stock Availability
                'qty' => 999 //qty
            ))
            ->setCategoryIds(array(347)); //assign product to categories

            $product->save();

        $pr = Mage::getModel('catalog/product')->loadByAttribute('sku', $newsku);
        $id = $pr->getId();
        if ($id) {
            $cart = Mage::helper('checkout/cart')->getCart();
            $data['newproductid']   = $id;
            $data['newproductsku']  = $newsku;
            $data['newproductname'] = $newsku;

            $compid = $this->saveproductquote($data);
            if($compid) {
                return $id;
            }
        }
        return $compid;
    } catch(Exception $e){
        Mage::log($e->getMessage());
    }
}

// Add to cart Function
public function saveaddtocart($visitorid)
{
    $updatecustomer  = Mage::getModel('compatibility/compatibility')->updatecustomerid($visitorid);
    $cart            = Mage::helper('checkout/cart')->getCart();
    $customerid      = Mage::getSingleton('customer/session')->getId();
    $model           = Mage::getModel('compatibility/compatibility');
    try {
        $savequote   = Mage::getModel('compatibility/savedquote')->createcustomerproduct($updatecustomer);
        $pid         = (int)$savequote;
        $newproducts = Mage::getModel('catalog/product')->load($pid);
        $cart        = Mage::helper('checkout/cart')->getCart();
        $params      = array('product' => $id ,'qty' => 1);

        $request  = new Varien_Object();
        $request->setData($params);

        $cart->addProduct($newproducts, $request);
        $cart->save();
    }
    catch(Exception $e) {
       var_dump($e->getMessage());
    }
    exit();
}
  • have you assigned the product websites? – Hunter May 17 '17 at 9:45
  • Do you have any manual indexer enabled? e.g is your indexing triggered on save or not? If not, you'll need to trigger indexing before the product can be purchased. – Ricky Odin Matthews Sep 18 '17 at 15:49
0

Please try this:

<?php
$productId = '100'; 
$qty = '3';
$product = Mage::getModel('catalog/product')->load($productId);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
?>

Use your custom product's id and qty here.

  • thanks for your answer, I did that changes but its not working its give the same error. – Tush Jul 16 '16 at 11:31
  • Is your product created at the first place? – Twinkal Jul 16 '16 at 12:39
  • yes,product is created and immediately i am adding it to cart but getting exception as “The product could not be found.” – Tush Jul 16 '16 at 12:43
  • And product id also generated and this product also show in backend – Tush Jul 16 '16 at 12:56
  • I see you have just created the form key but you are not using it, also are you using the product id fetched from savedquote or the one created in backend? – Twinkal Jul 17 '16 at 5:56
0

Try changing below code

$newsku = "$custname$rnd";

to

$newsku = $custname.'-'.$rnd;

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.