0

My current scenario:

I am given a spreadsheet similar to what we used to import products to Amazon. In this spreadsheet are multiple columns for Amazon's "bullet points" area. These are then combined into one cell on my Magmi import spreadsheet, separated by line breaks to form our short description area.

What I'd like to accomplish:

Instead of combining these columns manually, I plan to setup new custom text/text-area attributes named bullet_1, bullet_2, etc.. and import each column's information into each attribute individually. I will not add calls to these attributes into the view.phtml for product pages - but instead want to reference them in a .phtml file between line-items in an unordered list.

The new bullet attributes will always have content starting from the lowest number to the highest, so there would be no gaps until there is no more bullet point information to add. Adding to the custom array can stop when an empty bullet attribute is found.

bullet attributes will always be text strings. There will be no code imported into these attribute text fields.

In the short description area on the product import spreadsheet, I'll have the call to this (hopefully) universal .phtml file.

The .phtml file would work something like this:

<?php 

$bullet1 = $product->getData('bullet_a');
$bullet2 = $product->getData('bullet_b');
$bullet3 = $product->getData('bullet_c');
$bullet4 = $product->getData('bullet_d');
$bullet5 = $product->getData('bullet_e');
$bullet6 = $product->getData('bullet_f');
$bullet7 = $product->getData('bullet_g');
$bullet8 = $product->getData('bullet_h');
$bullet9 = $product->getData('bullet_i');
$bullet10 = $product->getData('bullet_j');

$bullets = array("$bullet1","$bullet2","$bullet3","$bullet4","$bullet5","$bullet6","$bullet7","$bullet8","$bullet9","$bullet10");

echo "<ul>\n";
foreach($bullets as $bullet){

 if (!empty($bullet)){
 echo "<li>". $bullet . "</li>\n";
}
}
echo "<ul>\n";

?>

I'll end up minifying this .phtml file as our current theme/template/coding is auto adding line breaks when a new line is used in the short description area. This does not happen in our description area.

Please feel free to ask questions if I didn't explain this well enough. I'm also open to other methods of making this happen - and can even pay if this needs to be super custom coded.

Thank you!


UPDATE: later that same day

I've had some help with writing the phtml file but am getting errors when trying to call the attribute and use it as a variable. What do I have wrong?

  • In your case I would go for a proper value import rather than some nasty attribute looping in the template after the import. Did you check out wiki.magmi.org/index.php?title=Value_Replacer? You should be able to build your unsorted list using that Magmi plugin. Something similar to this could work: <ul><li>-{{preg_replace("/[\n\r]/","</li><li>",{item.attributename})}}-</li></ul> – Christoph Farnleitner Jan 19 '18 at 21:51
  • Sorry, I've missed that these are separate columns. In that case something like this should do: <ul><li>-{item.attribu‌​tename1}</li><li>{item.attribu‌​tename2}</li><li>{item.attribu‌​tename3}-</li></ul‌​> – Christoph Farnleitner Jan 19 '18 at 22:12
  • I only referenced Magmi to let others know I'm using spreadsheets for import. the main goal is to not have all bullet points together in one text area separated by code, but instead each bullet point as it's own attribute to be updated when needed (by simple import or update in admin) – Steven J Jan 19 '18 at 23:41
  • "but am getting errors when trying to call the attribute" - what error do you get? And also, which might answer the problem already, what is $product your this case? – Christoph Farnleitner Jan 20 '18 at 0:37
1

You can create custom template to show all bullet attributes.

Add a new block in product.info Block in your theme catalog.xml.

<block type="catalog/product_view_description" name="custom_list" as="custom_list" template="catalog/product/view/custom_list.phtml" /> 

Call the custom block in Product template catalog/product/view.phtml like below

<?php echo $this->getChildHtml('custom_list') ?>

Create custom template in your theme catalog/product/view/custom_list.phtml and add below code:

<?php 
    $product = $this->getProduct();
    $alphas = range('a', 'j');
    $values = [];
    foreach($alphas as $alpha) {
        $attrValue = $product->getData('bullet_'.$alpha);   
        if(!empty($attrValue)) { 
            $values[] = $attrValue;
        }
    }
?>
<ul>
    <?php foreach($values as $value) { ?>
        <li><?php echo $value; ?></li>
    <?php } ?>
</ul>

Hope this help !!

  • Looked like it was working but I must have placed the new line in catalog.xml in the wrong place - it crashed the site until I removed it. I got the error Warning: simplexml_load_string(): Entity: line 264: parser error : Opening and ending tag mismatch: block line 192 and reference in ~/public_html/app/code/core/Mage/Core/Model/Layout/Update.php on line 450 – Steven J Jan 22 '18 at 17:13
  • 1
    Yes the block tag was not closed "/>". I have updated the code. you can use that now – Pankaj Pareek Jan 22 '18 at 18:13
  • Thank you. Now the page is loading and no crashes. The information I have in the attribute for the test product is not loading. I have only setup bullet_a and bullet_b - do I need to have all of them loaded as attributes before expecting this to work? – Steven J Jan 22 '18 at 19:48
  • Also, does it matter that I want this to load after/in place of the short_description field - and not the actual description field? My short_description shows after price and full description loads in lower area within tabs – Steven J Jan 22 '18 at 19:50
  • 1
    You can add the content anywhere on the page by placing echo $this->getChildHtml('custom_list') on desired location. No there is no need to load attributes as magento automatically do this. Double check in Manage Attributes section that attributes are visible on product detail page – Pankaj Pareek Jan 23 '18 at 4:52
0

With the help of Pankaj Pareek and a friend, I was able to make this happen!

After trying the answer provided by Pankaj - I wasn't able to get these attributes to appear. Specifically even after adding lines to the catalog.xml and view.phtml with call to ChildHTML only the DIVs outside were rendering.

I decided to enter the PHP code directly into the view.phtml where I wanted it to be displayed - and it works! Probably not the cleanest way, and doesn't future-proof it, but this can always be improved.

From there some changes were made to better suit my specific use.

<?php 
    $product = $this->getProduct();
    $alphas = range('a', 'j');
    $values = [];
    foreach($alphas as $alpha) {
        $attrValue = $product->getData('bullet_'.$alpha);   
        if(!empty($attrValue)) { 
            $values[] = $attrValue;
        }
    }
?>
<?php
    if(empty($values)) { 
        echo '<div id="new-bullets" class="short-description ul-bullets" style="display:none">'; 
    } else {
        echo '<div id="new-bullets" class="short-description ul-bullets">';
    } 
?>

<div class="std" itemprop="description">
    <ul style="list-style-type: disc !important; margin-left: 20px; ">
        <?php foreach($values as $value) { ?>
            <li><?php echo $value; ?></li>
        <?php } ?>
    </ul>
</div>

The above code will create an array of the specified attribute range, then dump them one by one into an unordered list. The styling shown is specific to my theme/setup - make sure to update classes and other styles for your own use.

I wanted to use document.getElementById to update the ID (now removed) and put a display: none when the array is empty, but Magento was showing an error referencing the = sign.

In that scenario, the two divs are outside of the IF statement:

<div id="customb" class="short-description ul-bullets">
    <div class="std" itemprop="description">
        <?php 
            $product = $this->getProduct();
            $alphas = range('a', 'j');
            $values = [];
            foreach($alphas as $alpha) {
                $attrValue = $product->getData('bullet_'.$alpha);   
                if(!empty($attrValue)) { 
                    $values[] = $attrValue;
                }
            }
        ?>
        <?php
            if(empty($values)) {
                document.getElementById('customb').style.display = 'none';
            }
        ?>
        <ul>
            <?php foreach($values as $value) { ?>
                <li><?php echo $value; ?></li>
            <?php } ?>
        </ul>
    </div>
</div>

Any updates or suggestions on making this work are appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.