ダウンロード可能な製品を magento 1.7 で作成しました。正常に作成されましたがAvailability: Out of stock
、製品ビュー ページに表示されます。製品を利用できるようにするには、管理パネルから製品を保存する必要があります
私のコードは以下の通りです
Mage::getSingleton("core/session", array("name" => "frontend"));
$storeId = Mage::app()->getStore()->getId(); // get store id
$filePath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
try {
$product = Mage::getModel('catalog/product');
$product->setStoreId($storeId);
$product->setWebsiteIds(array(
Mage::app()->getStore($storeId)->getWebsiteId()));
$product->setAttributeSetId(4);
$product->setHasOptions(4);
$product->setTypeId('downloadable');
$product->setSku(date('YmdHis'));
$product->setPrice('1.23');
$product->setStatus(1);
$product->setVisibility(4);
$product->setTaxClassId(0);
$product->setStockData(array('is_in_stock'=>0, '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($album_name."-".date('ymdis'));
$product->setMetaTitle($desc);
$product->setMetaDescription($desc);
$product->setLinksTitle("Download");
$product->setStockData(array(
'use_config_manage_stock' => 1,
'qty' => 1,
'min_qty' => 0,
'use_config_min_qty' => 0,
'min_sale_qty' => 0,
'use_config_min_sale_qty' => 0,
'max_sale_qty' => 0,
'use_config_max_sale_qty' => 1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 0,
'is_in_stock' => 1
));
$linkfile = array();
$samplefile = array();
$_highfilePath = "/highresolution/".$album_name."/" . $fname;
$_samplefilePath = "/lowresolution/".$album_name."/" . $fname;
$paths = array('highurl' => $_highfilePath, 'sampleurl' => $_samplefilePath);
$samplefile[] = array(
'file' => $_samplefilePath,
'name' => $fname,
'size' => $files['size'][0],
'status' => 'new'
);
$linkfile[] = array(
'file' => $_highfilePath,
'name' => $fname,
'size' => $files['size'][0],
'status' => 'new'
);
$tmpBasePath = Mage::getBaseDir('media') . DS . 'highresolution' . DS . $album_name;
$tmpSampleBasePath = Mage::getBaseDir('media') . DS . 'lowresolution' . DS . $album_name;
$BashPathUrl = $filePath.'highresolution/'.$album_name.'/'.$fname;
$SamplePathUrl = $filePath.'lowresolution/'.$album_name.'/'.$fname;
$product->addImageToMediaGallery($tmpSampleBasePath. DS. $fname, array ('image','small_image','thumbnail'), false, false);
$product->save();
$linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
Mage_Downloadable_Model_Link::getBaseTmpPath(),
Mage_Downloadable_Model_Link::getBasePath(),
$linkfile
);
$linkModel = Mage::getModel('downloadable/link')->setData(array(
'product_id' => $product->getId(),
'sort_order' => 0,
'number_of_downloads' => 0, // Unlimited downloads
'is_shareable' => 2, // Not shareable
'link_url' => '',
'link_type' => 'file',
'link_file' => json_encode($linkfile),
'sample_url' => $SamplePathUrl,
'sample_file' => json_encode($samplefile),
'sample_type' => 'url',
'use_default_title' => false,
'title' => 'downloadable link',
'default_price' => 0,
'price' => 0,
'store_id' => 0,
'website_id' => $product->getStore()->getWebsiteId(),
));
$linkModel->setLinkFile($linkFileName)->save();
return $product->getProductUrl();
} catch (Exception $e) {
echo "Exception : ".$e->getMessage();
exit;
}