$ap_product is the associated product (simple product). Even though its a simple product, I can't use the method getResource(), because $ap_product is in catalog/product_type_configurable class. I know multiple ways to get the value of color attribute (ex: 255 stands for red) but I can't get the actual text "Red".
getResource()->getAttribute("color");
if($attribute->usesSource()){
$apColorSizeValue = $attribute->getSource()->getOptionValue($test);
}
Also this method would not work with, because its an associated product, not just a simple product.
$_product->getAttributeText('color');
I've also tried to reload a product model but loading that associated product id. That didn't work either. So right now I have the associated product id and associated product attribute color value to work with. I need to get associated product attribute color text.
This work for me
$_associatedProduct = $ap_product->getId();
$_colorAttributeId = Color_ID
$query = "select cpei.value as id,eapv.value as value from catalog_product_entity_int cpei LEFT JOIN eav_attribute_option_value eapv on cpei.value = eapv.option_id where cpei.entity_id=" . $_associatedProduct. " and cpei.attribute_id = " . $_colorAttributeId . " limit 1";
$color = $_readConnection->fetchAll($query);
Thank
Related
Hope you are all good and in great shape.
TLDR: How can I filter my product grid on the admin side using a WEIGHT RANGE?
I am a newbie in Magento and I have been doing some progress over the last months on the basics of customizing Magento to our small company. I had a request from the sales team which I am struggling to get to term. We sell a kind of product which has a different weight to every item and the final price of the product is calculated by multiplying a variable rate by the weight. It makes the weight a very important attribute in the system not only for the sales/logistics, but also for the client's decision making. Therefore, I need to filter the product catalog grid on the admin side by weight, but not by a specific number, instead, I need a RANGE of weight. (From ...kg to ...kg) I managed to create the field, but I am struggling on the part were the action happens. Please take a look on the screenshots below:
Weight range in filters
Weight in columns
This is the structure of the module that I created:
module structure
And here is the code on the file AddWeightFieldToCollection.php which I am struggling with:
<?php
namespace Diamwill\WeightFilter\Ui\DataProvider\Product;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
class AddWeightFieldToCollection implements AddFieldToCollectionInterface
{
public function addField(Collection $collection, $field, $alias = null)
{
$mg_eav_attribute = $collection->getResource()->getTable('mg_eav_attribute');
$mg_catalog_product_entity_decimal = $collection->getResource()->getTable('mg_catalog_product_entity_decimal');
$collection->getSelect()->joinLeft(
['soa' => $mg_eav_attribute],
'soa.entity_type_id = 4 AND soa.attribute_code = \'weight\'',
null
);
$collection->getSelect()->joinLeft(
['dcrt' => $mg_catalog_product_entity_decimal],
'soa.attribute_id = dcrt.attribute_id',
['code']
);
$collection->joinField(
'weight_range',
'mg_catalog_product_entity',
'value',
'mg_catalog_product_entity.entity_id = soa.entity_id AND soa.store_id = 0',
null,
'left'
);
}
}
Basically, I know that I have to join the product tables to get weight, which in SQL terms would be something like this:
SELECT
w1.value AS 'weight'
FROM
mg_catalog_product_entity AS prod
LEFT JOIN
mg_catalog_product_entity_decimal w1 ON
prod.entity_id = w1.entity_id
AND w1.store_id = 0
AND w1.attribute_id = (SELECT
attribute_id
FROM
mg_eav_attribute
WHERE
attribute_code = 'weight'
AND entity_type_id = (SELECT
entity_type_id
FROM
mg_eav_entity_type
WHERE
entity_type_code = 'catalog_product'));
Which I am not capable of linking with the Collection class and apply to the column, in order to filter. Can someone help me with the code? I am happy to read a tutorial to learn or to get a certain book/article with more information on the topic.
Please, I do not want to buy a plugin/module in order to do that. I would like to learn how to do it.
Thanks to anyone who can help me.
Have a great day!
I'm newbie to smarty and php.
My situation is that my product_reference is unique but product_ean13 is similar in some products which are like each other. What I need is to have product_id of those products in one of them product page. I mean when visitors open a product page I want to show Image of those products which have the same ean13. Showing the images and HTML, CSS is ok for me, my problem is in PHP,SMARTY which should pass the value from PHP file to TPL file.
I guess I should write a function in Product.php file and pass array values to product.tpl file. But I couldn't.
Would you please help me?
Edited: As you may know a weakness in Prestashop 1.6 is that if your products nature is to have colors and size, like clothes and smartphones! you have two approach to create them. first approach is to create them as a combination of one product and second approach is to create them as separated and not related products. first approach has a good point that all of them will be showed up in product page when customer visits each of them and also have a weakness of exposure in category page which all those attributes will be seen as one product.(imagine between all colors of one shirt only one is seen and your costumer may like blue more but always the red is being showed in category page. or its not easy to know from category page that you also have the gold color of the smartphone) this solution will helps you what to create your products as separated products but you what to show them in product page of each of them. this way we will use benefit of both approach and we wont have the weakness of any of them. we use ean13 (or any other unused field you have, to use as a code that is a same value in same products)
I you have created the function in product.php, you should use it in ProductContronller.php and here you can assign a value that contains the list of your products wich has the same ean13.
we should add a class in product.php to get cover images of products with the same ean13:
public static function getImageByEan13Product($ean13_colors)
{
$rows = Db::getInstance()->executeS('
SELECT `id_product`, `id_image`
FROM `'._DB_PREFIX_.'image`
WHERE `id_product` IN ('.implode(',', $ean13_colors).') AND `cover` = 1'
);
$images = array();
foreach ($rows as $row){
$images[] = array(
'idProduct' => $row['id_product'],
'idImage' => $row['id_image']
);
}
return $images;
}
and the following function which gets the color attributes of the products which contain same ean13 code:
public static function getColorByEan13Product($ean13)
{
$rows = Db::getInstance()->executeS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'product`
WHERE `ean13` = '.$ean13.' AND `active` = 1 AND `id_product` IN (SELECT DISTINCT `id_product` FROM `'._DB_PREFIX_.'stock_available` WHERE `quantity` > 0)');
$colors = array();
foreach ($rows as $row){
$colors[] = $row['id_product'];
}
return $colors;
}
and also there is another class in the ProductController.php named assignAttributesGroups() which provide attributes of the product. in this class we should add following code:
$ean13_colors = array();
$ean13_colors = Product::getColorByEan13Product($this->product->ean13);
$this->context->smarty->assign('ean13colors', $ean13_colors);
$ean13_images = array();
$ean13_images = Product::getImageByEan13Product($ean13_colors);
$this->context->smarty->assign('ean13images', $ean13_images);
then these two values ean13_images and ean13_coloes can be used in TPL file product.tpl and showed up instead of color attributes. like the code bellow:
{foreach from=$ean13images key=k item=v}
{if $product->id != $v.idProduct}
<li>
<a href="{$link->getProductLink($v.idProduct)|escape:'html':'UTF-8'}" class="pro_column_left selected">
<img src="{$link->getImageLink($product->link_rewrite, $v.idImage, 'small_default')}" height="{$smallSize.height}" width="{$smallSize.width}" class="replace-2x img-responsive" {if !isset($from_product_secondary_column) || !$from_product_secondary_column} itemprop="image"{/if} />
</a>
</li>
{/if}
{/foreach}
and the value ean13colors can be used where we want to show a balloon tooltip on mouseover the image to say whats the color attribute of the other product that is being showed by same ean13.
Duplicated attriubte set for new product line.
Attributes set show in backend and front end.
Created new attribute and added to attribute set.
New attribute show's in back end but not front end.
If I add the new attribute to the origonal set that was duplicated the new attribute will now show on the front end for the product assoiated with the duplicated attribute group.
So basicly A new attribute added to a duplicate set won't show on the front end until its added to the attribute set that was duplicated.
I've checked to make sure the attribute is visable on front end etc and tried it several times checking the settings.
The goal is to be able to duplicate a attribute set and add new attributes for different product types. Then call the folder by id and display the assoiated attributes.
I've called the attribute group by ID (spec's). This code is working.
<?php
require_once 'app/Mage.php';
Mage::app();
$attributeGroupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
$product = $this->getProduct();
foreach ($attributeGroupCollection as $attributeGroup) {
$attributeGroup->getAttributeGroupId();
$attributeSpecs = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter(41);
}
?>
Help is appreciated, thanks
Duplicating an attribute set is duplicating at a time (the moment when you click the button). If you want to add an attribute in both attribute sets, you need to create it in both, for that, you just need to create your attribute and drop it in the section you want to have it linked to (what you call "folder"). If you want to do it programmatically. You need to create an observer that will be fired after the attribute set is saved and that will add the attribute to the duplicated attribute set. I would not advise to do so because it means that you need to enter the ID of the attribute set duplicated and it can be tricky thing when it comes to pushing the development to the production. I am sure there is a workaround for what you want to achieve.
The problem was is magento creates a new id for each new group folder in the new duplicated attribute set (makes sense). I was calling by ID for the group and as a result will only show attributes in the origional folder (weird) even if the product was assoicated with the new attribute set. What I did was get the current attribute set id and then sort out the attribute groups by name so even if the attribute sets are coppied as long as the folde has the name it will display in the custom loop displaying the attributes. Here is my working code:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
$attributeSetID = $attributeSetModel->getAttributeSetID();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetID)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
if (strpos($groupName , 'Specifications') !== false) {
echo 'true';
$specificationsNum = $groupId;
};
if (strpos($groupName , 'eatures') !== false) {
echo 'true';
$prodfeaturesNum = $groupId;
};
}
//echo $specifications;
$specifications = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($specificationsNum);
$prodfeatures = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($prodfeatures);
I have created a new IMEI attribute of type textarea for all products, see from the image. Can anyone point out a function to update the its value. I have the code like the following.
$this belogns to Mage_Sales_Model_Order.
foreach ($this->getAllItems() as $item) {
$item->setImei('123');
$item->save();
echo $item->getImei();
}
I am getting 123 from the last statement but when I am viewing from admin. Its not changing there. Also in which table the attribute and value will be stored, So I can debug from there.
What class is $this->getAllItems() is it Mage_Catalog_Model_Product?
If it not Mage_Catalog_Model_Product then load the product by id and save the product
foreach ($this->getAllItems() as $item) {
$product = Mage::getModel('catalog/product')->load($item->getId() or $item->getProductId())
$product->setImei($product->getImei() . '123');
$product->save();
}
The values of catalog product attributes of type text are stored in the table catalog_product_entity_text. An SQL would be
select * from catalog_product_entity_text where attribute_id = {insert your attribute id} and entity_id = {insert your product id}
The query will return results for every store view in the system.
The reason why you do not see a change to the attribute in the backend is probably because the new value is set for a different website/store than the one loaded in the backend.
You are already using a correct way to set the attribute value assuming $item is of type Mage_Catalog_Model_Product:
$item->setImei('123');
$item->save();
heres how you do it differently: (color = attribute name, red = attribute value id)
starts assuming you already have $product available.
$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
$avid = $attr->getSource()->getOptionId('red');
$product->setData('color', $avid);
$product->save();
}
Hi i have got the maximum elements of products using below code but it doesn't show the size attribute of my product, the size attribute is visible on front-end but i cant understand why it is not printing with this code
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId); //load the product
//$product_id = $product->getId();
//$created_at = $product->getcreated_at();
//$description = $product->getdescription();
//$short_description = $product->getshort_description();
//$sku = $product->getsku();
//$size_fit = $product->getsize_fit();
//$style_ideas = $product->getstyle_ideas();
//$name = $product->getname();
//$price = $product->getprice();
//$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
If its the size_fit attribute (i'm guessing that because its the only size attempt in your code..) use $product->getSizeFit(). For just size use $product->getSize(). When this is not returning anything, please post the attribute installer if you have one. Mufadall his answer is also correct but judging your code you are just using wrong syntax.
Basicly according to the magic get method the first letter is turned into a capital and all other letters after an underscore.
Ex.: To fetch my_sample_attribute use getMySampleAttribute().
getData('my_sample_attribute') would also be an option but you shouldn't make a habbit of doing that because in some cases, for some attributes getData('attribute') returns a different value then getAttribute()....
$product->getData($attribute_code);
will return you the actual attribute value. For attribute with type dropdown it will return option id
$product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
will return actual value
You can use this:
$product->getData('Your Attribute ID');
Go to Size attribute and check for Drop Down used in product listing if it set to No then set it to Yes, after that you can get your size attribute with other product attribute
You can use the getters, or getData();
The getter is set in magento with the magic __get() method, and you can use it in the following way:
$product->getDescription() // ( to get description attribute)
$product->getShortDescription() // (to get short_description attribute)
So basically you explode the attribute with underscores, and capitalize the words, and you will get what you need to put after "get".
Here is something very useful I use all the time
Zend_Debug::dump($product->getData());
This gets you all the data you have to work with. If you are missing data, it means it's not loaded.
Good luck!