2

Is there any way to set an attribute option's position per product?

To clarify with a simple scenario, we sell blue shirts and pink shirts. For products in a women's category, we want the pink shirts first in the list, for men, we want blue.

Thanks!

2
  • Can you reiterate the question? The question sounds like you want to reorder product attributes (like in the "additional info" section of the product view page), but the example sounds like you want to sort products under a category.
    – Sam
    Commented Jun 3, 2013 at 20:37
  • Sure. For example, for this product link. The order of the colors is set by the attribute option's position. I'm looking for a way to set the order of the colors per product instead. Commented Jun 3, 2013 at 21:00

2 Answers 2

2

Ok, solved this for us. Here's what I did.

First I created a textarea attribute (code: attribute_order_override) for configurable products to allow the content editors to enter a JSON snippet like this:

{
    "color": [
        "Athletic Grey", 
        "Athletic Blue"
    ]
}

Then in app/design/frontend/.../default/template/catalog/product/view/type/options/configurable.phtml. I created an "order reference array":

$orderReferenceArray = array();
$attributeOrderOverride = $_product->getResource()->getAttribute('attribute_order_override')->getFrontend()->getValue($_product);
if ($attributeOrderOverrideArray = Mage::helper('core')->jsonDecode($attributeOrderOverride, true)) {
    foreach($attributeOrderOverrideArray as $k => $v) {
        if (is_array($v) && count($v) > 1) {
            $attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $k);
            $orderReferenceArray[$attribute->getAttributeId()] = array();
            $options = $attribute->getSource()->getAllOptions(false);
            foreach($v as $k2 => $v2) {
                foreach($options as $option) {
                    if ($option['label'] === trim($v2)) {
                    $orderReferenceArray[$attribute->getAttributeId()][$option['value']] = $option['label'];
                    }
                }
            }
        }
    }
}

Then, in our case, we in our catalog helper, $this->helper('catalog'), we parse the JSON configuration from Magento with PHP to build our attribute structure. So before we build out the HTML, I check if there's an overriding "order reference" for these attribute options, and, if so, swap the options around. Otherwise, they'll use the position set in the Attribute in the admin area:

    $configData = Mage::helper('core')->jsonDecode($jsonConfig, true);

    if (count($orderReferenceArray) > 0) {
        foreach($configData['attributes'] as $k => $v) {
            if (count($v['options']) > 1 && isset($orderReferenceArray[$k])) {
                $tmpOptions = array();
                foreach ($v['options'] as $v2) {
                    $tmpOptions[$v2['id']] = $v2;
                }
                $configData['attributes'][$k]['options'] = $this->sortArrayByArray($tmpOptions, array_keys($orderReferenceArray[$k]));
            }
        }
    }

And the sortArrayByArray method is:

public function sortArrayByArray($array,$orderArray) {
    $ordered = array();
    foreach($orderArray as $key) {
        if(array_key_exists($key,$array)) {
            $ordered[$key] = $array[$key];
            unset($array[$key]);
        }
    }
    return $ordered + $array;
}
0
1

How about:

  1. sort them using usort and define a custom function to take into consideration your variables (gender, color)

  2. Create a new module where you can define the order based on the gender:

  • base attribute value (in your case gender)
  • attribute value (color: gray, blue, pink)
  • sort order

and implement this logic in the part that pulls the list of possible attribute values.

Note: Souldn't your products be configurable based on gender and color, not just color?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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