I have a custom module with change the default CATALOG/RESOURCE/PRODUCT/COMPARE/ITEM/collection.php, but the line below:
->order('ai.sort_order ASC');
Change the order for position with attributes position, but a few of is (about 10 attributes) is just a text (not Dropdown, Select or Price) and cannot allow to orders correctly position iquals a product page order. (i need to leave the page to compare products in the same order from the product page of the attribute list)
how i can make this?
To create sort by position write below code after getting product collection in app/design/frontend/<theme_name>/default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.
Related
In database, I have "categories" and "products" table. In "products" table, there is a category_ids column which is an array, because the product has more than one categories.
What I want is to query the product that has category_ids of 1. I am trying to do like this, it is works, but I want a simpler way how to do.
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
if(in_array($catId, $product->category_ids)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
As you can see above,I must loop through all the products to check if category ID is exist in product categories array.
Is there a better way so it is not checking all the products?
All code is OK except one line .
$catId = 1; //category ID
$allProducts = Product::all(); // get all products
foreach ($allProducts as $product) { //loop all the products
$cats = explode(',',$product->category_ids);
if(in_array($catId, $cats)) { // check if category ID is in array of its product categories
$products[] = Product::find($product->id);
}
}
I added this line. I assume that multiple categories of a product will be stored in a single field with comma separated (Although its not the correct way, you need to create a M2M table)
$cats = explode(',',$product->category_ids);
So In in_array function, 2nd element should be an array. and Because of output of explode, $cats is an array.
in_array($catId, $cats)
I'm looking to filter a product list by those products which have a group_price set at the product level and assigned to a specific customer group.
I was thinking it would be something along the lines of:
$products->addFieldToFilter('group_price', array(
array(
'cust_group' => 2
)
));
But it appears that group_price is not an attribute. Additionally, $_product->getGroupPrice() always returns the product's price despite if a group price is set on the product or not.
Ideally, I'd prefer to filter these by the customer group code (ie. Wholesale, Retail, etc) instead of the group id (simply for the case where someone might delete a customer group and recreate it later and the id changes).
Any thoughts?
I have a similar requirement but the problem is that iterating over a large product collection is way too slow.
The catalog_product_index_price table contains group information so you may try something like:
$productCollection = Mage::getModel('catalog/product')->getCollection();
...
$productCollection->addFinalPrice();
$productCollection->getSelect()->where(
'price_index.customer_group_id = ? AND price_index.group_price IS NOT NULL',
$customer_group_id
);
I ended up using this:
$products = Mage::getModel('catalog/product')->getResourceCollection();
$products
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', array('neq' => 1));
foreach ($products as $product => $value) {
//still not sure how to get 'group_price' without running load() =(
$productModel = Mage::getModel('catalog/product')->load($value->getId());
$groupPrices = $productModel->getData('group_price');
// A helper of mine
// store the customer's groupId to compare against available group prices array below.
$currentGroupId = Mage::helper('user')->getCustomerGroup()->getId();
// remove products w/o configured group prices
if (!count($groupPrices)) {
$products->removeItemByKey($product);
}
foreach ($groupPrices as $key) {
foreach ($key as $subkey => $value) {
if ($subkey == "cust_group") {
$customerGroup = $subkey;
if ($value != $currentGroupId) {
$products->removeItemByKey($product);
}
}
}
}
};
$this->_productCollection = $products;
return $this->_productCollection;
I have added a simple code to track each purchase on the site. All products have different types of rules based on categories to subtract a predefined percent of amount of product price.
But when there are configurable products in the cart then it is displaying me both products, original product price and the price of the selected option for the product details in the order detail.
If I have a lipstick in my cart and the color I selected is "RED" then lipstick price will change. But it is displaying me the original lipstick price as well as the red lipstick price in the order details.
I have added the code on order success page.
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
foreach ($items as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
I have also tried
$tempmain = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$temp = $tempmain->getAllItems();
$total = $tempmain->getGrandTotal();
foreach ($temp as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
With the same results.
How to get only "RED" lipstick price in Order details?
Thanks in advance.
This is the details for all the availabel lipstick and their values:
when I select "Tango" as the color for lipstick on front end:
I am getting the Price of both products on success page:
Here is the Order Details Screen shot:
In frontoffice, when a customer add a configurable produt in his cart and order it, you'll see (in Database) 2 lines (2 quote_items and 2 order_item).
It's normal because magento needs to store the configurable product and the linked simple produt corresponding to the user selection (and linked to the master configurable product).
When displaying order details, Magento handles that difference by checking if a product has a parent id. You can do the same to ignore some order_item :
foreach($items as $item){
if ($item->getParentProductId() {
continue; // ignoring simple product associated to master order item (configurable)
}
// your code
}
I'm NOT 100% sure what your tying to do, but if you are on the order success page then this is the way to retrive the current order info (get the the last order for the db may not always be the current order on the success page if two user make a purchase millisecond apart)
Try adding this to Success.phtml
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
if ($item->getParentItem()) {
continue;
}
echo Mage::helper('core')->currency($item->getPrice(),true,false);
}
I have a lot of products which need to be assigned to new categories. The products exist, the categories exist, but for some reason the import is not doing something.
I am using a dataflow profile using the following settings:
(source: i.imm.io)
The first two lines of my CSV are:
sku,store,category_ids
TT010,default,Face/Acne
What values should the first line have in order to assign the products to another category?
In the end I had to override the core/mage/catalog/model/convert/adapter/product.php file.
I added the following code after
if (isset($importData['category_ids'])) {
$product->setCategoryIds($importData['category_ids']);
}
//search for a category by name.
if(isset($importData['category']))
{
$cat_ids = array();
$cats = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->load();
foreach($cats as $cat)
{
if($cat->getName() == end(explode('/',$importData['category'])))
{
$cat_ids[] = $cat->getId();
}
}
$product->setCategoryIds($cat_ids);
}
This is for magento 1.7
I'm trying to do something that would seem simple but just doesn't work
I'm running Magento 1.5.1.
I would like to show on the product page all the items that have the same attribute set as the original item.
For example:
Item A has XYZ attributes, show me all the other items that have xyz attributes.
Nothing I've tried worked so far...
EDIT:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
array('name'=>'orig_price','eq'=>'Widget A'),
array('name'=>'orig_price','eq'=>'Widget B'),
));
foreach ($collection as $product) {
//var_dump($product);
var_dump($product->getData());
}
EDIT 2:
I tried :
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('color');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addAttributeToFilter(array( array('name'=>'color','eq'=>'red')
));
foreach ($collection as $product) { //var_dump($product);
var_dump($product->getData()); }
and nothing at all showed up
First you should check that the attribute value for 'Used in Product Listing' is set to yes and than try the code below. I feel it will help you.
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('color', 'red');
foreach ($collection as $product) { //var_dump($product);
var_dump($product->getData()); }
once you get the filtered data than add other select & filter attribute as per your requirement.