2

related.phtml is in

app/design/frontend/enterprise/default/template/targetrule/catalog/product/list/related.phtml

I wanted to order the products by sku in app/code/core/Enterprise/targetRule/Block/Product/Abstract.php we have a getItemcollection which will be used in related.phtml and it is so weird that it gets a position behavior and the position behavior is abstract protected and I cannot understand it, more over I tried to write a function to sort the $this->_items array by sku but the problem is that it's data is protected and I don't know how to call it

here is what I did in abstract.php:

public  function orderProductItemsByModel()
    {
        if (!is_null($this->_items)) {
            if ($this->isShuffled()) {
                // shuffling assoc
                $ids = array_keys($this->_items);
                shuffle($ids);
                $items = $this->_items;
                $this->_items = array();
                foreach ($ids as $id) {
                    $this->_items[$id] = $items[$id];
                }
            } else {

           usort($this->_items, "sort_by_sku");
            }
            $this->_sliceItems();
        }
        return $this->_items;
    }
function sort_by_sku($a, $b) {
    return strcmp($a["data"]["sku"], $b["data"]["sku"]);
}

the problem is $a["data"]["sku"] I think I cannot get the data if you have idea about getData() function would you please let me know what i should use?

so it is not working here is print_r of $this->_items:

Array
(
    [3446] => Mage_Catalog_Model_Product Object
        (
[_data:protected] => Array
                (
                    [entity_id] => 44444
                    [entity_type_id] => 4
                    [attribute_set_id] => 4
                    [type_id] => simple
                    [sku] => BC4R

I don't know id I should change item.phtml or I went my way correct

Thanks in advance

1

I found that I can loop through them and then sort them I didn't want to do that because the sort function is more neat and fast but I had not any choice so I used for loops:

  public  function orderProductItemsByModel()
{
    if (!is_null($this->_items)) {
        if ($this->isShuffled()) {
            // shuffling assoc
            $ids = array_keys($this->_items);
            shuffle($ids);
            $items = $this->_items;
            $this->_items = array();
            foreach ($ids as $id) {
                $this->_items[$id] = $items[$id];
            }
        } else {
            $newarray=array();
            foreach ($this->_items as $key => $value) {
                foreach ($value as $k=> $v)
                {
                    if($newarray[$key]=="")
                    $newarray[$key]  = $v['sku'];

                }
                    }
                    asort($newarray);
                foreach ( $newarray as $k=>$v)
                $keys[]=$k;
                $result=array();
        foreach($keys as $k=>$v) {
            $result[$v] = $this->_items[$v];
            }
                $this->_items=$result;
        }
        $this->_sliceItems();
    }
    return $this->_items;
}
1

Do not be fooled by a programmer, this does NOT require custom programming. Sorting products by attributes is a common problem with a very simple solution.

Here's how you add a custom attribute to the sortable options for customers

  1. Step 1 – Login to your Administration
  2. Step 2 – Go to the Manage Attributes page
  3. Step 3 – Locate the Attribute that you wish to sort by
  4. Step Four – Set the Attribute to be used for sorting

Once the Attribute options page comes up for you, scroll to the very bottom. It’s here that you’ll find the option to set this attribute to be used for sorting in the product listings pages. Change the drop down’s option to “Yes” and save the attribute.

Here's how to make that option a default

Once you have got the attribute to show up, you will need to set it as default. You can load the specific category options or you can load the websites global configurations, depending on how default you would like to make the option. In either of these sections the default sorting option is pretty easy to find.

  • thank you but I wanted to be able to sort by many of them like rice,sku,.. will it provide me a combobox to refresh the website? II don't think so I wrote it manually – Nickool Apr 2 '14 at 15:26
  • Yes it will also display in the sorting drop down box, so the users can choose how they want to sort. – Ecommerce Consultant Apr 2 '14 at 15:56
  • I changed the price attribute sorting to yes but nothing happened but if it really is there it is absolutely amazing! – Nickool Apr 2 '14 at 16:57
  • maybe because of custom theme I am not seeing anything – Nickool Apr 2 '14 at 16:59
  • The themes are not normally involved in the collection creation and management. Unless somebody coded something that they shouldn't have, this should work fine. – Ecommerce Consultant Dec 19 '14 at 20:51
0

magento uses magic functions, try using getData() to get _data:protected, example:

$data = $variable->orderProductItemsByModel();

print_r($data->getData);
  • but I should use something like $this->_items->getData() which will not work – Nickool Apr 1 '14 at 18:44

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.