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

标签 php magento magento-1.7

我在 magento 1.7 中创建了可下载的产品。它创建成功,但在产品 View 页面上显示可用性:缺货。我必须从管理面板保存产品才能使其可用

我的代码如下

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

最佳答案

在您的代码中:

'min_qty' => 1,

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

关于php - 以编程方式创建显示缺货的可下载产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728314/

相关文章:

php - 从 php 表单操作输出 AJAX 响应的一部分

php - 如何阻止 PHP 在每次更新表单时替换变量?

php - 如何根据关联模型的值过滤分页结果? (CakePHP 2.x)

php跟踪随机条件

magento - Amazon.com的相关商品算法

configuration - Magento 1.7 : toolbar/pager. phtml : getShowAmounts/getShowPerPage-> Where can I configure this?

performance - 如何减少服务器响应时间?

magento - 在 Magento 电子商务上更改产品图片大小

magento - SQLSTATE[40001] : Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction by Magento position

magento - 以编程方式创建订单时如何使用折扣代码设置折扣金额?