0

我使用 mage.php 添加产品,一切正常,但数量(数量)始终设置为 0,我想将其设置为 20。我使用库 mage.php,这是我的代码。

require_once 'app/Mage.php';
Mage::app();
$product = Mage::getModel('catalog/product');

$product->setWebsiteIds(array(1));
$product->setSku('rand-sku-' . rand());
$product->setPrice(rand(100,2000));
$product->setAttributeSetId(4);
$product->setCategoryIds(array(3));
$product->setType('simple');
$product->setName('Product Name'.rand(1,200000));
$product->setDescription('The Product Description');
$product->setShortDescription('Brief Description');
$product->setStatus(1);
$product->setTaxClassId('1');
$product->setWeight(0);
$product->setCreatedAt(strtotime('now'));



$product->save();

$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('stock_id', 1);
$stockItem->setData('store_id', 1);
$stockItem->setData('manage_stock', 1);
$stockItem->setData('use_config_manage_stock', 0);
$stockItem->setData('min_sale_qty', 0);
$stockItem->setData('use_config_min_sale_qty', 0);
$stockItem->setData('max_sale_qty', 1000);
$stockItem->setData('use_config_max_sale_qty', 0);
$stockItem->setData('qty', 20);

$stockItem->save();
4

1 回答 1

4

以这种方式使用它会完美地工作

require_once('app/Mage.php');
Mage::app();

$product = Mage::getModel('catalog/product');


$product->setSku('pro11');
$product->setAttributeSetId(9);
$product->setTypeId('simple');
$product->setName('Product title');
$product->setCategoryIds(array(7));
$product->setWebsiteIDs(array(1));
$product->setDescription('Product Full description');
$product->setShortDescription('Product Short description');
$product->setPrice(250.00);
$product->setWeight(30.00);
$product->setVisibility(4);
$product->setStatus(1);
$product->setTaxClassId(0);
$product->setStockData(array(
   'is_in_stock' => 1,
    'qty' => 20
));
$product->setCreatedAt(strtotime('now'));
$product->save();

希望这个帮助!

于 2012-09-25T13:49:44.527 回答