I use Magento 1.7.0.2.
I'd like to show the 8 latest added products in my homepage, but without setting a date "from" and "to". I need it to be automatically.
Does anyone know of a solution?
Product IDs are incremental. By ordering them descending and limiting the collection to 8 you will have 8 last products.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->order('entity_id desc')->limit(8);
/* just for testing
Mage::log($collection->getSelect()->assemble());
foreach ($collection as $product) {
Mage::log($product->getSku());
} */
With the collection you can do whatever you need, add visibility and status filter etc.
in order to do that you will need to use the date that the order was created. The key to display all products information as price, name, and etc. is to us ->addAttributeToSelect('*') Here is the script:
$store_id = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
->addStoreFilter($store_id)
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('status', 1)
->addAttributeToSelect('*')
->setVisibility(array(2,3,4))
->setOrder('created_at', 'desc')
->setPage(1, 9);
Related
I am using the Megamenu third-party module in Magento 2 and I have customized this module due to some custom requirements.
I need to count product same as display in the admin category section for the anchored category and using the below code
$category->getProductCollection()->count()
this code is returning 0 products while this category is anchored and its subcategory has some product so it should count it subcategories product same as display in the admin section.
Please advise what will the code to get the products.
Thanks,
Please make sure you have successfully reindexed after setting the anchor via php bin/magento indexer/reindex.
Then try following code:
Include product collection factory to your construct
public function __construct(
// ...
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
// ...
) {
// ...
$this->productCollection = $productCollection;
// ...
}
And count the product collection filtered by your category.
$products = $this->productCollection->create();
$products->addCategoryFilter($category);
$products->count();
I got an extension in which i want to include some filters, i know figured out that i can filter the results that are shown of my listAction() by using findBy.
I tested it and it worked like this:
$cars = $this->carRepository->findByCarid("1");
$this->view->assign('cars', $cars);
My problem now is i need to filter the result with more than one Parameter, what if i want to add findByColor("blue") so it gives me all cars wit hid 1 and color blue? What solution does extbase have for that kind of search queries? i can`t find anything good or understandable in the documentation.
You have to extend you repository and code this functionality on your own. Extbase offers you a simple but powerful API to do so.
class whatEverYourRepositoryIsCalled extends \TYPO3\CMS\Extbase\Persistence\Repository {
public function findByFilter($carId, $color) {
// Create empty query = select * from table
$query = $this->createQuery();
// Add query options
return $query->matching(
// ALL conditions have to be met (AND)
$query->logicalAnd(
// table column carId must be euqal to $carId
$query->equals('carId', $carId),
// table column color must be euqal to $color
$query->equals('color', $color)
)
);
}
}
This is a quite simple approach to your problem. In a real world scenario I would probably use an array of filter criteria to do the filtering like array('carId' => 1, 'color' => 'blue'). Inside of findByFilter() those values would be extracted and added to the query.
The key is to build the desired query. A quite comprehensive explanation of how to do that can be found at http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/. Unfortunately it's not completely up to date but the part about constructing queries is still valid.
I need to select records created at current year, with Eloquent
So far, this is the code I'm using. How can I filter the results to retrieve only the ones created in the current year?
public function vacationsbalance($typeId) {
// Get vacataions balance for existing employee.
$vacationsBalance = $this->vacations()->where('vacation_type_id', '=', $typeId)->sum('days_num');
return $vacationsBalance;
}
Assuming you have timestamps in your table (that is, you have a created_at column with the record's creation date), you can use:
public function vacationsbalance($typeId) {
// Get vacations balance for existing employee and for the current year.
return $this->vacations()
->where('vacation_type_id', '=', $typeId)
->whereRaw('year(`created_at`) = ?', array(date('Y')))
->sum('days_num');
}
Check Eloquent's documentation and look for whereRaw.
How to update all fields affected in MongoDB using cakephp. Say I have queried the Start and End Time. I want to update all the fields affected BETWEEN those time of an specific user.
<?php
$stime = $this->data["User"]["sTime"]; //$stime = "2:29 PM";
$etime = $this->data["User"]["eTime"]; //$eTime = "3:40 PM";
$user = $this->data["User"]["affected_user"];
?>
All the fields within the start and end time will be affected. I would like to update a field called status and set it to "1". Thanks
You can use the updateAll() statement to update multiple fields like.
<?php
// first of all convert the start time and end time in proper date format the use the statement like bellow.
$this->ModelName->updateAll(array('status' => 1), array('time >=' => $stime, 'time <' => $etime));
?>
If you want to update multiple fields then you can specify like status in the same array. For more information checkout the updateAll() documentation on the cakephp site.
I have to create collection for users per each day they register so if I have 5 users and 2 register on 2013-03-02 and 3 have register on 2013-03-03 I will have 2 collections
The user is declare in a YML file with no collection
In the project I do an import via CLI something like this
foreach($arUsers as $key=>$arUser)
{
$collection_name = "day_".$arUser['date'];
$user = new User();
/* Change the collection name to be dynamic */
$OdmMetaData = MeltApplication::getDocumentManager()->getClassMetaData( get_class($user) );
$OdmMetaData->setCollection($collection_name);
$user->setUserId($arUser['user_id']);
$user->setEmail($arUser['email']);
....
$this->getDocumentManager()->persist($user);
$this->getDocumentManager()->flush();
}
Problem is that when I look in the mongoDB I see only one collection with name day_2013-03-03" which is the last date , if I set the MeltApplication::getDocumentManager()->flush(); after the foreach end statement which I should it save all in the first collection 03-02 .
Are there any options to do this , to save them separate?
Thanks
$data = $odm->getClassMetadata('NameEntity');
$data->setCollection($collectionName);
This is the answer.
The only solution was to reinitialized the documentManager