How to get last running transaction Id in magento - magento

How can I get the last running transaction Id ? (eg: 10000001)
I've tried numerous ways, with no success.

I was suddenly enlightened when I looked at the problem again at home. Why not get the last order increment id from the sales/order collection?
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('increment_id','DESC')
->setPageSize(1)
->setCurPage(1);
echo $orders->getFirstItem()->getIncrementId();
Tested and working on Magento 1.3.2.3

Try this:
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

$dbc_collect_order = Mage::getSingleton('core/resource')->getConnection('core_read');
$items_collect_order = $dbc_collect_order->fetchAll("SELECT `increment_id` FROM `sales_flat_order` ORDER BY `entity_id` DESC LIMIT 1");
echo $last_main_order_id = $items_collect_order['0']['increment_id'];

Please note that there is a more simpler answer to silvo's:
$orders = Mage::getModel('sales/order')->getCollection();
echo $orders->getLastItem()->getIncrementId();

Related

How to insert the value into query?

Do I need to insert the variable in sum(orderdets.quantity) into another variable?
$order = Order::with('customer','product')
->select(
'orders.id',
'orders.customer_id',
'orderdets.product_id',
DB::raw('SUM(orderdets.quantity)'))
->get();
Try This
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
dd($orders);
Use AS tempName to get the column data by specific name. this way it can be accessed.
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) AS sumOrders'))->get();
$orders = Order::with('customer','product')
->select('orders.id', 'orders.customer_id', 'orderdets.product_id'))
->get()->map(function($order){
$order->put('sum', $order->orderdets->sum('quantity');
})->save();
This will sum all your debts and will also update orders table in one go, Although you question wasn't clear but I think you must be looking for something like this
Please try below script in your controller.
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
return $orders;
Thanks
PHPanchal

Magento 1.9.1.1 - Duplicate results displayed

I have a Magento issue. I'm using Magento ver. 1.9.1.1.
The issue is that I am seeing duplicate results for a page which contains a list of jobs (its a recruitment page). The problem is each of the results is duplicated. I've never done any real Magento development before and it seems like a steep learning curve.
I've checked the content and that has only been entered once.
This is the offending code:
//I tried this line with no effect
//$collection = Mage::getModel('cms/page')->getCollection()->distinct(true);
// $collection contains the duplicate results
$collection = Mage::getModel('cms/page')->getCollection();
Can anyone give me any ideas on how I can solve this? Even an idea of where to look in the code would be good.
I've found two data structures when iterating through the collection. These are _origData and _Data. Don't know why its using both of these but I managed to fix/hack it by doing:
if($key == "_origData"){
continue;
}
Surely there's a better way to do this?
Thank you in advance :)
Have you tried applying filters to your collection?
See below:
$getStoreId = Mage::app()->getStore()->getId();
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter($getStoreId)
->addFieldToFilter('is_active', 1);
Resources:
Class Mage_Cms_Model_Mysql4_Page_Collection
You can see the code details on above mentioned Model class.But if you want custom code you can try like this :
$collection = Mage::getModel('cms/page')->getCollection();
$collection->getSelect()
->join(
array('s' => $collection->getTable('cms/page_store')),
's.page_id = main_table.page_id AND s.store_id != 0',
array('store_id')
)
->columns(array('stores_count' => new Zend_Db_Expr('COUNT(s.store_id)')))
->group('main_table.page_id')
->having('stores_count = ?', 1)
->having('s.store_id = ?', $storeId)
;

How to get order increment id using order id?

How can I get order increment id (like 100000028) with order id (like 28). In sales order page order increment id is like 100000028, but I have order id like 28.
How can I get order increment id by order id? I have tried below
$write = Mage::getSingleton('core/resource')->getConnection('core_read');
$result=$write->query("SELECT entity_id FROM `sales_flat_order` WHERE `increment_id` = 'your increment id' ");
$row = $result->fetch();
echo $row['entity_id'];
$order = Mage::getModel('sales/order')->load($orderid);
$Incrementid = $order->getIncrementId();
The other answers require loading the entire order, which is overkill. You can use this built-in method instead:
Mage::getResourceModel('sales/order')->getIncrementId($orderId)
This will run a simple SELECT query to fetch just that one field.
Use the following code in order to get Order increment id -
$order = Mage::getModel('sales/order');
$order->load([Enter Order Id]);
$incrementId = $order->getIncrementId();
I use preg_match, which is easy and simple:
$o_id = preg_match('/10*(.*)/', $row[order_id], $o_id2 );
echo "Your order id is: $o_id2[1]"
As long as your increment id is like 10, it will work.

Magento - get order id from increment id

How do you get the Order Id from magento Order Increment Id?
I can use the following get the product id from SKU:
$product_id = Mage::getModel('catalog/product')->getIdBySku('ABCD1234');
The above example lets me get just the database entity_id of a product without loading everything. I want to achieve the same for order.
In a Magento model, the load method can take an optional second argument of what attribute to load by.
So, in your case, the following should work:
$order = Mage::getModel('sales/order')->load($incrementId, 'increment_id');
$id = $order->getId();
In more complex cases, e.g. where you want to load by a combination of fields, you can load a collection, and get the first element of the collection. In your case, you'd do it like this:
$order = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('increment_id', $increment_id)
->getFirstItem();
You can load an order from the IncrementId.
$orderIncrementId = "1000001";
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
echo $order->getId();
If you want to fetch only order_id then simply use mysql query, if you want order_id in for loop, it is not load entire order object and it is very quick and fast, you don't need to load order object model.
$write = Mage::getSingleton('core/resource')->getConnection('core_read');
$result=$write->query("SELECT entity_id FROM `sales_flat_order` WHERE `increment_id` = 'your increment id' ");
$row = $result->fetch();
echo $row['entity_id'];

Magento - addStoreFilter not working?

When getting a product collection in Magento, I would expect the StoreFilter to do just that, filter by the current store. But I can't get it to work.
Say I have 2 stores set up like so:
And both stores have a different root category. Main Store is the default sample data, Store2 has just one product I added. I would have thought that using the store filter, only products within the root category of the current store would show up. But I'm getting every product showing. I'm testing this by placing the following in my category view template:
$store_id = Mage::app()->getStore()->getId();
$_testproductCollection = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($store_id)
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName());
};
If I echo the store ID, it's giving me the correct number. I have only one product in Store 2, so why am I getting every product from all stores returned? I can set every product in Main Store to not show in Store2 and then add a visibility filter, but that would take forever.
Also, I just noticed, if I echo the products store ID, I get the current ID, not the store it's assigned to:
echo $_testproduct->getStoreId()
What's going on?
UPDATE (April 8 2011):
OK, so I tried joining the fields so that the store_id is included (as suggested below), the section of code {{table}}.store_id = 1 is just setting all the products to have a store_id of 1. How can I just get the store_id associated with the product?
$_testproductCollection = Mage::getResourceModel('catalog/product_collection');
$_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
$_testproductCollection->getSelect()->distinct(true);
$_testproductCollection->addAttributeToSelect('*')->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
echo "STORE IS ".$_testproduct->getData('store_id')."<br/>";
};
If I check the catalog_category_product_index table of my db, the store_id's are correct.
$_testproductCollection should look like this $_testproductCollection = Mage::getResourceModel('reports/product_collection')->addAttributeToSelect('*')->addStoreFilter().
If You print SELECT from that collection You will see that there ain't any store column, so addStoreFilter() can't apply WHERE.
You should use joinField() on Your collection and add store_id column from catalog_product_entity_varchar table.
EDIT
Sorry to keep You waiting ;)
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
$collection->getSelect()->distinct(true);
This should do the trick, but just to be sure, please check if you're getting right products :)
This worked for me:
Mage::app()->setCurrentStore($storeId);
$productCollection = Mage::getModel('catalog/product')
->getCollection()
->addStoreFilter()
->addAttributeToSelect(array('sku','price'));
OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:
$_rootcatID = Mage::app()->getStore()->getRootCategoryId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
};
I think
You don't need to do any joins as the magento's setStoreId() will work.
$collection = Mage::getModel("catalog/product")
->getCollection()
->setStoreId(1) //change store Id according your needs
->addAttributeToSelect(array('name','url','sku'))
->setPageSize(20);
This will get maximum 20 products from store id 1

Resources