Question:

can not add simple products through vendor panel

David: 5 days ago

I am unable to add products through Vendor panel.

the error i am getting after submitting the product data is.

Warning: Invalid argument supplied for foreach()  in /var/www/html/abc/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1180

#0 /var/www/html/abc/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1180): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/html/p...', 1180, Array)
#1 /var/www/html/abc/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1123): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /var/www/html/abc/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 /var/www/html/abc/app/code/local/Webkul/Marketplace/Model/Product.php(26): Mage_Core_Model_Abstract->save()
#4 /var/www/html/abc/app/code/local/Webkul/Marketplace/controllers/MarketplaceaccountController.php(16): Webkul_Marketplace_Model_Product->saveSimpleNewProduct(Array)
#5 /var/www/html/abc/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Webkul_Marketplace_MarketplaceaccountController->simpleproductAction()
#6 /var/www/html/abc/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('simpleproduct')
#7 /var/www/html/abc/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#8 /var/www/html/abc/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#9 /var/www/html/abc/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#10 /var/www/html/abc/index.php(82): Mage::run('', 'store')
#11 {main}

Here is my function for save products

model/products.php:

public function saveSimpleNewProduct($wholedata){
        $cats=array();
        foreach($wholedata['category'] as $keycat){
            array_push($cats,$keycat);
        }
        if($wholedata['status']==1 && isset($wholedata['wstoreids']) ){
            $status=1; 
            $stores=$wholedata['wstoreids'];
        }
        else{       
            $status=Mage::getStoreConfig('marketplace/marketplace_options/product_approval')? 2:1;
            $stores=Mage::app()->getStore()->getStoreId();
        }
        Mage::setIsDeveloperMode(true);
        $magentoProductModel = Mage::getModel('catalog/product');
        $magentoProductModel->setData($wholedata);
        $saved=$magentoProductModel->save();
        $magentoProductModel = Mage::getModel('catalog/product')->load($saved->getId());
        $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
        $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies(); 
        $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
        if($wholedata['special_price']){
            $special_price = $wholedata['special_price']/$rates[$currentCurrencyCode];
            $magentoProductModel->setSpecialPrice($special_price);
        }
        $price = $wholedata['price']/$rates[$currentCurrencyCode];
        $magentoProductModel->setPrice($price);
        $magentoProductModel->setWeight($wholedata['weight']);
        $magentoProductModel->setStoresIds(array($stores));
        $storeId = Mage::app()->getStore()->getId();
        $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
        $magentoProductModel->setCategoryIds($cats);
        $magentoProductModel->setStatus($status);
        $saved=$magentoProductModel->save();
        $lastId = $saved->getId();
        $this->_saveStock($lastId,$wholedata['stock'],$wholedata['is_in_stock']);

        $wholedata['id'] = $lastId;
        Mage::dispatchEvent('mp_customoption_setdata', $wholedata);

        $collection1=Mage::getModel('marketplace/product');
        $collection1->setmageproductid($lastId);
        $collection1->setuserid($wholedata['userid']);
        $collection1->setstatus($status);
        $collection1->save();
        if(isset($_FILES) && count($_FILES) > 0){
            if($wholedata['type_id']=='simple'){
                foreach($_FILES as $image ){
                    if($image['tmp_name'] != ''){
                        if (($error = $this->uploadImage1($image, $lastId)) !== true){
                            $errors[] = $error;
                        }
                    }
                }
            }               
        }
        $this->_addImages($lastId,$wholedata['defaultimage']);

        Mage::dispatchEvent('mp_customattribute_settierpricedata', $wholedata);

        return $lastId;     
    }

and here is my Marketplace controller:

public function simpleproductAction() {
        if($this->getRequest()->isPost()){ 
            list($data, $errors) = $this->validatePost();
            $wholedata=$this->getRequest()->getParams();
            if(empty($errors)){     
                Mage::getModel('marketplace/product')->saveSimpleNewProduct($wholedata);
                $status=Mage::getStoreConfig('marketplace/marketplace_options/partner_approval');
                if($status==1){
                    $customer = Mage::getModel('customer/customer')->load($wholedata['userid']);
                    $cfname=$customer->getFirstname()." ".$customer->getLastname();
                    $cmail=$customer->getEmail();
                    $catagory_model = Mage::getModel('catalog/category');
                    $categoryname="";                
                    foreach($wholedata['category'] as $key)
                    {
                       $categoriesy = $catagory_model->load($key);
                       $categoryname=$categoryname ."  ". $categoriesy->getName().", ";
                    }
                    $emailTemp = Mage::getModel('core/email_template')->loadDefault('approveproduct');
                    $emailTempVariables = array();
                    $userModel = Mage::getModel('admin/user');
                     $userCollection = $userModel->getCollection()->load(); 
                    foreach ($userCollection as  $value) {
                        $emailTempVariables['myvar1'] = $wholedata['name'];
                        $emailTempVariables['myvar2'] =$categoryname;
                        $emailTempVariables['myvar3'] = $value->getName();
                        $processedTemplate = $emailTemp->getProcessedTemplate($emailTempVariables);
                        $emailTemp->setSenderName($cfname);
                        $emailTemp->setSenderEmail($cmail);
                        $emailTemp->send( $value->getEmail(),$value->getName(),$emailTempVariables);
                        }
                }
            }else{
                foreach ($errors as $message) {Mage::getSingleton('core/session')->addError($message);}
                $_SESSION['new_products_errors'] = $data;
            }
            if (empty($errors))
                Mage::getSingleton('core/session')->addSuccess(Mage::helper('marketplace')->__('Your product was successfully Saved'));
                $this->_redirect('marketplace/marketplaceaccount/simpleproduct/');
        }
        else{
            $this->loadLayout( array('default','marketplace_account_simpleproduct'));
            $this->_initLayoutMessages('customer/session');
            $this->_initLayoutMessages('catalog/session');
            $this->getLayout()->getBlock('head')->setTitle( Mage::helper('marketplace')->__('MarketPlace Product Type: Simple Product'));
            $this->renderLayout();
        }

Answer:
Luca: 5 days ago

Its hard to find in which line error is.Here is little tip for over come this issue using try{}catch(){} (http://php.net/manual/en/language.exceptions.php) blocks e.g.

try{
    .....
    // your code block goes here 
    ........

} catch(Exception $e) {
    echo $e->getMessage();
}

And always test your input data before using it in foreach. Suggestions:

if (is_array($values) || is_object($values))
{
    foreach ($values as $value)
    {
        ...
    }
}

Hope this will help you.