/** * Return Google shopping item ID * * @param Mage_Catalog_Model_Product $product * @param integer $storeId ID of store in which product was published; null for current store * @return string|null ID or null if no such item * @throws RuntimeException If Mage_GoogleShopping is disabled */ public function getItemId(Mage_Catalog_Model_Product $product, $storeId = null) { $this->_checkIsActive(); if ($storeId === null) { $storeId = Mage::app()->getStore()->getId(); } return Mage::getModel('googleshopping/item')->loadByProduct($product->setStoreId($storeId))->getGcontentItemId(); }
/** * @magentoConfigFixture current_store catalog/price/scope 1 * @magentoConfigFixture current_store currency/options/base GBP */ public function testAfterSave() { $product = new Mage_Catalog_Model_Product(); $product->load(1); $product->setOrigData(); $product->setPrice(9.99); $product->setStoreId(0); $this->_model->setScope($this->_model->getAttribute()); $this->_model->afterSave($product); $this->assertEquals('9.99', $product->getResource()->getAttributeRawValue($product->getId(), $this->_model->getAttribute()->getId(), Mage::app()->getStore()->getId())); }
/** * Setup product for quote item * * @param Mage_Catalog_Model_Product $product * @return Mage_Sales_Model_Quote_Item */ public function setProduct($product) { if ($this->getQuote()) { $product->setStoreId($this->getQuote()->getStoreId()); $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); } $this->setData('product', $product)->setProductId($product->getId())->setProductType($product->getTypeId())->setWeight($this->getProduct()->getWeight())->setTaxClassId($product->getTaxClassId())->setBaseCost($product->getCost())->setIsRecurring($product->getIsRecurring())->setIsQtyDecimal(false); if (!Vikont_ARIOEM_Helper_Cart::isOemProduct($product) || !$this->hasData('sku')) { $this->setSku($this->getProduct()->getSku())->setName($product->getName()); } Mage::dispatchEvent('sales_quote_item_set_product', array('product' => $product, 'quote_item' => $this)); return $this; }
/** * Setup product for quote item * * @param Mage_Catalog_Model_Product $product * @return Mage_Sales_Model_Quote_Item */ public function setProduct($product) { if ($this->getQuote()) { $product->setStoreId($this->getQuote()->getStoreId()); $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); } $this->setData('product', $product)->setProductId($product->getId())->setProductType($product->getTypeId())->setSku($this->getProduct()->getSku())->setName($product->getName())->setWeight($this->getProduct()->getWeight())->setTaxClassId($product->getTaxClassId())->setBaseCost($product->getCost())->setIsRecurring($product->getIsRecurring()); if ($product->getStockItem()) { $this->setIsQtyDecimal($product->getStockItem()->getIsQtyDecimal()); } Mage::dispatchEvent('sales_quote_item_set_product', array('product' => $product, 'quote_item' => $this)); // if ($options = $product->getCustomOptions()) { // foreach ($options as $option) { // $this->addOption($option); // } // } return $this; }
public function importProducts($products, $storeId, $store) { Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); if (!empty($products)) { foreach ($products as $_product) { $update = false; if ($_product["code"]) { $sku = $_product["code"]; } elseif ($_product["code2"]) { $sku = $_product["code2"]; } else { $sku = $_product["code3"]; } $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); if (!$product) { $product = Mage::getModel('catalog/product')->load($_product["productID"]); if (!$product->getName()) { $product = new Mage_Catalog_Model_Product(); $product->setId($_product["productID"]); Mage::helper('Erply')->log("Creating new product: " . $_product["productID"]); } else { Mage::helper('Erply')->log("Editing old product: " . $_product["productID"]); $update = true; } } else { $update = true; } if ($_product["displayedInWebshop"] == 0) { if ($update) { try { $product->delete(); Mage::helper('Erply')->log("Delete existing product which should be in webshop id: " . $_product["productID"] . " - sku: " . $sku); } catch (Exception $e) { Mage::helper('Erply')->log("Failed to delete product with message: " . $e->getMessage()); } } continue; } $product->setStoreId($storeId); // product does not exist so we will be creating a new one. $product->setIsMassupdate(true); $product->setExcludeUrlRewrite(true); $product->setTypeId('simple'); $product->setWeight(1.0); $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); $product->setStatus(1); $product->setSku($sku); $product->setTaxClassId(0); // set the rest of the product information here that can be set on either new/update if (!$update) { $product->setAttributeSetId((int) Mage::getStoreConfig('eepohs_erply/product/attribute_set', $storeId)); // the product attribute set to use } $product->setName($_product["name"]); $category = Mage::getModel('catalog/category')->load($_product["groupID"]); if ($category->getName()) { $product->setCategoryIds(array($_product["groupID"])); // array of categories it will relate to } if (Mage::app()->isSingleStoreMode()) { $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsiteId())); } else { $product->setWebsiteIds(array($store->getWebsiteId())); } $product->setBatchPrices(array()); $product->setStockPriorities(array()); //$product->setPrice($_product["price"]); // set the product images as such // $image is a full path to the image. I found it to only work when I put all the images I wanted to import into the {magento_path}/media/catalog/products - I just created my own folder called import and it read from those images on import. // $image = '/path/to/magento/media/catalog/products/import/image.jpg'; // // $product->setMediaGallery (array('images'=>array (), 'values'=>array ())); // $product->addImageToMediaGallery ($image, array ('image'), false, false); // $product->addImageToMediaGallery ($image, array ('small_image'), false, false); // $product->addImageToMediaGallery ($image, array ('thumbnail'), false, false); // setting custom attributes. for example for a custom attribute called special_attribute // special_attribute will be used on all examples below for the various attribute types //$product->setSpecialAttribute('value here'); // setting a Yes/No Attribute // $product->setSpecialField(1); // setting a Selection Attribute //$product->setSpecialAttribute($idOfAttributeOption); //specify the ID of the attribute option, eg you creteated an option called Blue in special_attribute it was assigned an ID of some number. Use that number. // setting a Mutli-Selection Attribute //$data['special_attribute'] = '101 , 102 , 103'; // coma separated string of option IDs. As ID , ID (mind the spaces before and after coma, it worked for me like that) // $product->setData($data); if (isset($_product["attributes"])) { $erplyAttributes = $_product["attributes"]; $mapping = unserialize(Mage::getStoreConfig('eepohs_erply/product/attributes', $storeId)); if (!empty($erplyAttributes) && !empty($mapping)) { $mappings = array(); foreach ($mapping as $map) { $mappings[$map["erply_attribute"]] = $map["magento_attribute"]; } foreach ($erplyAttributes as $attribute) { if (in_array($attribute["attributeName"], array_keys($mappings))) { if ($attribute["attributeValue"]) { $product->setData($mappings[$attribute["attributeName"]], $attribute["attributeValue"]); } } } } } $product->save(); Mage::helper('Erply')->log("Added: " . $product->getSku()); } } }
protected function _createProducts($productsToCreateCount) { $skus = array(); $initialProductCount = $this->_getProductCount(); $randValues = array('general_name' => 'crife_Simple Product Required', 'general_sku' => 'wkdov_simple_sku_req_zuknf'); // Set an Admin Session Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); Mage::getSingleton('core/session', array('name' => 'adminhtml')); $userModel = Mage::getModel('admin/user'); $userModel->setUserId(1); $session = Mage::getSingleton('admin/session'); $session->setUser($userModel); $session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl()); for ($c = 0; $c < $productsToCreateCount; $c++) { array_walk_recursive($randValues, array($this, 'randomizeData'), array('general_name', 'general_sku')); $newProduct = new Mage_Catalog_Model_Product(); $newProduct->setTypeId('simple'); $newProduct->setCategoryIds(array(42)); $newProduct->setWebsiteIDs(array(1)); $newProduct->setWeight('1.000'); $newProduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); $newProduct->setStatus(1); $newProduct->setSku($randValues['general_sku']); $newProduct->setTaxClassId(0); $newProduct->setStoreId(Mage::app()->getStore()->getId()); $newProduct->setAttributeSetId(9); $newProduct->setName($randValues['general_name']); $newProduct->setDescription('Description'); $newProduct->setShortDescription('Short Description'); $newProduct->setPrice(9.99); $newProduct->setStockData(array('is_in_stock' => 1, 'qty' => 99999)); $newProduct->setCreatedAt(strtotime('now')); $newProduct->save(); $skus[] = $randValues['general_sku']; } $finalProductCount = $this->_getProductCount(); $this->assertEquals($productsToCreateCount, $finalProductCount - $initialProductCount); return $skus; }
public function createCreditProduct() { $product1 = new Mage_Catalog_Model_Product(); $product1->setStoreId(0)->setId(null)->setAttributeSetId(9)->setTypeId("customercredit")->setName("Credit product 1")->setSku("creditproduct")->setStatus("1")->setTaxClassId("0")->setVisibility("4")->setEnableGooglecheckout("1")->setCreditAmount("100")->setDescription("Credit product")->setShortDescription("credit")->save(); $product2 = new Mage_Catalog_Model_Product(); $product2->setStoreId(0)->setId(null)->setAttributeSetId(9)->setTypeId("customercredit")->setName("Credit product 2")->setSku("creditproduct")->setStatus("1")->setTaxClassId("0")->setVisibility("4")->setEnableGooglecheckout("1")->setCreditAmount("1-10000")->setDescription("Credit product")->setShortDescription("credit")->save(); }
/** * Fill simple product data during generation * * @param Mage_Catalog_Model_Product $product * @param Mage_Catalog_Model_Product $parentProduct * @param array $postData */ protected function _fillSimpleProductData(Mage_Catalog_Model_Product $product, Mage_Catalog_Model_Product $parentProduct, $postData) { $product->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->setTypeId($postData['weight'] ? Mage_Catalog_Model_Product_Type::TYPE_SIMPLE : Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL)->setAttributeSetId($parentProduct->getNewVariationsAttributeSetId()); foreach ($product->getTypeInstance()->getEditableAttributes($product) as $attribute) { if ($attribute->getIsUnique() || $attribute->getAttributeCode() == 'url_key' || $attribute->getFrontend()->getInputType() == 'gallery' || $attribute->getFrontend()->getInputType() == 'media_image' || !$attribute->getIsVisible()) { continue; } $product->setData($attribute->getAttributeCode(), $parentProduct->getData($attribute->getAttributeCode())); } if (!isset($postData['stock_data']['use_config_manage_stock'])) { $postData['stock_data']['use_config_manage_stock'] = 0; } $product->addData($postData)->setWebsiteIds($parentProduct->getWebsiteIds())->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); }
/** * Update product url key * * @param Mage_Catalog_Model_Product $product * @param string $newUrlKey * @param int $storeId */ function processProduct($product, $newUrlKey, $storeId) { $store = Mage::app()->getStore(); Mage::app()->setCurrentStore(Mage::app()->getStore(0)); if (!$this->isEntityProcessed(self::ENTITY_TYPE_PRODUCT, $product->getId() . '-' . $storeId) && !preg_match('~-[a-f0-9]{32}$~i', $newUrlKey)) { $product->setStoreId($storeId ? $storeId : null); $product->setUrlKey($newUrlKey . '-' . md5($product->getStoreId() . $product->getId())); $product->save(); $this->_reindexProduct($product->getId()); $product->unsetData('request_path'); $product->unsetData('url'); $this->markEntityProcessed(self::ENTITY_TYPE_PRODUCT, $product->getId() . '-' . $product->getStoreId()); } Mage::app()->setCurrentStore($store); }
/** * Setup product for quote item * * @param Mage_Catalog_Model_Product $product * @return Mage_Sales_Model_Quote_Item */ public function setProduct($product) { if ($this->getQuote()) { $product->setStoreId($this->getQuote()->getStoreId()); } $this->setData('product', $product)->setProductId($product->getId())->setProductType($product->getTypeId())->setSku($this->getProduct()->getSku())->setName($product->getName())->setWeight($this->getProduct()->getWeight())->setTaxClassId($product->getTaxClassId())->setCost($product->getCost())->setIsQtyDecimal($product->getIsQtyDecimal()); // if ($options = $product->getCustomOptions()) { // foreach ($options as $option) { // $this->addOption($option); // } // } return $this; }