Re-order sort orders in toolbar - magento

What is the best way to re-order the available sort orders shown in the product list toolbar? Currently, we have 3 sorting options available: Name, Price, Most Popular (in that order). I want to have Most Popular be the left-most item since it is our default sort option. I can write a custom module to extend "getAvailableSortOrders" or something like that, but I thought there had to be an easier way. Anyone have a recommendation?
Brian

The best way I've found so far is to make a copy of
/app/code/core/Mage/Catalog/Model/Config.php
at
/app/code/local/Mage/Catalog/Model/Config.php
and then edit your local copy of the file at line 341
//before
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
//after
$options = array(
'name' => Mage::helper('catalog')->__('Name'),
'price' => Mage::helper('catalog')->__('Price'),
'position' => Mage::helper('catalog')->__('Best Value')
);
I wanted name to be first, followed by price, and I wanted position to be renamed "Best Value" on the frontend. Since position wouldn't really mean anything to a customer.
I was inspired by this comment on Inchoo.

#jon.niesen: your solution worked only partially. I had no problems renaming 'position' but when it comes to renaming 'name', Magento was very stubborn on this and was still displaying "Name" in the select drop down box.
Maybe in 1.4.1.1 "Name" is hardcoded or something?

Related

Comparing laravel collections

I have two collections: "Instructions" and "Known". Basically I am taking a new set of "Instructions" and checking whether anything is different to what is "Known".
So, the quantity is not massive. I retrieve the info:
$Instructions = Instruction::all();
$Knowns = Known::all();
Now, I'm looking for the differences, and I've tried each of these three methods:
$IssuesFound = $Instructions->diff($Knowns);
$IssuesFound = $Instructions->diffKeys($Knowns);
$IssuesFound = $Instructions->diffAssoc($Knowns);
The thing is, an "Instruction" or "Known" is an item with 17 attributes, and anyone of those attributes can be different. I want to compare the attributes of an "Instruction" with the matching attribute of a "Known". (Both items have the same keys, bot items have a Reference attribute to act as a unique identifier.
What I'm finding is that theese methods give me the item that is different, but doesn't tell me which individual attributes are the mismatch.
foreach ($IssuesFound as $issue)
{
dd($issue);
}
So a method like $IssuesFound = $Instructions->diffKeys($Knowns); will come up with item xxx being different, but I can't see how to find out which attribute of the item it is that is different. Not unless I start nesting loops and iterating through all the attributes - which I'm trying to avoid.
How do I do it?
Thanks in advance. (Laravel 5.6)
Straight from laravel docs, diffAssoc will return what you are asking:
$collection = collect([
'color' => 'orange',
'type' => 'fruit',
'remain' => 6
]);
$diff = $collection->diffAssoc([
'color' => 'yellow',
'type' => 'fruit',
'remain' => 3,
'used' => 6
]);
$diff->all();
// ['color' => 'orange', 'remain' => 6]
You get the attribute from the FIRST collection that is different on the SECOND collection, therefore if you get 3 attributes when calling $diff->all() you will know WHICH attributes ARE DIFFERENT, so you could access them or do whatever you want to, if you post more specific results of what you are getting and what you are trying we can help, but I think you are just not thinking how to use these methods

Custom Promotion for Selected Products When Using Specific Shipping Method

I'm a trying to create a module in Magento that will allow the admin to select products and categories that are eligible for a discount on shipping when using a specific shipping method.
I began by making a custom section in System->Config and adding an enable/disable option using the system.xml file for each product individually. When the rate for the shipping method is calculated, I would scan the items in the cart and if one was enabled in the admin, the discount would be applied.
My problem is that this method is only really feasible for stores with a small amount of products. I suspect that there is a better way to solve this problem within Magento, but I have not had much luck finding any information on the topic so far. Is there a better way to accomplish this task?
1) Create an attribute 'eligible_for_discount' with options Yes/No
2) Create a CSV file having product_id and assign attribute 'eligible_for_discount' with values 'Yes/No' (i.e. 1 or 0) to whichever products you want to have discount applied.
3) Import the CSV file from Magento Admin.
4) On the cart page you can check if the product contains the attribute value for 'eligible_for_discount' and if Yes, then apply discount.
While the other answer was great as a guide, there were a couple of things that gave me some trouble that I want to try and clarify. I decided to create a product attribute using an install script. I placed the following code in my mysql4-install-0.1.0.php file:
$entity = $this->getEntityTypeId('catalog_product');
$this->addAttribute($entity, 'reduced_shipping', array(
'type' => 'varchar',
'input' => 'boolean',
'label' => 'Product Qualified For Free Shipping',
'visible' => true,
'default' => '0',
'required' => true,
'used_in_forms' => array(
'adminhtml_checkout',
'adminhtml_customer',
'admin_html_customer_address',
'checkout_register',
'customer_account_create',
'customer_account_edit',
'customer_address_edit',
'customer_register_address',
)
));
While boolean is a valid form of input when creating attributes, it looks like it cannot be used to store the attribute type. This can be solved by using varchar instead. After the setup script has been run, the admin can go to Catalog->Manage Products, where they can select Yes or No for the Free Shipping attribute on each item.
Finally, I used the following in the collectRates() method of the Carrier that I created to cycle through the items in the customer's cart and see if the admin has selected them for free shipping.
$quote = Mage::getModel('checkout/session')->getQuote();
$itemList = $quote->getAllVisibleItems();
foreach($itemList as $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
if($product->getReducedShipping() == 1)
{
//update the price of shipping here
}
}
I've skimmed over a couple things to keep this answer from getting too long, so let me know if there is any part that I can clarify.

Custom Magento Report for Taxable/Non-Taxable sales

Let me preface by saying I'm new to Magento as well as Data Collections in general (only recently begun working with OOP/frameworks).
I've followed the excellent tutorial here and I'm familiar with Alan Storm's overviews on the subject. My aim is to create a custom Magento report which, given a start/end date, will return the following totals:
Taxable Net (SUM subtotal for orders with tax)
Non-Taxable Net (SUM subtotal for orders without tax)
*Total Gross Sales (Grand total)
*Total Net Sales (Grand subtotal)
*Total Shipping
*Total Tax
*For these figures, I realize they are available in existing separate reports or can be manually calculated from them, however the purpose of this report is to give our store owner a single page to visit and file to export to send to his accountant for tax purposes.
I have the basic report structure already in place in Adminhtml including the date range, and I'm confident I can include additional filters if needed for order status/etc. Now I just need to pull the correct Data collection and figure out how to retrieve the relevant data.
My trouble is I can't make heads or tails of how the orders data is stored, what Joins are necessary (if any), how to manipulate the data once I have it, or how they interface with the Grid I've set up. The existing tutorials on the subject that I've found are all specifically dealing with product reports, as opposed to the aggregate sales data I need.
Many thanks in advance if anyone can point me in the right direction to a resource that can help me understand how to work with Magento sales data, or offer any other insight.
I have been working on something extremely similar and I used that tutorial as my base.
Expanding Orders Join Inner
Most of the order information you need is located in sales_flat_order with relates to $this->getTable('sales/order')
This actually already exists in her code but the array is empty so you need to populate it with the fields you want, here for example is mine:
->joinInner(
array('order' => $this->getTable('sales/order')),
implode(' AND ', $orderJoinCondition),
array(
'order_id' => 'order.entity_id',
'store_id' => 'order.store_id',
'currency_code' => 'order.order_currency_code',
'state' => 'order.state',
'status' => 'order.status',
'shipping_amount' => 'order.shipping_amount',
'shipping_tax_amount' => 'order.shipping_tax_amount',
'shipping_incl_tax' => 'base_shipping_incl_tax',
'subtotal' => 'order.subtotal',
'subtotal_incl_tax' => 'order.subtotal_incl_tax',
'total_item_count' => 'order.total_item_count',
'created_at' => 'order.created_at',
'updated_at' => 'order.updated_at'
))
To find the fields just desc sales_flat_order in mysql.
Adding additional Join Left
Ok so if you want information from other tables you need to add an ->joinLeft() for example I needed the shipment tracking number:
Create the Join condition:
$shipmentJoinCondition = array(
$orderTableAliasName . '.entity_id = shipment.order_id'
);
Perform the join left:
->joinLeft(
array('shipment' => $this->getTable('sales/shipment_track')),
implode(' AND ', $shipmentJoinCondition),
array(
'track_number' => 'shipment.track_number'
)
)
Sorry I couldn't go into more depth just dropping the snippet for you here.
Performing Calculations
To modify the data returned to the grid you have to change addItem(Varien_Object $item) in your model, basically whatever is returned from here get put in the grid, and well I am not 100% sure how it works and it seems a bit magical to me.
Ok first things first $item is an object, whatever you do to this object will stay with the object (sorry terrible explanation): Example, I wanted to return each order on a separate line and for each have (1/3, 2/3, 3/3), any changes I made would happen globally to the order object so they would all show (3/3). So keep this in mind, if funky stuff starts happening use PHP Clone.
$item_array = clone $item;
So now onto your logic, you can add any key you want to the array and it will be accessible in Grid.php
For example(bad since subtotal_incl_tax exists) :
$item_array['my_taxable_net_calc'] = $item['sub_total'] + $item['tax'];
Then at the end do:
$this->_items[] = $item_array;
return $this->_items;
You can also add more rows based on the existing by just adding more data to $this->_items[];
$this->_items[] = $item_array;
$this->_items[] = $item_array;
return $this->_items;
Would return same item on two lines.
Sorry I have started to lose the plot, if something doesn't make sense just ask, hope this helped.
Oh and to add to Block/Adminhtml/namespace/Grid.php
$this->addColumn('my_taxable_net_calc', array(
'header' => Mage::helper('report')->__('Taxable Net'),
'sortable' => false,
'filter' => false,
'index' => 'my_taxable_net_calc'
));

magento add last login to customer grid

I am new to Magento. I am trying to add a last login value displaying on customer grid. it returns a null value. I have read other tutorials, but it does not help much. The Magento version is 1.7. Here is my code:
$customer = Mage::getSingleton('customer/session')->getCustomer();
$logCustomer = Mage::getModel('log/customer')->load($customer ->getId());
$lastVisited = $logCustomer->getLoginAt();
$this->addColumn('$lastVisited', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => '$lastVisited',
'gmtoffset' => true
));
Magento stores the login time in the following table:
log_customer
But also, this data is cleaned periodically (see: Mage_Log_Model_Resource_Log::_cleanCustomers which is triggered via Magento cron).
There are different ways to approach your task.
1) Non-persistent - I am just interested to see recent data (I can ignore that log_customer is cleaned periodically)
In this case you can just rely on the data from log_customer and display it in Manage Customers Grid.
Extend Mage_Adminhtml_Block_Customer_Grid and in _prepareCollection add the following:
$collection->getSelect()->columns(array('last_login_at' => new Zend_Db_Expr ("(SELECT login_at
FROM `log_customer`
WHERE customer_id =e.entity_id
ORDER BY log_id DESC
LIMIT 1)")));
before: $this->setCollection($collection);
Note: use the proper Magento function to get log_customer table name, my query is just for example
2) Persistent - I want to always to see the data
add a new attribute to the customer entity called: last_login_at
(datetime).
add an observer to custom_login event to update this
attribute.
use addColumn function in the grid to display this new attribute.
#user1414056
Regarding your code:
bixe made a fair point related to '$lastVisited' (this just shows
lack of experience in php programming
you seem to also be new to programming (in general) because the addColumn is called only once... do how do you expect your code to make sense?
With a better understanding of Zend Framework and OOP Programming in general you will be able to actually work and get things done with Magento.
Your '$lastVisited' can't work : in php variables are evaluated in a String only when they're in a double quote.
EDIT:
Ok the column system of magento only display value when they're available in the collection linked to the grid..
You'll have to add the log information you want to display in the grid collection.
For an example, take a look at Mage_Adminhtml_Block_Customer_Online_Grid::_prepareCollection()
When it's done, you will add your column with :
$this->addColumn('login_at', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => 'login_at',
'gmtoffset' => true
));

Magento attributes with different sorting

I am new to php, What i want is if i can define sorting order to ascending to only products that are showing by price by doing something like this in the file
Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort
$options[] = array(
'label' => Mage::helper('catalog')->__('Price'),
'value' => 'price'
'getCurrentDirection' => 'asc'
);
and rest of the attributes with descending order.
Unfortunately, doesn't seem to be working.
Can anyone help?
I think you're looking in the wrong file. The ListSort file you describe above merely lists the available options for sort by. It does nothing to the current sort.
Also, the file you referenced to is in the Adminhtml scope. If you need to change the default sort on the frontend, you should look elsewhere.
Your question, if I understand correctly, is how to sort ASC by default if price is selected for sort by, while sorting DESC by default is another attribute is used for sort by.
For the frontend, you should take a look at the getCurrentOrder() function in the Mage_Catalog_Block_Product_List_Toolbar file. Here you have both the default direction and the sort order available. It is not good practice to hack app/core/Mage files, but you could copy this file and place it in app/local/Mage/* (exact same dir as the core file) and it will automatically overload the default method.
For the backend, you could look at the _prepareCollection() function in the Mage_Adminhtml_Block_Widget file. The default sort is 'desc', so all you need to do is change it to 'asc' for price. Here too, you should make a copy in app/local/Mage/*. You could try something like this (For Magento 1.7.1.0, this is line 507-508):
change
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
$dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
to
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
if($this->getVarNameSort() == 'price') {
$dir = $this->getParam($this->getVarNameDir(), 'asc');
} else {
$dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
}
I hope this helps!

Resources