本文整理汇总了PHP中Mage_Catalog_Model_Product::setStockData方法的典型用法代码示例。如果您正苦于以下问题:PHP Mage_Catalog_Model_Product::setStockData方法的具体用法?PHP Mage_Catalog_Model_Product::setStockData怎么用?PHP Mage_Catalog_Model_Product::setStockData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mage_Catalog_Model_Product
的用法示例。
在下文中一共展示了Mage_Catalog_Model_Product::setStockData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (isset($productData['categories']) && is_array($productData['categories'])) {
$product->setCategoryIds($productData['categories']);
}
if (isset($productData['websites']) && is_array($productData['websites'])) {
foreach ($productData['websites'] as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData['websites']);
}
if (Mage::app()->isSingleStoreMode()) {
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
}
if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
$product->setStockData($productData['stock_data']);
} else {
$product->setStockData(array('use_config_manage_stock' => 0));
}
if (isset($productData['tier_price']) && is_array($productData['tier_price'])) {
$tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api')->prepareTierPrices($product, $productData['tier_price']);
$product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api::ATTRIBUTE_CODE, $tierPrices);
}
/*
* Check if configurable product data array passed
*/
if (isset($productData['configurable_products_data']) && is_array($productData['configurable_products_data'])) {
$product->setConfigurableProductsData($productData['configurable_products_data']);
}
if (isset($productData['configurable_attributes_data']) && is_array($productData['configurable_attributes_data'])) {
foreach ($productData['configurable_attributes_data'] as $key => $data) {
//Check to see if these values exist, otherwise try and populate from existing values
$data['label'] = !empty($data['label']) ? $data['label'] : $product->getResource()->getAttribute($data['attribute_code'])->getStoreLabel();
$data['frontend_label'] = !empty($data['frontend_label']) ? $data['frontend_label'] : $product->getResource()->getAttribute($data['attribute_code'])->getFrontendLabel();
$productData['configurable_attributes_data'][$key] = $data;
}
$product->setConfigurableAttributesData($productData['configurable_attributes_data']);
$product->setCanSaveConfigurableAttributes(true);
}
/*
* Check if bundle product data, options and bundle items arrays passed
*/
if (isset($productData['bundle_items_data']) && isset($productData['options_data']) && is_array($productData['bundle_items_data']) && is_array($productData['options_data'])) {
$product->setBundleOptionsData($productData['options_data']);
$product->setBundleSelectionsData($productData['bundle_items_data']);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
Mage::register('product', $product);
// product must be registred in order to get the store_id, see _beforeSave() in Mage/Bundle/Model/Selection.php
}
}
示例2: saveAnimalAction
public function saveAnimalAction()
{
$request = $this->getRequest()->getPost();
$leiloeiro = Mage::getModel('leilao/leiloeiro')->getCurrentLeiloeiroSession();
$leiloeiro_product = Mage::getModel('leilao/product');
$product = new Mage_Catalog_Model_Product();
$product->setAttributeSetId(4);
// #4 is for default
$product->setTypeId('simple');
$product->setName($request['name']);
$product->setDescription($request['description']);
$product->setShortDescription('Short description here');
$product->setSku(time());
$product->setWeight(1.0);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
//4
$product->setPrice($request['price']);
// # Set some price
$product->setTaxClassId(0);
// # default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 1));
//$product->setCategoryIds(array(27)); // # some cat id's,
$product->setWebsiteIDs(array(1));
// # Website id, 1 is default
//Default Magento attribute
$product->setCreatedAt(strtotime('now'));
//print_r($product);
try {
$product->save();
$leiloeiro_product->setData('id_leiloeiro', $leiloeiro->getData('entity_id'));
$leiloeiro_product->setData('id_product', $product->getId());
$leiloeiro_product->save();
echo "Product Created";
} catch (Exception $ex) {
//Handle the error
echo "Product Creation Failed";
}
}
示例3: createProduct
public function createProduct(&$item, $asid)
{
$logFileName = Mage::getSingleton('core/session')->getEmailID();
$attributeSetModel = Mage::getModel('eav/entity_attribute_set');
$attributeSetModel->load($asid);
if (count($attributeSetModel->getData()) > 0) {
$p_status = (string) $item->isActive == 'Y' ? 1 : 2;
$p_taxclass = (string) $item->isTaxable == 'Y' ? 2 : 0;
$product = new Mage_Catalog_Model_Product();
$product->setTypeId('simple');
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
//New and created data code start
$format = 'Y-m-d H:i:s';
$catalogNewproductDays = Mage::getStoreConfig('catalog/newproduct/days', Mage::app()->getStore());
if (!empty($catalogNewproductDays) && $catalogNewproductDays >= 0) {
$currenDateTime = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
$new_from_date = date($format, strtotime('1 days' . $currenDateTime));
$new_to_date = date($format, strtotime($catalogNewproductDays . ' days' . $new_from_date));
$product->setNewsFromDate($new_from_date);
$product->setNewsToDate($new_to_date);
}
if ($product->getCreatedTime == NULL || $product->getUpdateTime() == NULL) {
$product->setCreatedTime($currenDateTime)->setUpdateTime($currenDateTime);
}
//New and created data code end
$product->setSku((string) $item->id);
//Product custom id
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
//Default website (main website) ?? To Do : make it dynamic
$product->setStoreIDs(array($this->_store_id));
// Default store id .
$inventory = $item->inventory;
$manageItem = (string) $inventory->manageStock;
$manageItem = strtoupper($manageItem);
if ($manageItem == 'Y' && strtoupper($inventory->allowBackorders) == 'Y') {
$product->setStockData(array('is_in_stock' => 1, 'qty' => $inventory->atp, 'manage_stock' => 1, 'use_config_backorders' => 0, 'backorders' => 1));
} elseif ($manageItem == 'Y') {
$product->setStockData(array('is_in_stock' => 1, 'qty' => $inventory->atp, 'use_config_backorders' => 0, 'manage_stock' => 1));
} elseif ($manageItem == 'N') {
$product->setStockData(array('use_config_backorders' => 0, 'manage_stock' => 0));
}
$product->setAttributeSetId($asid);
$product->setData('name', (string) $item->name);
$product->setPrice((double) $item->price);
$splAmt = (array) $item->specialPrice->amount;
if (isset($item->specialPrice->amount) && $item->specialPrice->amount != NULL) {
if (!empty($splAmt)) {
$product->setSpecialPrice((double) $item->specialPrice->amount);
}
//special price in form 11.22
}
$fromDate = (array) $item->specialPrice->fromDateTime;
if (isset($item->specialPrice->fromDateTime) && $item->specialPrice->fromDateTime != NULL) {
if (!empty($fromDate)) {
$product->setSpecialFromDate(Mage::helper('customimport')->getCurrentLocaleDateTime($item->specialPrice->fromDateTime));
}
//special price from (MM-DD-YYYY)
}
$toDate = (array) $item->specialPrice->toDateTime;
if (isset($item->specialPrice->toDateTime) && $item->specialPrice->toDateTime != NULL) {
if (!empty($toDate)) {
$product->setSpecialToDate(Mage::helper('customimport')->getCurrentLocaleDateTime($item->specialPrice->toDateTime));
}
//special price to (MM-DD-YYYY)
}
$product->setWeight((double) $item->weight);
$product->setStatus($p_status);
$product->setTaxClassId($p_taxclass);
$product->setDescription((string) $item->longDescription);
$product->setShortDescription((string) $item->shortDescription);
$product->setMetaTitle((string) $item->pageTitle);
$product->setMetaKeyword((string) $item->metaKeywords);
$product->setMetaDescription((string) $item->metaDescription);
$product->setExternalImage((string) $item->originalImageUrl);
$product->setExternalSmallImage((string) $item->largeImageUrl);
$product->setExternalThumbnail((string) $item->smallImageUrl);
$attributeValues = $item->attributeValues;
$attributeOcuurance = array();
//stores no. of occurance for all attributes
$configAttributeValue = array();
// will use to take value of attributes that ocuures once
$multiple_values = array();
// stores an array of available values
$i = 1;
$model = Mage::getModel('catalog/resource_eav_attribute');
foreach ($attributeValues->attribute as $attr) {
$loadedattr = $model->loadByCode('catalog_product', (string) $attr->id);
$attr_type = $loadedattr->getFrontendInput();
if (array_key_exists((string) $attr->id, $attributeOcuurance)) {
$multiple_values[(string) $attr->id][] = (string) $attr->valueDefId;
$attributeOcuurance[(string) $attr->id] = (int) $attributeOcuurance[(string) $attr->id] + 1;
if ($attr_type == 'text' || $attr_type == 'textarea') {
$multiple_values[(string) $attr->id][] = (string) $attr->value;
}
} else {
$multiple_values[(string) $attr->id][] = (string) $attr->valueDefId;
$attributeOcuurance[(string) $attr->id] = $i;
if ($attr_type == 'text' || $attr_type == 'textarea') {
$multiple_values[(string) $attr->id][] = (string) $attr->value;
}
//.........这里部分代码省略.........
示例4: CreateGeneralProducts
public function CreateGeneralProducts()
{
try {
$parent_category = Mage::getStoreConfig('oscommerceimportconf/mageconfiguration/conf_category', Mage::app()->getStore());
$storeId = Mage::getStoreConfig('oscommerceimportconf/mageconfiguration/conf_website', Mage::app()->getStore());
$AttributeSetId = Mage::getStoreConfig('oscommerceimportconf/mageconfiguration/conf_attribute', Mage::app()->getStore());
/////////////////////////////////////////////////////////////////////////////////////
// Create Custom Product for Import Shipping
/////////////////////////////////////////////////////////////////////////////////////
$pro_Model = 'osc-shipping';
$pro_Name = 'Shipping';
$oProduct = Mage::getModel("catalog/product")->getCollection()->setStoreId($storeId)->addAttributeToSelect("sku")->addFieldToFilter("sku", array('eq' => $pro_Model))->getFirstItem();
if (sizeof($oProduct->getData()) == 0) {
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku($pro_Model);
$product->setAttributeSetId($AttributeSetId);
$product->setTypeId('simple');
$product->setName($pro_Name);
$product->setCategoryIds($parent_category);
$product->setWebsiteIDs(array($storeId));
$product->setDescription('-');
$product->setShortDescription('-');
$product->setPrice(0);
$product->setWeight(0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(1);
$product->setTaxClassId(0);
# My default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 10000));
$product->setCreatedAt(strtotime('now'));
$product->save();
}
/////////////////////////////////////////////////////////////////////////////////////
// Create Custom Product for Import Discount
/////////////////////////////////////////////////////////////////////////////////////
$pro_Model = 'osc-discount';
$pro_Name = 'Discount';
$oProduct = Mage::getModel("catalog/product")->getCollection()->setStoreId($storeId)->addAttributeToSelect("sku")->addFieldToFilter("sku", array('eq' => $pro_Model))->getFirstItem();
if (sizeof($oProduct->getData()) == 0) {
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku($pro_Model);
$product->setAttributeSetId($AttributeSetId);
$product->setTypeId('simple');
$product->setName($pro_Name);
$product->setCategoryIds($parent_category);
$product->setWebsiteIDs(array($storeId));
$product->setDescription('-');
$product->setShortDescription('-');
$product->setPrice(0);
$product->setWeight(0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(1);
$product->setTaxClassId(0);
# My default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 10000));
$product->setCreatedAt(strtotime('now'));
$product->save();
}
/////////////////////////////////////////////////////////////////////////////////////
// Create Custom Product for Import Taxes
/////////////////////////////////////////////////////////////////////////////////////
$pro_Model = 'osc-tax';
$pro_Name = 'Tax';
$oProduct = Mage::getModel("catalog/product")->getCollection()->setStoreId($storeId)->addAttributeToSelect("sku")->addFieldToFilter("sku", array('eq' => $pro_Model))->getFirstItem();
if (sizeof($oProduct->getData()) == 0) {
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku($pro_Model);
$product->setAttributeSetId($AttributeSetId);
$product->setTypeId('simple');
$product->setName($pro_Name);
$product->setCategoryIds($parent_category);
$product->setWebsiteIDs(array($storeId));
$product->setDescription('-');
$product->setShortDescription('-');
$product->setPrice(0);
$product->setWeight(0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(1);
$product->setTaxClassId(0);
# My default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 10000));
$product->setCreatedAt(strtotime('now'));
$product->save();
}
/////////////////////////////////////////////////////////////////////////////////////
// Create Custom Product for Import Other
/////////////////////////////////////////////////////////////////////////////////////
$pro_Model = 'osc-other';
$pro_Name = 'Other Charges';
$oProduct = Mage::getModel("catalog/product")->getCollection()->setStoreId($storeId)->addAttributeToSelect("sku")->addFieldToFilter("sku", array('eq' => $pro_Model))->getFirstItem();
if (sizeof($oProduct->getData()) == 0) {
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku($pro_Model);
$product->setAttributeSetId($AttributeSetId);
$product->setTypeId('simple');
$product->setName($pro_Name);
//.........这里部分代码省略.........
示例5: _prepareDataForSave
/**
* Set additional data before product save
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
*/
protected function _prepareDataForSave($product, $productData)
{
if (isset($productData['stock_data'])) {
if (!$product->isObjectNew() && !isset($productData['stock_data']['manage_stock'])) {
$productData['stock_data']['manage_stock'] = $product->getStockItem()->getManageStock();
}
$this->_filterStockData($productData['stock_data']);
} else {
$productData['stock_data'] = array('use_config_manage_stock' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1);
}
$product->setStockData($productData['stock_data']);
// save gift options
$this->_filterConfigValueUsed($productData, array('gift_message_available', 'gift_wrapping_available'));
if (isset($productData['use_config_gift_message_available'])) {
$product->setData('use_config_gift_message_available', $productData['use_config_gift_message_available']);
if (!$productData['use_config_gift_message_available'] && $product->getData('gift_message_available') === null) {
$product->setData('gift_message_available', (int) Mage::getStoreConfig(Mage_GiftMessage_Helper_Message::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $product->getStoreId()));
}
}
if (isset($productData['use_config_gift_wrapping_available'])) {
$product->setData('use_config_gift_wrapping_available', $productData['use_config_gift_wrapping_available']);
if (!$productData['use_config_gift_wrapping_available'] && $product->getData('gift_wrapping_available') === null) {
$xmlPathGiftWrappingAvailable = 'sales/gift_options/wrapping_allow_items';
$product->setData('gift_wrapping_available', (int) Mage::getStoreConfig($xmlPathGiftWrappingAvailable, $product->getStoreId()));
}
}
if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
$product->setWebsiteIds($productData['website_ids']);
}
// Create Permanent Redirect for old URL key
if (!$product->isObjectNew() && isset($productData['url_key']) && isset($productData['url_key_create_redirect'])) {
$product->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']);
}
/** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
//Unset data if object attribute has no value in current store
if (Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID !== (int) $product->getStoreId() && !$product->getExistsStoreValueFlag($attribute->getAttributeCode()) && !$attribute->isScopeGlobal()) {
$product->setData($attribute->getAttributeCode(), false);
}
if ($this->_isAllowedAttribute($attribute)) {
if (isset($productData[$attribute->getAttributeCode()])) {
$product->setData($attribute->getAttributeCode(), $productData[$attribute->getAttributeCode()]);
}
}
}
}
示例6: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (property_exists($productData, 'categories') && is_array($productData->categories)) {
$product->setCategoryIds($productData->categories);
}
if (property_exists($productData, 'websites') && is_array($productData->websites)) {
foreach ($productData->websites as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData->websites);
}
if (Mage::app()->isSingleStoreMode()) {
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
}
if (property_exists($productData, 'stock_data')) {
$_stockData = array();
foreach ($productData->stock_data as $key => $value) {
$_stockData[$key] = $value;
}
} else {
$_stockData = array('use_config_manage_stock' => 0);
}
$product->setStockData($_stockData);
if (property_exists($productData, 'tier_price')) {
$tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api_V2')->prepareTierPrices($product, $productData->tier_price);
$product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api_V2::ATTRIBUTE_CODE, $tierPrices);
}
}
示例7: addProduct
public function addProduct($data)
{
$product = new Mage_Catalog_Model_Product();
// Build the product
$setId = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($this->typeId)->addFilter('attribute_set_name', $this->attributeSet['name'])->getFirstItem()->getId();
// firstItem becouse of filter ($this->typeId) and filter ('attribute_set_name') design single attribute set.
$product->setSku($data['sku']);
$product->setAttributeSetId($setId);
# 9 is for default
$product->setTypeId('simple');
$product->setName($data['name']);
$product->setWebsiteIDs(array(1));
//only array!!!!! # Website id, 1 is default
$product->setStoreIDs(array(0, 1));
$product->setDescription($data['description']);
$product->setShortDescription($data['short_description']);
$product->setPrice($data['price']);
# Set some price
$product->setWeight($data['weight']);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setTaxClassId(0);
# default tax class
if ($catId = $this->getCategoryIdByName($this->categoryName)) {
$product->setCategoryIds(array($catId));
# some cat id's,
}
$product->setStockData(array('is_in_stock' => 1, 'qty' => $data['qty']));
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
echo "product created. ID: {$product->getId()} \n";
} catch (Exception $ex) {
zend_debug::dump($ex->getMessage());
}
}
示例8: addProduct
//.........这里部分代码省略.........
$product->setTypeId('simple');
$product->setStatus(1);
$product->setWebsiteIDs(array(1));
$product->setCategoryIds($row['category_ids']);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setPrice($row['price']);
$product->setData('store', $row['store']);
//$product->setData('websites', $row['websites']);
$product->setData('attribute_set', $row['attribute_set']);
$product->setData('type', $row['type']);
$product->setData('sku', $row['sku']);
$product->setData('has_options', $row['has_options']);
$product->setData('name', $row['name']);
$product->setData('meta_title', $row['meta_title']);
$product->setData('meta_description', $row['meta_description']);
$product->setData('url_key', $row['url_key']);
$product->setData('url_path', $row['url_path']);
$product->setData('options_container', $row['options_container']);
$product->setData('extra_title', $row['extra_title']);
$product->setData('frame_gender', $row['frame_gender']);
$product->setData('frame_shape', $row['frame_shape']);
$product->setData('cost', $row['cost']);
$product->setData('weight', $row['weight']);
$product->setData('manufacturer', $row['manufacturer']);
//$product->setData('status', $row['status']);
$product->setData('tax_class_id', $row['tax_class_id']);
//$product->setData('visibility', $row['visibility']);
$product->setData('enable_googlecheckout', $row['enable_googlecheckout']);
$product->setData('is_imported', $row['is_imported']);
$product->setData('frame_type', $row['frame_type']);
//$product->setData('sell_by_phone_only', $row['sell_by_phone_only']);
$product->setData('description', $row['description']);
$product->setData('short_description', $row['short_description']);
$product->setData('meta_keyword', $row['meta_keyword']);
$product->setData('special_from_date', $row['special_from_date']);
$product->setData('min_qty', $row['min_qty']);
$product->setData('use_config_min_qty', $row['use_config_min_qty']);
$product->setData('is_qty_decimal', $row['is_qty_decimal']);
$product->setData('backorders', $row['backorders']);
$product->setData('use_config_backorders', $row['use_config_backorders']);
$product->setData('min_sale_qty', $row['min_sale_qty']);
$product->setData('use_config_min_sale_qty', $row['use_config_min_sale_qty']);
$product->setData('max_sale_qty', $row['max_sale_qty']);
$product->setData('use_config_max_sale_qty', $row['use_config_max_sale_qty']);
$product->setData('low_stock_date', $row['low_stock_date']);
$product->setData('notify_stock_qty', $row['notify_stock_qty']);
$product->setData('use_config_notify_stock_qty', $row['use_config_notify_stock_qty']);
$product->setData('manage_stock', $row['manage_stock']);
$product->setData('use_config_manage_stock', $row['use_config_manage_stock']);
$product->setData('stock_status_changed_automatically', $row['stock_status_changed_automatically']);
$product->setData('use_config_qty_increments', $row['use_config_qty_increments']);
$product->setData('qty_increments', $row['qty_increments']);
$product->setData('use_config_enable_qty_increments', $row['use_config_enable_qty_increments']);
$product->setData('enable_qty_increments', $row['enable_qty_increments']);
$product->setData('product_name', $row['product_name']);
$product->setData('store_id', $row['store_id']);
$product->setData('product_type_id', $row['product_type_id']);
$product->setData('product_status_changed', $row['product_status_changed']);
$product->setData('product_changed_websites', $row['product_changed_websites']);
$product->setData('custom_design', $row['custom_design']);
$product->setData('page_layout', $row['page_layout']);
$product->setData('image_label', $row['image_label']);
$product->setData('small_image_label', $row['small_image_label']);
$product->setData('thumbnail_label', $row['thumbnail_label']);
$product->setData('gift_message_available', $row['gift_message_available']);
$product->setData('framecolour', $row['framecolour']);
$product->setData('special_price', $row['special_price']);
$product->setData('is_recurring', $row['is_recurring']);
$product->setData('frame_size', $row['frame_size']);
$product->setData('custom_layout_update', $row['custom_layout_update']);
$product->setData('special_to_date', $row['special_to_date']);
$product->setData('news_from_date', $row['news_from_date']);
$product->setData('news_to_date', $row['news_to_date']);
$product->setData('custom_design_from', $row['custom_design_from']);
$product->setData('custom_design_to', $row['custom_design_to']);
$product->setData('stock_clearance', $row['stock_clearance']);
$product->setData('lenstint', $row['lenstint']);
$product->setData('framesizetest', $row['framesizetest']);
$product->setStockData(array('is_in_stock' => $row['is_in_stock'], 'qty' => $row['qty']));
$product->setIsMassupdate(false);
try {
$product->save();
//Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds(array($productId));
//Mage::register('current_product', $product);
//$product = null;
} catch (Exception $ex) {
echo 'test\\n';
echo $ex->getMessage();
//Handle the error
}
}
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
$indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price');
if ($indexProcess) {
$indexProcess->reindexAll();
}
echo date("\nY-d-m H:i:s\n");
}
示例9: addProduct
public function addProduct($data)
{
$connection = Mage::getModel('core/resource')->getConnection('core_read');
$product = new Mage_Catalog_Model_Product();
$product->setAttributeSetId(Mage::getModel('eav/entity_attribute_set')->getCollection()->setEntityTypeFilter($this->typeId)->addFieldToFilter('attribute_set_name', $data['attribute_set_id'])->getFirstItem()->getAttributeSetId());
unset($data['attribute_set_id']);
$product->setWebsiteIDs(array(1));
//// assign product to the default website $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
foreach ($data as $key => $value) {
switch ($key) {
case 'is_in_stock':
case 'qty':
case 'media_gallery':
case 'related':
case 'upsell':
case 'crosssell':
case 'options':
break;
case 'visibility':
switch ($value) {
case 'Not Visible Individually':
$val = 1;
break;
case 'Catalog':
$val = 2;
break;
case 'Search':
$val = 3;
break;
case 'Catalog, Search':
$val = 4;
break;
default:
$val = '';
}
$product->setData($key, $val);
break;
case 'status':
switch ($value) {
case 'Enabled':
$val = 1;
break;
case 'Disabled':
$val = 2;
break;
default:
$val = '';
}
$product->setData($key, $val);
break;
default:
in_array($key, $this->attrsWithOpts) ? $product->setData($key, $this->optByCode($key, $value)) : $product->setData($key, $value);
}
}
//unset($value);
$product->setStockData(array('is_in_stock' => $data['is_in_stock'], 'qty' => $data['qty']));
$product->setIsMassupdate(false);
$product->setExcludeUrlRewrite(true);
try {
$product->save();
//Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds(array($productId));
# ----------- set media gallery ---------------- #
// TODO: move folder Media need fix.
$product = Mage::getModel('catalog/product')->load($product->getId());
$product->setMediaGallery(array('images' => array(), 'values' => array()));
foreach ($data['media_gallery']['images'] as $img) {
$mediaGalleryData['images'][] = array('file' => $img['file'], 'position' => $img['position'], 'label' => $img['label'], 'disabled' => $img['disabled']);
$product->setData('media_gallery', $mediaGalleryData);
/*if (!is_null($mediaAttribute)) {
Mage::('catalog/product_attribute_backend_media')->setMediaAttribute($product, $mediaAttribute, $fileName);
}*/
}
$product->save();
# ----------- set custom options ---------------- #
$options = array();
$sku = $data['sku'];
$i = 0;
foreach ($data['options'] as $opt) {
$options[$i][$sku] = array('title' => $opt['describe']['title'], 'type' => $opt['describe']['type'], 'is_require' => $opt['describe']['is_require'], 'sort_order' => $opt['describe']['sort_order'], 'values' => array());
if (!empty($opt['values'])) {
foreach ($opt['values'] as $val) {
$options[$i][$sku]['values'][] = array('title' => $val['title'], 'price' => $val['price'], 'price_type' => empty($val['price_type']) ? 'fixed' : $val['price_type'], 'sku' => $val['sku'], 'sort_order' => $val['sort_order']);
}
}
++$i;
}
foreach ($options as $option) {
foreach ($option as $sku => $opt1) {
$product = Mage::getModel('catalog/product')->load($product->getId());
if (!$product->getOptionsReadonly()) {
$product->setProductOptions(array($opt1));
$product->setCanSaveCustomOptions(true);
try {
$product->save();
} catch (Exception $e) {
echo $e->getMessage();
}
}
Mage::getSingleton('catalog/product_option')->unsetOptions();
}
//.........这里部分代码省略.........
示例10: date
// Build the product
$product->setSku($sku);
$product->setAttributeSetId($setId);
# 9 is for default
$product->setTypeId('simple');
$product->setName('Lens 1');
$product->setCategoryIds(array(3));
# some cat id's,
$product->setWebsiteIDs(array(0, 1));
//only array!!!!! # Website id, 1 is default
$product->setStoreIDs(array(0, 1));
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99);
# Set some price
$product->setWeight(4.0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setTaxClassId(0);
# default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 99999));
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
echo "product created";
} catch (Exception $ex) {
zend_debug::dump($ex->getMessage());
}
echo date("\nY-d-m H:i:s\n");
?>
示例11: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (isset($productData['categories']) && is_array($productData['categories'])) {
$product->setCategoryIds($productData['categories']);
}
if (isset($productData['websites']) && is_array($productData['websites'])) {
foreach ($productData['websites'] as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData['websites']);
}
if (Mage::app()->isSingleStoreMode()) {
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
}
if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
$product->setStockData($productData['stock_data']);
} else {
$product->setStockData(array('use_config_manage_stock' => 0));
}
if (isset($productData['tier_price']) && is_array($productData['tier_price'])) {
$tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api')->prepareTierPrices($product, $productData['tier_price']);
$product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api::ATTRIBUTE_CODE, $tierPrices);
}
}
示例12: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (property_exists($productData, 'categories') && is_array($productData->categories)) {
$product->setCategoryIds($productData->categories);
}
if (property_exists($productData, 'websites') && is_array($productData->websites)) {
foreach ($productData->websites as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData->websites);
}
if (property_exists($productData, 'stock_data') && is_array($productData->stock_data)) {
$product->setStockData($productData->stock_data);
}
}
示例13: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (property_exists($productData, 'categories') && is_array($productData->categories)) {
$product->setCategoryIds($productData->categories);
}
if (property_exists($productData, 'websites') && is_array($productData->websites)) {
foreach ($productData->websites as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData->websites);
}
if (Mage::app()->isSingleStoreMode()) {
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
}
if (property_exists($productData, 'stock_data')) {
$_stockData = array();
foreach ($productData->stock_data as $key => $value) {
$_stockData[$key] = $value;
}
$product->setStockData($_stockData);
}
}
示例14: createConfigurable
function createConfigurable($sku, $name, $optionId, $simpleProducts, $price, $brandId, $ageGroupId, $dimensions, $numPlayersId, $gameTime, $minNumPlayers, $shortDesc, $desc, $image1, $image2, $image3, $categoryId, $subCategoryId, $weight, $numPieces)
{
try {
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku($sku);
$product->setAttributeSetId('13');
# 9 is for default
$product->setTypeId('configurable');
$product->setName($name);
$product->setCategoryIds(array(25));
# some cat id's,
$product->setWebsiteIDs(array(1));
# Website id, 1 is default
$product->setDescription($desc);
$product->setShortDescription($shortDesc);
$product->setPrice($price);
# Set some price
//Default Magento attribute
//$product->setWeight($weight);
echo '<br>' . $weight . '<br>';
//$product->setWeight(0.2000);
$product->setWeight(4.0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0);
# default tax class
$product->setStockData(array('is_in_stock' => 1, 'qty' => 99999));
$product->setCreatedAt(strtotime('now'));
$product->setCategoryIds(array($categoryId, $subCategoryId));
$product->setMetaDescription($shortDesc);
$mediaAttribute = array('thumbnail', 'small_image', 'image');
$imagePath1 = './media/import/' . $image1;
$imagePath2 = './media/import/' . $image2;
$imagePath3 = './media/import/' . $image3;
//echo $imagePath1.'<br>'.$imagePath2.'<br>'.$imagePath3;die();
if (!file_exists($imagePath1) && $imagePath1 != '') {
if (file_exists('./media/import.bak/' . $image1)) {
exec('cp media/import.bak/' . $image1 . ' media/import');
$product->addImageToMediaGallery('./media/import/' . $image1, $mediaAttribute, true, false);
} else {
echo 'ERROR: file ' . $imagePath1 . ' does not exist. SKU is: ' . $sku;
}
} elseif ($imagePath1 != '') {
$product->addImageToMediaGallery($imagePath1, $mediaAttribute, true, false);
}
if (!file_exists($imagePath2) && $imagePath2 != '') {
if (file_exists('./media/import.bak/' . $image2)) {
exec('cp media/import.bak/' . $image2 . ' media/import');
$product->addImageToMediaGallery('./media/import/' . $image2, null, true, false);
} else {
echo 'ERROR: file ' . $imagePath2 . ' does not exist. SKU is: ' . $sku;
}
} elseif ($imagePath2 != '') {
$product->addImageToMediaGallery($imagePath2, null, true, false);
}
if (!file_exists($imagePath3) && $imagePath3 != '') {
if (file_exists('./media/import.bak/' . $image3)) {
exec('cp media/import.bak/' . $image3 . ' media/import');
$product->addImageToMediaGallery('./media/import/' . $image3, null, true, false);
} else {
echo 'ERROR: file ' . $imagePath3 . ' does not exist. SKU is: ' . $sku;
}
} elseif ($imagePath3 != '') {
$product->addImageToMediaGallery($imagePath3, null, true, false);
}
//custom attribute for gift wrapping
} catch (Exception $exMain) {
echo 'configurable main exception:<br><br>' . $exMain . '<br><br>';
}
try {
$product->save();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$configId = $_product->getId();
$resource = Mage::getSingleton('core/resource');
$readCon = $resource->getConnection('core_read');
$writeCon = $resource->getConnection('core_write');
$table = 'catalog_product_super_attribute';
if (!$writeCon) {
die('Could not connect: ' . mysql_error());
}
//set gift wrap attribute: super attribute id, configurable product id (auto-increment), normal attribute id, position (always 0 for one configurable option)
$query = 'insert into catalog_product_super_attribute (product_id, attribute_id) values (' . $configId . ', 142 )';
$writeCon->query($query);
//i think this has to be set to create the associated products: configurable product id, associated simple product id ...repeat as necessary (twice in this case)
$simpleId1 = $simpleProducts[0]->getId();
$simpleId2 = $simpleProducts[1]->getId();
$query = 'insert into catalog_product_relation values (' . $configId . ', ' . $simpleId1 . ' )';
$writeCon->query($query);
$query = 'insert into catalog_product_relation values (' . $configId . ', ' . $simpleId2 . ' )';
$writeCon->query($query);
//set the link id between the configurable product and the associated simple product: link id, simple product, configurable product
$query = 'insert into catalog_product_super_link (product_id, parent_id) values (' . $simpleId1 . ', ' . $configId . ')';
$writeCon->query($query);
$query = 'insert into catalog_product_super_link (product_id, parent_id) values (' . $simpleId2 . ', ' . $configId . ')';
$writeCon->query($query);
//get the superattribute id for the next write query
$query = 'select product_super_attribute_id from catalog_product_super_attribute where product_id=' . $configId;
$superAttrId = $readCon->fetchOne($query);
$query = 'insert into catalog_product_super_attribute_pricing (product_super_attribute_id, value_index, is_percent, pricing_value, website_id) values (' . $superAttrId . ', 91, 0, 5.000, 0)';
//.........这里部分代码省略.........
示例15: _prepareDataForSave
/**
* Set additional data before product saved
*
* @param Mage_Catalog_Model_Product $product
* @param array $productData
* @return object
*/
protected function _prepareDataForSave($product, $productData)
{
if (isset($productData['categories']) && is_array($productData['categories'])) {
$product->setCategoryIds($productData['categories']);
}
if (isset($productData['websites']) && is_array($productData['websites'])) {
foreach ($productData['websites'] as &$website) {
if (is_string($website)) {
try {
$website = Mage::app()->getWebsite($website)->getId();
} catch (Exception $e) {
}
}
}
$product->setWebsiteIds($productData['websites']);
}
if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
$product->setStockData($productData['stock_data']);
}
}