Magento advanced profiles export - Prepend URL to IMAGE path
Asked Answered
S

4

6

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>
Spotter answered 14/2, 2014 at 20:57 Comment(0)
I
2

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.

Indiscerptible answered 20/2, 2014 at 15:2 Comment(4)
There is no other way apart from amending the core files? Insane! So what about if I have multi store setup and I want to use different domain names? I am presuming that the file is column.phpSpotter
You would not have to modify core files, you have to override those classes in your own modules under app/code/local, for the etc/config.xml tags that would override a model class you have an example here: #6576326 What do you mean by multi store and different domain names, can you give an example of what you're trying to achieve? Yes, the file is core/Mage/Dataflow/Model/Convert/Mapper/Column.phpIndiscerptible
Well lets say I wanted an output from each of my stores with a different domain prefix on the image url. Thats what I mean. I am very new to Magento, OpenCart convert...Spotter
I finally got around to looking at this and thank you for your advice, but I cannot get it to work, I have edited the /app/code/core/Mage/Dataflow/Model/Convert/Mapper/Column.php with the code blocks given and added the XML additions but get an error on the file generation of ' Error in field mapping: field list for mapping is not defined.', so clearly I have done something incorrectly. Can you expand on your advice?Spotter
E
2

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>
Eloquence answered 17/7, 2014 at 8:55 Comment(3)
Hello MarmiK, thanks for the hint, but I thougt because it is the solution of the question, I do not need to make a new question?Eloquence
Now this looks like answer, I am deleting my comment above :) keep it upMorphophonemics
I think you need to modify the if statement as following: if ($varNode['name'] == 'map' || $varNode['name'] == 'prepend')Discrimination
H
0

I'v developed a simple module to solve the issue. Just install it and append/prepend whatever you want. Here is the link http://bkielbasa.pl/magento-advanced-profiles-export-prepend-url-image-path/

It does not edit any core files.

Heikeheil answered 25/12, 2014 at 23:20 Comment(0)
K
0

If anyone want this variation, I took kabanek's solution and extended it to added a constant column variation. This way we can export information straight into a template that need fields not in the database.

In Convert.php changed

if ($varNode['name'] == 'map' || $varNode['name'] == 'prepend' || $varNode['name'] == 'append' ) {

to

if ($varNode['name'] == 'map' || $varNode['name'] == 'prepend' || $varNode['name'] == 'append' || $varNode['name'] == 'const') {

Then in Column.php added:

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

and changed:

$newRow[$mapField] = isset($row[$field]) ? ($prependText . $row[$field] . $appendText) : null;

to

$constText = isset($constCol[$field]) ? $constCol[$field] : null;
$newRow[$mapField] = isset($row[$field]) ? ($prependText . $row[$field] . $appendText) : $constText;
Kobold answered 19/3, 2015 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.