Prestashop product combination display on product list - smarty

I am trying to get available product combination (size) in product list page on each product. Basically there is a image, product name, available combinations, price, buy button.
I tried to out put $product object, but it does not have combination variable to it.
Is there any way to achieve that?

There is function assignAttributesGroups() in the ProductController.php from where you can get code for size combination.
protected function assignAttributesGroups()
{
...
...
...
$this->context->smarty->assign(array(
'groups' => $groups,
'colors' => (count($colors)) ? $colors : false,
'combinations' => $combinations,
'combinationImages' => $combination_images
));
}
If you print the value of groups you will get desired output. The combinations has been arranged to show dropdown in product.tpl(/PRESTASHOP_FOLDER/themes/default-bootstrap/product.tpl) as shown in image
You can take a code from the function mentioned above and run it for every product in the list. You have to create a product object and receive combination through it. And create a drop down for the same.

Related

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.

Magento: Resource Model 'loadByAttribute' with multiple parameters

I need a way to locate a Magento object my multiple attributes. I can look up an object by a single parameter using the 'loadByAttribute' method, as follows.
$mageObj->loadByAttribute('name', 'Test Category');
However, I have been unable to get this to work for multiple parameters. For example, I would like to be able to do the above query using all of the following search parameters. It might look something like the following.
$mageObj->loadByAttribute(array('entity_id' => 128,
'parent_id' => 1,
'name' => 'Test Category'));
Yes, I know that you don't need all of these fields to find a single category record. However, I am writing a module to export and import a whole website, and I need to test if an object, such as a category, already exists on the target system before I create it. To do this, i have to check to see if an object of the same type, with multiple matching attributes already exists, even if it's ID is different.
This may not answer your question, but it may solve your problem.
Magento does not support loadByAttribute for multiple attributes, but instead you can do this.
$collection = $mageObj->getCollection()
->addAttributeToFilter('entity_id', 128)
->addAttributeToFilter('parent_id', 1)
->addAttributeToFilter('name', 'Test Category');
$item = $collection->getFirstItem();
if ($item->getId()){
//the item exists
}
else {
//the item does not exist
}
addAttributeToFilter works for EAV entities (products, categories, customers).
For flat entities use addFieldToFilter.
There is a special case for sales entities (orders, invoices, creditmemos and shipments) that can use both of them.

How to show the store name in the grid in the Magento

I have my custom module in the admin. I have listed the values from the database in the grid. The values are store specific, so every row have the store_id. While listing the value, i also want to display the store name and website name in the row. How can i fetch the store_name and website name in the grid. e.g i have the store_id 5 inserted in the row.
I want the output like
Location Value Store
xyz xyz English
If you put below code in your module grid file then you are able to display Store name in your grid, here's a code
$this->addColumn(’store_id’, array(
‘header’ => Mage::helper(’sales’)->__(’Website’),
‘index’ => ‘store_id’,
‘type’ => ‘store’,
‘width’ => ‘100px’,
‘store_view’=> true,
‘display_deleted’ => true,
));
Further Information you can refer this Link
So you have store_id and you want to display the store name right ?
So use below code to get the store name
$storeId = 5;
$storeName = Mage::getModel('core/store')->load($storeId)->getName();

how to get the size of product to the cart page

I have size of product (XS, XL, ...).
I want add column in cart table Size, where show sizes of products.
I added SKU in this table $_item->getSku(); This works.
But $_item->getSize(); not works. Please, help me.
try adding ->addFieldToSelect('*'), it'll add every attribute associated with your products in the returning data, you can replace the * with the attribute code of the attribute your trying to display
problem is, it'll return integers that are used in either eav_attribute_option or eav_attribute_option_value (not sure why there's 2 different tables, one has values and one has sort order, it's not like Magento even links a value to 2 options), however if you use the following code
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('ATTRIBUTE_CODE')->getFirstItem();
$sizeValues = array();
// populates sizevalue array with data
foreach ($attributeOptions as $key => $value)
{
$sizeValues[$value['value']] = $value['label'];
}
you get an array of values with their index's being that of that it returned in your collection

Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no value is assigned to the attribute.
I see you already found a solution, but there is also this option:
$collection->addFieldToFilter('parent_item_id', array('null' => true));
But if you want to use "NULL" => false, which DOESN'T WORK.
(and I noticed you can use elements such as "in", "nin", "eq", "neq", "gt"), you can do this:
$collection->addFieldToFilter('parent_item_id', array('neq' => 'NULL' ));
Hope this is still helpful...
This works for NOT NULL filters
$collection->addFieldToFilter('parent_item_id', array('notnull' => true));
Filtering a product collection by null/empty attributes has two possible solutions. Magento uses an INNER JOIN to grab the values of the attributes to filter. BUT if the product attribute is not assigned a value the join will fail, as a database table / relationship is missing.
Solution #1: Use addAttributeToFilter() and change the join type from "inner" (the default) to "left":
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('custom_attribute', array( ... condition options ..), 'left');
Solution #2: Make sure your custom attribute has a default value. Magento is conservative in this regard. Magento will only create the relationship between an attribute and a product if a value is given for the attribute. So in the absence of user-specified value or a default value the attribute will not be accessible for filtering a product even if the attribute appears in the product detail view under the admin panel.
Because the question does not match exactly the title of the question and I found the them multiple times by searching for a condition like: special VALUE OR NULL
If you want to filter a collection matching a VALUE OR NULL, then you can use:
$collection->addFieldToFilter('<attribute>', array(
array('eq' => '<value>'),
array('null' => true)
));
You don't need to use addFieldToFilter.
now the solution:
attributes name is known as code in magento, you just need to use the code below to get all of the products which have a specific attribute as an array
$prodsArray=Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('custom_attribute_code')
->getItems();
you can also specify certain conditions for attribute's value in addAttributeToFilter in the second parameter of addAttributeToFilter.
you can find this method in this file (for further study):
app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

Resources