php脚本基于产品值创建csv导出文件

时间:2018-12-18 09:37:12

标签: php csv magento-1.9

我目前有以下脚本,用于更改具有特定ID的产品的产品标题。

现在,我想重新编写脚本,以便它不会更新属性,而是生成带有两列的整个目录的.csv文件。

  1. Product_SKU
  2. $ productnamingseo值

我该如何实现?

一种产品当前有效的php脚本,它成功添加了所有产品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种产品。所有其他产品都获得了产品1的$ productnamingseo价值,而不是它们自己的唯一价值。

这将创建一个具有以下输出的.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 个答案:

没有答案