get data for current product from custom table magento - magento

I have created custom table and model, I can access all data by this model using this code
$model = Mage::getModel('groupprice/groupprice');
$data = $model->getCollection()->getData();
print_r($data);
I just want to filter data only for current product, foreign key in custom table for product is product_id .
How I can achieve this?

Try this:
$productId = 1;//change to your product id
$collection = Mage::getModel('groupprice/groupprice')->getCollection()
->addFieldToFilter('product_id', $productId);
foreach ($collection as $item) {
//do something with $item
//for example print_r($item) to see all it's data
}

Related

How to insert in Laravel?

I have output object $product after inserting data into table Product:
$product = Product::create($data);
In model Product I have:
public function images()
{
return $this->hasMany("App\ProductImage");
}
The I try to add images to table product_images:
$product->images()->save(new ProductImage(UploadFiles::$files));
Relationship beetwen tables is product.id = product_images.
So, after inserting I get message:
General error: 1364 Field 'image' doesn't have a default value (SQL: insert into `product_images` (`product_id`) values (21))
OK, firstly let's deal with saving one image:
Assuming you have a product_images table with id, image, product_id, you would attach an image like so:
$image = new ProductImage(['image' => 'myImage.jpg']);
$product->images()->save($image);
Now it looks like you have an array, so you can't save that directly, you need to put it in a for loop. I don't know the format of your UploadFiles::$files, but I'll assume it's just a list of file names to save:
$files = UploadFiles::$files;
foreach($files as $file){
$image = new ProductImage(['image' => $file]);
$product->images()->save($image);
}
Alternatively, you could create the whole thing as an array and use saveMany, which would be better:
$files = UploadFiles::$files;
$productImages = [];
foreach($files as $image){
$productImages[] = new ProductImage(['image' => $image]);
}
$product->images()->saveMany($productImages);

How to get a list of skus in magento

How to get a list of skus whiout using the looping in magento.
Example: I am using below code with my conditons.
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku');
Now I want to result as array('A001','A002'....) etc.
I don't want to iterate (loop) the product collection.
Please suggest.
If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.
$skus = array();
foreach ($productsCollection as $product) {
$skus[] = $product->getSku();
}
If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');
Retrieve the read connection you should use ->getConnection('core_read') not ->getConnection('core_write'). Whole codes is below which runs faster.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$skus = $readConnection->fetchCol('SELECT sku FROM `catalog_product_entity`');
foreach ($skus as $sku) {
echo $sku;
}

Get all existing values of attribute in the eav table

I have the attribute "country" which is varchar, it is set in every product that I have.
I need to have a list of all the values that are set for this attribute thought all the products. Basically what I want to do is the equivalent of this query:
SELECT `value` FROM `catalog_product_entity_varchar` WHERE attribute_id=147
How to do this the magento way?
The thing I am doing at the moment is to get the collection of all the products and loop through them, which is not an option.
Check below code:-
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
What about this?
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('sku');
$collection->addAttributeToSelect('country');
foreach ($collection as $product) {
// ... Your stuff here
}

Magento: How to filter a collection (sales/order) by a certain category?

I spend a lot of hours to solve this problem, but I don't get it :(
I need a selection of all ordered items from a special category. How can I filter the Collection e.g. for categoryId '44' ?
Here my code:
<?php
require_once '/home/web/public_html/app/Mage.php';
Mage::app();
//$_category = Mage::getModel('catalog/category')->load($category_id);
$salesCollection = Mage::getModel("sales/order")->getCollection();
echo $salesCollection->getSelect();
foreach ($salesCollection as $order) {
$items = $order->getAllItems();
... ?>
Thanks everyone for helping me,
best, Rik
the sales_flat_order_item database table knows nothing about the categories.
So I guess you have to use: $collection->getSelect()->join(.....);
In sales_flat_order_item (Mage::getModel('sales/order')) you can find the product_id.
In catalog_category_product (Mage::getModel('catalog/category_product')) you can find category_id, product_id, position
Now you have to join them...
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
Here's one (perhaps) not so elegant approach to doing so...
First grab all products in the category you want
$category_id = 44;
$category = Mage::getModel("catalog/category")->load($category_id);
$products = Mage::getModel("catalog/product")->getCollection()
->addCategoryFilter($category);
Next I collect just the product ids so I can use them
$product_ids = array();
foreach ($products as $product)
$product_ids[] = $product->getId();
Grab all order items where the product id is one of the products from our category
$items = Mage::getModel("sales/order_item")->getCollection()
->addFieldToFilter("product_id", array("in" => $product_ids));
Now fetch all the unique orders referenced by the items
$orders = array();
foreach ($items as $item) {
$order_id = $item->getOrderId();
if (!isset($orders[$order_id]))
$orders[$order_id] = Mage::getModel("sales/order")->load($order_id);
}

magento product collection

In extention I'm getting product collection on catalog_product_view page like this:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
$category = new Mage_Catalog_Model_Category();
$category->load($product->getCategoryId());
$collection = $category->getProductCollection();
and how can I add additional attributes to this collection?
for example I can't get something like this
$collection->addAttributeToSelect('manufacturer');
I wish to add some attribute by code (not id, because this may be different attributes added in layout) and then group data by this attribute
thnx
You could instantiate a product collection and filter it instead of getting the products of a specific category directly:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
// get a product collection and filter it by category
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($product->getCategoryId());
// add the attributes you need
$collection->addAttributeToSelect('manufacturer');
$collection->setOrder('manufacturer', 'asc');
// load the collection and return it
$collection->load();
return $collection;
Be careful: You can not add attributes to the collection after loading it!
Additionally, you do not have to explicitely call load() - the collection will be loaded on demand.
Hope this helps.
Try this
<?php echo $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product); ?>

Resources