Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento advanced profiles export - Prepend URL to IMAGE path

Just a couple of weeks into Magento, managed to get going with the Advanced Export Profiles (Very Handy), What I would like to do is Prepend a url value to one of the output columns, specifically the image url. I would like to append the url to the beginning of the path output.

Can anyone assist?

<action type="catalog/convert_adapter_product" method="load">
    <var name="store"><![CDATA[0]]></var>
    <var name="filter/price/from"><![CDATA[0.01]]></var>
    <var name="filter/price/to"><![CDATA[999999]]></var>
    <var name="filter/visibility"><![CDATA[4]]></var>
    <var name="filter/status"><![CDATA[1]]></var>
</action>

<action type="catalog/convert_parser_product" method="unparse">
    <var name="store"><![CDATA[0]]></var>
    <var name="url_field"><![CDATA[0]]></var>
</action>

<action type="dataflow/convert_mapper_column" method="map">
    <var name="map">
        <map name="name"><![CDATA[ItemTitle]]></map>
        <map name="upc"><![CDATA[EANBarCode]]></map>
        <map name="description"><![CDATA[ItemTextDescription]]></map>
        <map name="sku"><![CDATA[SKU]]></map>
        <map name="qty"><![CDATA[StockLevel]]></map>
        <map name="price"><![CDATA[CostPrice]]></map>
        <map name="manufacturer"><![CDATA[Brand]]></map>
        <map name="ebaycategory1"><![CDATA[eBayCategory1]]></map>
        <map name="ebaycategory2"><![CDATA[eBayCategory2]]></map>
        <map name="image"><![CDATA[Image1]]></map>
        <map name="description"><![CDATA[ListingDescription]]></map>
        <map name="name"><![CDATA[ListingTitle]]></map>
        <map name="msrp"><![CDATA[OriginalRetailPrice]]></map>
        <map name="conditionnote"><![CDATA[SellerNotes]]></map>
    </var>
    <var name="_only_specified">true</var>
</action>

<action type="dataflow/convert_parser_csv" method="unparse">
    <var name="delimiter"><![CDATA[,]]></var>
    <var name="enclose"><![CDATA["]]></var>
    <var name="fieldnames">true</var>
</action>

<action type="dataflow/convert_adapter_io" method="save">
    <var name="type">file</var>
    <var name="path">var/export</var>
    <var name="filename"><![CDATA[testing123.csv]]></var>
</action>
like image 918
Stuart Avatar asked Feb 14 '14 20:02

Stuart


2 Answers

In the class Mage_Dataflow_Model_Convert_Mapper_Column looking at the method map, you have only 2 vars right now: map and _only_specified. What you need to do is override this class and this method and add something like this at line 125, after var is set:

if ($this->getVar('prepend') && is_array($this->getVar('prepend'))) {
        $prepend = $this->getVar('prepend');
} else {
        $prepend = array();
}

Now you have a new variable prepend that you can use for the batch data like this - at line 138 in the same class you have:

$newRow = array();
foreach ($attributesToSelect as $field => $mapField) {
        $newRow[$mapField] = isset($row[$field]) ? $row[$field] : null;
}

Change this into:

$newRow = array();
foreach ($attributesToSelect as $field => $mapField) {
        $prepend = isset($prepend[$field]) ? $prepend[$field] : '';
        $newRow[$mapField] = isset($row[$field]) ? $prepend . $row[$field] : null;
}

Now in your xml that you posted above, you can add a prepend variable like this:

<action type="dataflow/convert_mapper_column" method="map">
<var name="prepend">
    <map name="image"><![CDATA[http://example.com/]]></map>

I haven't tested this, but that's how I would try it first. Also didn't add the part about how you can override this model class, since I think that there are plenty examples out there.

like image 68
Emi Avatar answered Oct 24 '22 08:10

Emi


I just registered, so I can not make a Comment on the Answer of Emi. I want to thank him, because he is right, but there is one small piece of Code you have to change too. Here is my solution in short.

I've also made a more detailed blog-entry in my private blog: https://www.timoschindler.de/vollstaendige-urls-in-dataflow-exportierten-csv-dateien-von-magento/ Unfortunately it is in German ;). If you have any Problems, just let me know and I can translate it.

First I added the file app/code/local/Mage/Dataflow/Model/Convert/Mapper/MyColumn.php

<?php
class Mage_Dataflow_Model_Convert_Mapper_MyColumn extends Mage_Dataflow_Model_Convert_Mapper_Column
{
public function map()
{
    $batchModel  = $this->getBatchModel();
    $batchExport = $this->getBatchExportModel();

    $batchExportIds = $batchExport
        ->setBatchId($this->getBatchModel()->getId())
        ->getIdCollection();

    $onlySpecified = (bool)$this->getVar('_only_specified') === true;

    if (!$onlySpecified) {
        foreach ($batchExportIds as $batchExportId) {
            $batchExport->load($batchExportId);
            $batchModel->parseFieldList($batchExport->getBatchData());
        }

        return $this;
    }

    if ($this->getVar('map') && is_array($this->getVar('map'))) {
        $attributesToSelect = $this->getVar('map');
    }
    else {
        $attributesToSelect = array();
    }
    if ($this->getVar('prepend') && is_array($this->getVar('prepend'))) {
            $prepend = $this->getVar('prepend');
    } else {
            $prepend = array();
    }
    if (!$attributesToSelect) {
        $this->getBatchExportModel()
            ->setBatchId($this->getBatchModel()->getId())
            ->deleteCollection();

        throw new Exception(Mage::helper('dataflow')->__('Error in field mapping: field list for mapping is not defined.'));
    }

    foreach ($batchExportIds as $batchExportId) {
        $batchExport = $this->getBatchExportModel()->load($batchExportId);
        $row = $batchExport->getBatchData();

        $newRow = array();
        foreach ($attributesToSelect as $field => $mapField) {
            $prepend_2 = isset($prepend[$field]) ? $prepend[$field] : '';
            $newRow[$mapField] = isset($row[$field]) ? $prepend_2 . $row[$field] : null;
        }

        $batchExport->setBatchData($newRow)
            ->setStatus(2)
            ->save();
        $this->getBatchModel()->parseFieldList($batchExport->getBatchData());
    }

    return $this;
}
}

then I copied app/code/core/Mage/Dataflow/Model/Convert/Profile/Collection.php to app/code/local/Mage/Dataflow/Model/Convert/Profile/Collection.php and changed one if in the code:

/** @var $varNode Varien_Simplexml_Element */
       foreach ($actionNode->var as $key => $varNode) {
           if ($varNode['name'] == 'map') {
               $mapData = array();
               foreach ($varNode->map as $mapNode) {
                   $mapData[(string)$mapNode['name']] = (string)$mapNode;
               }
               $container->setVar((string)$varNode['name'], $mapData);
           }  else {

Thats all! A simple Dataflow XML now looks like:

<action type="dataflow/convert_mapper_myColumn" method="map">
    <var name="map">
        <map name="sku"><![CDATA[Artikelnummer]]></map>
        <map name="name"><![CDATA[Artikelbezeichnung]]></map>
        <map name="image"><![CDATA[image]]></map>
    </var>
    <var name="prepend">
        <map name="image"><![CDATA[https://www.bier-kaufen.de/media/catalog/product]]></map>
    </var>
    <var name="_only_specified">true</var>
</action>
like image 42
timo Avatar answered Oct 24 '22 06:10

timo