Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

JSON Help
 
bobbrodie
Sr. Member
 
Avatar
Total Posts:  132
Joined:  2008-04-02
Philadelphia, PA
 

Hello,

I know this has been done other ways, but I need to have my attribute dropdown change the image on my product page. I have over 4,000 products, so I can’t do it by filename - I have to figure out how to do this by using JSON and the database.

On the product page, attributes for configurable products aren’t in option tags inside the select tags, it looks like this:

<dl>
            <
dt><label>Color<span class="required">&nbsp;*</span></label></dt>
        <
dd class="last">

          <
select name="super_attribute[76]" id="attribute76" class="required-entry super-attribute-select">
            <
option>Choose an Option...</option>
          </
select>
        </
dd>
        </
dl>

    <
script type="text/javascript">
        var 
spConfig = new Product.Config({"attributes":{"76":{"id":"76","code":"color","label":"Color","options":[{"id":"1","label":"Blue","price":"0","products":["2"]},{"id":"2","label":"Pink","price":"0","products":["3"]}]}},"template":"$#{price}","basePrice":"5","oldPrice":"5","productId":"1","chooseText":"Choose option..."});
    
</script>

This is (I'm pretty sure) JSON. My idea is to use:
var selectmenu=document.getElementById("attribute<?php echo $_attribute->getAttributeId() ?>");
selectmenu.onchange=function(){
        CODE HERE
}

inside the onchange, I want to pull the product id, which I thought would be something like:

spConfig.attributes.76[0].options[0].id

and then use a Magento function in PHP to pull the image for that product id and show it.

No matter what I’ve tried, I can’t read the attributes from the JSON array.

Can anyone help me? I think a feature like this would mean a great deal to the community.

Thanks,
Bob Brodie

 
Magento Community Magento Community
Magento Community
Magento Community
 
bobbrodie
Sr. Member
 
Avatar
Total Posts:  132
Joined:  2008-04-02
Philadelphia, PA
 

I think this is the correct conversion to XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root type="object">
  <
attributes type="object">
    <
a:item xmlns:a="item" item="76" type="object">
      <
id type="string">76</id>
      <
code type="string">color</code>
      <
label type="string">Color</label>
      <
options type="array">
        <
item type="object">
          <
id type="string">1</id>
          <
label type="string">Blue</label>
          <
price type="string">0</price>
          <
products type="array">
            <
item type="string">2</item>
          </
products>
        </
item>
        <
item type="object">
          <
id type="string">2</id>
          <
label type="string">Pink</label>
          <
price type="string">0</price>
          <
products type="array">
            <
item type="string">3</item>
          </
products>
        </
item>
      </
options>
    </
a:item>
  </
attributes>
  <
template type="string">$#{price}</template>
  
<basePrice type="string">5</basePrice>
  <
oldPrice type="string">5</oldPrice>
  <
productId type="string">1</productId>
  <
chooseText type="string">Choose option...</chooseText>
</
root>
 
Magento Community Magento Community
Magento Community
Magento Community
 
chapagain
Sr. Member
 
Avatar
Total Posts:  202
Joined:  2009-07-19
 

it’s like this :-

var attributeId 76;

var 
options spConfig['config']['attributes'][attributeId]['options'];
var 
optionsId options[0]['id'];

var 
product spConfig['config']['attributes'][attributeId]['options'][0]['products'];
var 
productId product[0];

Hope this helps. Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top