以编程方式创建可下载的产品,显示缺货

时间:2013-02-06 11:47:27

标签: php magento magento-1.7

我在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;
    }

3 个答案:

答案 0 :(得分:1)

在您的代码中:

'min_qty' => 1,

表示设置“项目状态变为缺货数量”,因此如果该值与“数量”相同,则显示“缺货”。增加数量或将此值减少到0。

答案 1 :(得分:1)

我改变了这个并且它正在运作

首先删除此代码

$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
    ));  

然后添加以下代码

$product->save();
$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();

答案 2 :(得分:0)

您是否检查过项目状态变为缺货数量的后端设置:如果设置为超过1,那么您的商品将显示缺货。你还可以检查另一件事

干杯