public function parse($item) { $catalogPriceRule = Mage::getModel('catalogrule/rule'); $cond = unserialize($item->getConditionsSerialized()); $this->conditions($cond); $item->setConditionsSerialized(serialize($cond)); $object = $item->getData(); $data = array('rule_id' => $object['rule_id'], 'name' => $object['name'], 'description' => $object['description'], 'from_date' => $object['from_date'], 'to_date' => $object['to_date'], 'customer_group_ids' => $object['customer_group_ids'], 'is_active' => $object['is_active'], 'conditions_serialized' => $object['conditions_serialized'], 'actions_serialized' => $object['actions_serialized'], 'stop_rules_processing' => $object['stop_rules_processing'], 'sort_order' => $object['sort_order'], 'simple_action' => $object['simple_action'], 'discount_amount' => $object['discount_amount'], 'website_ids' => array(1), 'customer_group_checked' => $object['customer_group_checked']); $catalogPriceRule->setName($object['name'])->setDescription($object['description'])->setFromDate($object['from_date'])->setToDate($object['to_date'])->setCustomerGroupIds($object['customer_group_ids'])->setIsActive($object['is_active'])->setConditionsSerialized($object['conditions_serialized'])->setActionsSerialized($object['actions_serialized'])->setStopRulesProcessing($object['stop_rules_processing'])->setSortOrder($object['sort_order'])->setSimpleAction($object['simple_action'])->setDiscountAmount($object['discount_amount'])->setWebsiteIds(array(1))->setCustomerGroupChecked($object['customer_group_checked']); try { $catalogPriceRule->save(); } catch (Exception $e) { mage::D($e); return; } }
public function parse($item) { $catalogPriceRule = Mage::getModel('salesrule/rule'); $cond = unserialize($item->getConditionsSerialized()); //mage::D($item); return; //die; $this->conditions($cond); $item->setConditionsSerialized(serialize($cond)); $object = $item->getData(); //mage::d($object); $data = array('rule_id' => $object['rule_id'], 'name' => $object['name'], 'description' => $object['description'], 'from_date' => $object['from_date'], 'to_date' => $object['to_date'], 'uses_per_customer' => $object['uses_per_customer'], 'customer_group_ids' => $object['customer_group_ids'], 'is_active' => $object['is_active'], 'conditions_serialized' => $object['conditions_serialized'], 'actions_serialized' => $object['actions_serialized'], 'stop_rules_processing' => $object['stop_rules_processing'], 'is_advanced' => $object['is_advanced'], 'sort_order' => $object['sort_order'], 'simple_action' => $object['simple_action'], 'discount_amount' => $object['discount_amount'], 'discount_qty' => $object['discount_qty'], 'discount_step' => $object['discount_step'], 'simple_free_shipping' => $object['simple_free_shipping'], 'apply_to_shipping' => $object['apply_to_shipping'], 'times_used' => $object['times_used'], 'is_rss' => $object['is_rss'], 'website_ids' => array(1), 'coupon_type' => $object['coupon_type'], 'coupon_code' => $object['coupon_code'], 'uses_per_coupon' => $object['uses_per_coupon']); $catalogPriceRule->setName($object['name'])->setDescription($object['description'])->setFromDate($object['from_date'])->setToDate($object['to_date'])->setUsesPerCustomer($object['uses_per_customer'])->setCustomerGroupIds($object['customer_group_ids'])->setIsActive($object['is_active'])->setConditionsSerialized($object['conditions_serialized'])->setActionsSerialized($object['actions_serialized'])->setStopRulesProcessing($object['stop_rules_processing'])->setIsAdvanced($object['is_advanced'])->setSortOrder($object['sort_order'])->setSimpleAction($object['simple_action'])->setDiscountAmount($object['discount_amount'])->setDiscountQty($object['discount_qty'])->setDiscountStep($object['discount_step'])->setSimpleFreeShipping($object['simple_free_shipping'])->setApplyToShipping($object['apply_to_shipping'])->setTimesUsed($object['times_used'])->setIsRss($object['is_rss'])->setWebsiteIds(array(1))->setCouponType($object['coupon_type'])->setCouponCode($object['coupon_code'])->setUsesPerCoupon($object['uses_per_coupon']); try { $catalogPriceRule->save(); } catch (Exception $e) { mage::D($e); return; } }
public function addCustomer($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())); $product->setStoreIDs(array(1)); foreach ($data as $key => $value) { switch ($key) { case 'store_id': case 'website_id': 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->setIsMassupdate(false); $product->setExcludeUrlRewrite(true); try { $product->save(); //Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds(array($productId)); } catch (Exception $ex) { mage::D($ex); #mage::log($ex->getMessage(), null, 'prodsimp.log'); } }
public function upsell($data) { if (empty($data['upsell'])) { return; } $productId = Mage::getModel('catalog/product')->getCollection()->getItemByColumnValue('sku', $data['sku'])->getId(); $product = Mage::getModel('catalog/product')->load($productId); $param = array(); $upsell = $data['upsell']; foreach ($upsell as $key => $value) { $id = Mage::getModel('catalog/product')->getCollection()->getItemByColumnValue('sku', $key)->getId(); $upsell[$id] = $value; unset($upsell[$key]); } $product->setUpSellLinkData($upsell); try { $product->save(); } catch (Exception $e) { mage::D($e); } }
public function addOptions() { foreach ($this->data as $d) { mage::D($d['sku']); } }
public function parse($object) { mage::D($object); die; //$this->createAttribute($object); }
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(); } } # ----------- set upsell, crossel, related products not possible, first need load all products :-) ---------------- # } catch (Exception $ex) { mage::D($ex); #mage::log($ex->getMessage(), null, 'prodsimp.log'); } }
public function loadAttributes() { $optionText = file_get_contents('../var/import/attributes.txt'); $attrArray = unserialize($optionText); //mage::d($attrArray); //die; //$model = Mage::getModel('catalog/resource_eav_attribute')->load(143)->delete(); die; foreach ($attrArray as $attr) { try { $attr->getSource()->getAllOptions(false); } catch (Exception $e) { echo "yes"; die; continue; } $model = Mage::getModel('catalog/resource_eav_attribute'); //mage::ms($model); die; // if attribute_code already exists, renew it only. if ($attributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', $attr->getAttributeCode())) { $model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); #$model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId)->delete(); //delete old attribute options. TODO: apply API for it. $query = "DELETE ao FROM eav_attribute_option ao JOIN eav_attribute ea ON ao.attribute_id = ea.attribute_id AND ea.attribute_id ={$attributeId}"; $this->query($query); } $data = array('attribute_code' => $attr->getAttributeCode(), 'attribute_model' => $attr->getData('attribute_model'), 'backend_model' => $attr->getData('backend_model'), 'backend_type' => $attr->getData('backend_type'), 'backend_table' => $attr->getData('backend_table'), 'frontend_model' => $attr->getData('frontend_model'), 'frontend_input' => $attr->getData('frontend_input'), 'frontend_label' => $attr->getData('frontend_label'), 'frontend_class' => $attr->getData('frontend_class'), 'source_model' => $attr->getData('source_model'), 'is_required' => $attr->getData('is_required'), 'is_user_defined' => $attr->getData('is_user_defined'), 'default_value' => $attr->getData('default_value'), 'is_unique' => $attr->getData('is_unique'), 'note' => $attr->getData('note'), 'frontend_input_renderer' => $attr->getData('frontend_input_renderer'), 'is_global' => $attr->getData('is_global'), 'is_visible' => $attr->getData('is_visible'), 'is_searchable' => $attr->getData('is_searchable'), 'is_filterable' => $attr->getData('is_filterable'), 'is_comparable' => $attr->getData('is_comparable'), 'is_visible_on_front' => $attr->getData('is_visible_on_front'), 'is_html_allowed_on_front' => $attr->getData('is_html_allowed_on_front'), 'is_used_for_price_rules' => $attr->getData('is_used_for_price_rules'), 'is_filterable_in_search' => $attr->getData('is_filterable_in_search'), 'used_in_product_listing' => $attr->getData('used_in_product_listing'), 'used_for_sort_by' => $attr->getData('used_for_sort_by'), 'is_configurable' => $attr->getData('is_configurable'), 'apply_to' => $attr->getData('apply_to'), 'is_visible_in_advanced_search' => $attr->getData('is_visible_in_advanced_search'), 'position' => $attr->getData('position'), 'is_wysiwyg_enabled' => $attr->getData('is_wysiwyg_enabled'), 'is_used_for_promo_rules' => $attr->getData('is_used_for_promo_rules')); if (!$attributeId) { $model->addData($data); $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId()); } else { $model->setDataChanges($data); } // set groups and sets $connection = Mage::getModel('core/resource')->getConnection('core_read'); $select = $connection->select()->from(array('oasm' => 'oberig_attribute_set_mapping'), array('attribute_group_name', 'sort_order'))->joinInner(array('eas' => 'eav_attribute_set'), 'oasm.attribute_set_name = eas.attribute_set_name AND entity_type_id = 4', 'attribute_set_id')->joinInner(array('eag' => 'eav_attribute_group'), 'eag.attribute_set_id = eas.attribute_set_id AND eag.attribute_group_name = oasm.attribute_group_name', 'attribute_group_id')->where('attribute_code=?', $attr->getAttributeCode()); echo $select->__toString() . "\n"; $result = $connection->query($select)->fetchAll(); echo "-------------------------------------------------\n"; mage::D($result); echo "-------------------------------------------------\n"; foreach ($result as $res) { if ($attributeId) { $model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); } $model->setAttributeSetId($res['attribute_set_id']); $model->setAttributeGroupId($res['attribute_group_id']); $model->setSortOrder($res['sort_order']); if ($attributeId) { try { $model->save(); } catch (Exception $ex) { echo "Error: " . $ex->getMessage() . "\n"; } } } //end set groups and sets //if( $attr->getAttributeCode() == 'lenstint' ) //{ try { if (!$attributeId) { $model->save(); echo $attr->getAttributeCode() . " ..........................ok. ID: " . $model->getId() . "\n"; } $option['attribute_id'] = $model->getId(); $all_opts = $attr->getSource()->getAllOptions(false); } catch (Exception $ex) { echo "Error: " . $ex->getMessage() . "\n"; } //add attribute options foreach ($all_opts as $opt) { $option['value']['option'][0] = $opt['label']; mage::d($option); //die; $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $setup->addAttributeOption($option); } echo "Model " . $model->getAttributeCode() . " saved. options added ..........................ok. \n"; //$model = Mage::getModel('catalog/resource_eav_attribute')->load($model->getId())->delete(); //die; //} } // set tax class options TODO: API $this->query("insert ignore into tax_class select * from {$this->oldDB}.tax_class"); //die; }
public function addProduct() { echo date("\nY-d-m H:i:s\n"); //$processes = Mage::getSingleton('index/indexer')->getProcessesCollection(); //$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL)); //$processes->walk('save'); $connection = Mage::getModel('core/resource')->getConnection('core_read'); $product = new Mage_Catalog_Model_Product(); // get data array $select = $connection->select()->from('oberig_products'); $result = $connection->query($select)->fetchAll(); //(PDO::FETCH_ASSOC); //mage::d($row); //echo $select->__toString();//die;//mage::D($result); //category mapping $result2 = array(); foreach ($result as &$row) { if (empty($row['category_ids'])) { $row['category_ids'] = array(); $result2[] = $row; continue; } $cats = explode(',', $row['category_ids']); foreach ($cats as &$cat) { $select = $connection->select()->from('oberig_category_mapping', 'new_cat_id')->where('old_cat_id=?', $cat); $res = $connection->query($select)->fetch(); $cat = $res['new_cat_id']; } $row['category_ids'] = $cats; $result2[] = $row; } $result = $result2; foreach ($result as $row) { $product = new Mage_Catalog_Model_Product(); // Build the product $product->setSku($row['sku']); $product->setAttributeSetId(Mage::getModel('eav/entity_attribute_set')->getCollection()->setEntityTypeFilter($this->typeId)->addFieldToFilter('attribute_set_name', $row['attribute_set'])->getFirstItem()->getAttributeSetId()); #$product->setAttributeSetId( $this->typeId );# 9 is for default $product->setTypeId('simple'); $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); $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', $this->optByCode('options_container', $row['options_container'])); $product->setData('extra_title', $row['extra_title']); $product->setData('frame_gender', $this->optByCode('frame_gender', $row['frame_gender'])); $product->setData('frame_shape', $this->optByCode('frame_shape', $row['frame_shape'])); $product->setData('cost', $row['cost']); $product->setData('weight', $row['weight']); $product->setData('manufacturer', $this->optByCode('manufacturer', $row['manufacturer'])); //$product->setData('status', $this->optByCode('status', $row['status'])); $product->setData('tax_class_id', $this->optByCode('tax_class_id', $row['tax_class_id'])); //$product->setData('visibility', $this->optByCode('visibility', $row['visibility'])); $product->setData('enable_googlecheckout', $this->optByCode('enable_googlecheckout', $row['enable_googlecheckout'])); $product->setData('is_imported', $this->optByCode('is_imported', $row['is_imported'])); $product->setData('frame_type', $this->optByCode('frame_type', $row['frame_type'])); $product->setData('sell_by_phone_only', $this->optByCode('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', $this->optByCode('custom_design', $row['custom_design'])); $product->setData('page_layout', $this->optByCode('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', $this->optByCode('gift_message_available', $row['gift_message_available'])); $product->setData('framecolour', $this->optByCode('framecolour', $row['framecolour'])); $product->setData('special_price', $row['special_price']); $product->setData('is_recurring', $this->optByCode('is_recurring', $row['is_recurring'])); $product->setData('frame_size', $this->optByCode('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', $this->optByCode('stock_clearance', $row['stock_clearance'])); $product->setData('lenstint', $this->optByCode('lenstint', $row['lenstint'])); $product->setData('framesizetest', $this->optByCode('framesizetest', $row['framesizetest'])); $product->setStockData(array('is_in_stock' => $row['is_in_stock'], 'qty' => $row['qty'])); $product->setIsMassupdate(false); $product->setExcludeUrlRewrite(true); try { $product->save(); //Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds(array($productId)); } catch (Exception $ex) { mage::D($ex); echo $ex->getMessage(); die; } //die; } //$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'); //$processes->walk('reindexAll'); //if ($indexProcess) { //$indexProcess->reindexAll(); //} echo date("\nY-d-m H:i:s\n"); }
public function synchronizeProdsInCats() { $connection = Mage::getModel('core/resource')->getConnection('core_write'); foreach ($this->objects as $object) { $catId = $this->getCategoryIdByPath($object['fullpath']); if (empty($catId)) { die("Not exists: {$object['fullpath']}"); } $category = Mage::getModel('catalog/category')->load($catId); //mage::ms($category->getProductCollection()); $ids = $category->getProductCollection()->getAllIds(); $skus = null; //mage::ms(Mage::getModel('catalog/product')); //die; foreach ($ids as $prodId) { $arr = Mage::getModel('catalog/product')->getResource()->getProductsSku($prodId); $skus[$arr[0]['entity_id']] = $arr[0]['sku']; //$skus[] = Mage::getModel('catalog/product')->load($prodId)->getSku(); } //var_dump($skus); $diff = array_diff($skus, $object['products']); $diff2 = array_diff($object['products'], $skus); // TODO: if sku: cd3197-8 and CD3197-8 - that is different for array_diff() but the same for mysql! if (!empty($diff2)) { foreach ($diff2 as $sku2) { $product_id = Mage::getModel('catalog/product')->getIdBySku($sku2); $query = "insert into catalog_category_product (category_id,product_id,position) VALUES ({$catId},{$product_id},1)"; echo $query . "\n"; continue; try { $connection->exec($query); } catch (Exception $e) { echo $query . "\n"; mage::D($sku2); echo $e->getMessage() . "\n"; } } //die; } $str = null; if (!empty($diff)) { $str = implode(',', array_flip($diff)); } if (!empty($str)) { $query = "delete from catalog_category_product where product_id IN ({$str}) and category_id={$catId}"; echo $query . "\n"; continue; try { $connection->exec($query); } catch (Exception $e) { var_dump($e->getMessage()); } } //mage::d($object['products'] ); //die; //$catProdIds = $category->getProductCollection()->getAllIds(); } }