2

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

3 回答 3

1

在您的代码中:

'min_qty' => 1,

表示设置“物品状态变为缺货的数量”,因此如果与“数量”具有相同的值,则会显示“缺货”。增加数量或将此值减少到 0。

于 2013-02-06T15:45:53.257 回答
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();
于 2013-02-09T10:06:38.310 回答
0

您是否检查了项目状态为缺货的数量的后端设置:如果设置为大于 1,那么您的项目将显示缺货。您可以检查的另一件事

干杯

于 2013-05-30T00:26:15.300 回答