PHP-скрипт создает CSV-файл экспорта на основе значений продукта

В настоящее время у меня есть следующий скрипт, который изменяет название продукта с определенным идентификатором.

Теперь я хочу переписать скрипт, чтобы он не обновлял атрибуты, а генерировал файл .csv всего каталога с двумя столбцами.

  1. Product_SKU
  2. $ productnamingseo value

Как мне этого добиться?

Текущий рабочий скрипт php для 1 продукта, он успешно добавляет все SKU продукта, но не реферал $ productnamingseo, только первый продукт:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

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

$product = Mage::getModel('catalog/product')->load(409728);
Mage::register('current_product', $product);
$seotitle = Mage::helper('seo')->getCurrentSeo();
$productnamestring = Mage::getSingleton('seo/object_product')->getTitle();

$findseo = array('/\h+inch (?:(i[357])-\w+|\h+\w+)?/', '/(\w+)#\w+/', '/(^| )(.{4,}) (.*)\2/', '/\s*-\s*$/');
$replaceseo = array('" $1', '$1', '$1$2 $3', '');
$productnamingseo = preg_replace($findseo, $replaceseo, $productnamestring);

$product->setName($productnamingseo);
$product->getResource()->saveAttribute($product, 'name');

EDIT уже пробовал это, это работает только для 1 продукта. Все остальные продукты получили значение $ productnamingseo для продукта 1 вместо собственной уникальной стоимости.

Это создаст файл .csv со следующим выводом. Только первая строка верна. Все остальные строки получили неправильный $ productnamingseo первого продукта.

"ACER_EY.JE001.002","Acer C120 LED - EY.JE001.002""ALLIEDTELESIS_AT-2701FXA/SC-001","Acer C120 LED - EY.JE001.002""APC_0M-0213-005","Acer C120 LED - EY.JE001.002"

Автор сценария:

ini_set('display_errors', 'On');
error_reporting(E_ALL);
ini_set('memory_limit', '4G');

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

$file_path = "var/import/productname.csv";
$mage_csv = new Varien_File_Csv();

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setPageSize(1)->setCurPage(1);

foreach ($products as $product) {
$prod = Mage::register('current_product', $product);
$seotitle = Mage::helper('seo')->getCurrentSeo();
$productnamestring = Mage::getSingleton('seo/object_product')->getTitle();

$findseo = array('/\h+inch (?:(i[357])-\w+|\h+\w+)?/', '/(\w+)#\w+/', '/(^| )(.{4,}) (.*)\2/', '/\s*-\s*$/');
$replaceseo = array('" $1', '$1', '$1$2 $3', '');
$productnamingseo = preg_replace($findseo, $replaceseo, $productnamestring);

echo $productnamingseo;

$data = array();
$data['sku'] = $product->getSku();
$data['name'] = $productnamingseo;
$products_row[] = $data;

Mage::unregister('current_product')
}

$mage_csv->saveData($file_path, $products_row);
echo 'Done!';

SEO помощник Mirasvit_Seo_Model_Object_Product:

public function _construct()
{
parent::_construct();
$this->_product = Mage::registry('current_product');
if (!$this->_product) {
$this->_product = Mage::registry('product');
}
if (!$this->_product) {
return;
}


$this->_parseObjects['product'] = $this->_product;

$this->setAdditionalVariable('product', 'url', $this->_product->getProductUrl());
$this->setAdditionalVariable('product', 'final_price', $this->_product->getFinalPrice());
$this->setAdditionalVariable('product', 'final_price_minimal', Mage::helper('seo')->getCurrentProductFinalPrice($this->_product));
$this->setAdditionalVariable('product', 'final_price_range', Mage::helper('seo')->getCurrentProductFinalPriceRange($this->_product));
$this->setAdditionalVariable('product', 'stock_qty', Mage::helper('seo')->getCurrentProductStockQty($this->_product));


$categoryId = $this->_product->getSeoCategory();
$this->_category = Mage::registry('current_category');

if ($this->_category && !$categoryId) {
$this->_parseObjects['category'] = $this->_category;
} elseif ($this->_product) {
if (!$categoryId) {
$categoryIds = $this->_product->getCategoryIds();
if (count($categoryIds) > 0) {
//we need this for multi websites configuration
$categoryRootId = Mage::app()->getStore()->getRootCategoryId();
$category = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('path', array('like' => "%/{$categoryRootId}/%"));

//don't delete (for some stores need main_table)
$stringSelect = $category->getSelect()->__toString();
$entityIdFilter = (strpos($stringSelect, 'main_table') !== false)
? 'main_table.entity_id' : 'entity_id';

$category = $category->addFieldToFilter($entityIdFilter, $categoryIds)
->setOrder('level', 'desc')
->setOrder($entityIdFilter, 'desc')
->getFirstItem()
;
$categoryId = $category->getId();
}
}
//load category with flat data attributes
$category = Mage::getModel('catalog/category')->load($categoryId);
$this->_category = $category;
$this->_parseObjects['category'] = $category;
if (!Mage::registry('seo_current_category')) {// to be sure that register will not be done twice
Mage::register('seo_current_category', $category);
};
}

$this->_parseObjects['store'] = Mage::getModel('seo/object_store');

$this->init();
}

getCurrentSeo () код:

public function getCurrentSeo()
{
if (Mage::app()->getStore()->getCode() == 'admin') {
return new Varien_Object();
}

$isCategory = Mage::registry('current_category') || Mage::registry('category');
$isProduct  = Mage::registry('current_product') || Mage::registry('product');
$isFilter   = false;

if ($isCategory) {
$filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
$isFilter = count($filters) > 0;
}

if ($isProduct) {
$seo = Mage::getSingleton('seo/object_product');
} elseif ($isCategory && $isFilter) {
$seo =  Mage::getSingleton('seo/object_filter');
} elseif ($isCategory) {
$seo =  Mage::getSingleton('seo/object_category');
} else {
$seo = new Varien_Object();
}

if ($seoTempalate = $this->checkTempalateRule($isProduct, $isCategory, $isFilter)) {
foreach ($seoTempalate->getData() as $k=>$v) {
if ($v) {
$seo->setData($k, $v);
}
}
}

if ($seoRewrite = $this->checkRewrite()) {
foreach ($seoRewrite->getData() as $k=>$v) {
if ($v) {
$seo->setData($k, $v);
}
}
}

$storeId = Mage::app()->getStore()->getStoreId();
$page    = Mage::app()->getFrontController()->getRequest()->getParam('p');
if (!$page) {
$page = 1;
}

if ($isCategory && !$isProduct) {
if ($this->_titlePage) {
switch ($this->_config->getMetaTitlePageNumber($storeId)) {
case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_BEGIN:
if ($page > 1) {
$seo->setMetaTitle(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaTitle()));
$this->_titlePage = false;
}
break;
case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_END:
if ($page > 1) {
$seo->setMetaTitle(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaTitle(), $page));
$this->_titlePage = false;
}
break;
case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_BEGIN_FIRST_PAGE:
$seo->setMetaTitle(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaTitle()));
$this->_titlePage = false;
break;
case Mirasvit_Seo_Model_Config::META_TITLE_PAGE_NUMBER_END_FIRST_PAGE:
$seo->setMetaTitle(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaTitle(), $page));
$this->_titlePage = false;
break;
}
}

if ($this->_descriptionPage) {
switch ($this->_config->getMetaDescriptionPageNumber($storeId)) {
case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_BEGIN:
if ($page > 1) {
$seo->setMetaDescription(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaDescription()));
$this->_descriptionPage = false;
}
break;
case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_END:
if ($page > 1) {
$seo->setMetaDescription(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaDescription(), $page));
$this->_descriptionPage = false;
}
break;
case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_BEGIN_FIRST_PAGE:
$seo->setMetaDescription(Mage::helper('seo')->__("Page %s | %s", $page, $seo->getMetaDescription()));
$this->_descriptionPage = false;
break;
case Mirasvit_Seo_Model_Config::META_DESCRIPTION_PAGE_NUMBER_END_FIRST_PAGE:
$seo->setMetaDescription(Mage::helper('seo')->__("%s | Page %s", $seo->getMetaDescription(), $page));
$this->_descriptionPage = false;
break;
}
}

if ($page > 1) {
$seo->setDescription(''); //set an empty description for page with number > 1 (to not have a duplicate content)
}
}

if ($metaTitleMaxLength = $this->_config->getMetaTitleMaxLength($storeId)) {
$metaTitleMaxLength = (int)$metaTitleMaxLength;
if ($metaTitleMaxLength < Mirasvit_Seo_Model_Config::META_TITLE_INCORRECT_LENGTH) {
$metaTitleMaxLength = Mirasvit_Seo_Model_Config::META_TITLE_MAX_LENGTH; //recommended length
}
$seo->setMetaTitle($this->_getTruncatedString($seo->getMetaTitle(), $metaTitleMaxLength, $page));
}

if ($metaDescriptionMaxLength = $this->_config->getMetaDescriptionMaxLength($storeId)) {
$metaDescriptionMaxLength = (int)$metaDescriptionMaxLength;
if ($metaDescriptionMaxLength < Mirasvit_Seo_Model_Config::META_DESCRIPTION_INCORRECT_LENGTH) {
$metaDescriptionMaxLength = Mirasvit_Seo_Model_Config::META_DESCRIPTION_MAX_LENGTH; //recommended length
}
$seo->setMetaDescription($this->_getTruncatedString($seo->getMetaDescription(), $metaDescriptionMaxLength, $page));
}

return $seo;
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector