如何以编程方式添加可下载产品并在magento中以网格视图显示?

时间:2014-03-12 07:23:06

标签: php magento

我正在以编程方式添加可下载产品。它保存在数据库中,但未显示在网格目录中 - >管理产品。但是当我从管理员手动保存时,它会显示。所以我必须制作代码以在网格中显示产品。 在这里输入代码

$fname=Mage::getSingleton('customer/session')->getCustomer()->getName();
            $attr_set_id=27;
            $tot_price=2222;
            $desc="test";
            $link="/f/l/test.zip";
            $storeId = Mage::app()->getStore()->getId();

            $filePath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

            $arrXmlChildTag=array();
            $postdata = file_get_contents("php://input"); 
            $xml=simplexml_load_string($postdata);
           // $link=trim($xml->Product->DownloadableLink);

            $orderxml = new ExSimpleXMLElement("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?> 
                    <simpleProducts></simpleProducts>");  

            $product = Mage::getModel('catalog/product');//->getCollection()->addAttributeToSelect('*');
            //var_dump(get_class_methods($product));die;

            $product->setStoreId($storeId);
            $product->setWebsiteIds(array(Mage::app()->getStore($storeId)->getWebsiteId()));

            $product->setAttributeSetId($attr_set_id);
            $product->setHasOptions(1);
            $product->setTypeId('downloadable');
            $product->setSku("custombooks_".date('ymdis'));
            $product->setPrice($tot_price);
            $product->setStatus(1);
            $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
            $product->setStockData(array('is_in_stock'=>1, 'qty' => 1));
            $product->setLinksPurchasedSeparately(0);
            $product->setEnableGooglecheckout(0);
            $product->setIsImported(0);
            $product->setLinksExist(false);
            $product->setDescription($desc);
            $product->setShortDescription($desc); //added, meta description to 'short description' field, you can change this value
            $product->setMetaKeyword($desc);
            $product->setCustomLayoutUpdate(NULL);
            $product->setName("Custombooks_".date('ymdis'));
            $product->setMetaTitle($desc);
            $product->setMetaDescription($desc);
            $product->setLinksTitle("Download");
            $product->setCreatedAt(strtotime('now'));      
            $product->setMsrp(2);
            $product->setMsrpDisplayActualPriceType(4);

            $product->save();

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

            Mage::unregister('product');
            Mage::register('product', $products);
            $products->setTaxClassId(2);
            $product->isSalable("true"); 
            try{
                $products->save();    
            }
            catch(Exception $e)
            {
                echo $e;
            }

            $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
            $stockItemId = $stockItem->getId();
            $stock = array();
            if (!$stockItemId)
            {
                $stockItem->setData('product_id', $product->getId());
                $stockItem->setData('stock_id', 1);
            } 
            else
            {
                $stock = $stockItem->getData();
            }
            $stockItem->setIsInStock(1);
            $stockItem->save();
            $stockStatus = Mage::getModel('cataloginventory/stock_status')->load($product->getId());
            $stockStatus->updateStatus($product->getId());

0 个答案:

没有答案