How to get order increment id using order id? - magento

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.

Related

how to use order by random in codeigniter

While Searching I want to show the paid customers firstly, and then rest of customers list
but the problem is I want to show by random
for Example. paid customers should not mix with others.
Can anyone tell what will be query?
please help me!
I am using codeigniter
Example:
function randomval()
{
$this->db->order_by('id', 'RANDOM');
$this->db->limit(1);
$query = $this->db->get('tblname');
return $query->result_array();
}
Mysql query for your need if i understood well
SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured` ORDER BY `featured` DESC
now with php
$results = $this->db->query("SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured` ORDER BY `featured` DESC")->result_array();
$paid = $results[0];//featured = 1
// comma seprated ids of the paid people e.g :- 3,7,1,26,92 are available in
$paidusers = $results[0]["id"];
//seprate them by
$paidusers = explode(",",$paidusers);
foreach($paidusers as $paiduser)
{
$row = $this->db->get_where("dbc_posts", array("id"=> $paiduser))->row();
print_r($row );
echo "<br>";
}
// do same for unpaid
$unpaid = $results[1];//featured = 0
$unpaidusers = $results[1]["id"];
//seprate them by
$unpaidusers = explode(",",$unpaidusers);
foreach($unpaidusers as $unpaiduser)
{
$row = $this->db->get_where("dbc_posts", array("id"=> $unpaiduser))->row();
print_r($row );
echo "<br>";
}
Ask me if anything goes wrong
Set featured = 1 for paid and featured = 0 for unpaid customers in database. Then use the query
mysql_query("SELECT * FROM dbc_posts WHERE status = 1 ORDER BY featured DESC, RAND() LIMIT 1");

magento getting product id based on custom option value

I need to retrieve an array of cross sell product ids from a product based on a product id retrieved based on a value in a product custom option.
I have researched how to get it using straight MySQL statements. How do I get it using the standard magento calls?
here is the MySQL process:
Select option_type_id from mage_catalog_product_option_type_title where option_type_id EQ 'desired value';
then:
Select option_id from mage_catalog_product_option_type_value where option_type_id EQ option_type_id; (from above step)
then:
Select product_id from mage_catalog_product_option where option_id EQ option id; (from previous step)
then:
Select linked_product_id from mage_catalog_product_link where product_id EQ product_id (from previous step) AND link_type_id ='5';
(I'm experienced in PHP/MySQL but barely past novice level in Magento)
Can anyone help? Thank you very, very much!
Mark
Try this solution:
$collection = Mage::getModel('catalog/product_link')->getCollection()->addFieldToFilter('option_type_id','desired value’);
$collection->join(array('option' => 'catalog/product_option'), 'main_table.product_id = option.product_id')
->join(array('option_value' => 'catalog/product_option_type_value'), 'option.option_id = option_value.option_id');
var_dump($collection->getFirstItem()->getData());
And this is how it generally works:
$collection = Mage::getModel('catalog/product_option_value')->getCollection()->addFieldToFilter('option_type_id','desired value’);
$collection = Mage::getModel('catalog/product_option')->getCollection()->addFieldToFilter('option_id',$collection->getFirstItem()->getOptionId());
$collection = Mage::getModel('catalog/product_link')->getCollection()->addFieldToFilter('product_id',$collection->getFirstItem()->getProductId());
var_dump($collection->getFirstItem()->getData());

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'];

How to get last running transaction Id in 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();

attribute select magento query

i am trying to get this query in magento.
Select merk, option_id from catalog ....... group by option_id
Sofar i have this but it aint showing the option_id value unfortunately and also i dont know how to group by them...
i hope someone is willing to help me
[code]
<?php
function getMenuWatches(){
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("attribute_set_id", 26);
$collection->addAttributeToSelect("option_id , merk");
return $collection;
}
$collection=getMenuWatches();
//print_r($collection);
foreach ($collection as $product){
echo $product->getOptionId();
$product->getMerk();
echo $product->getId('merk');
echo $product->getAttributeText('merk')."<br/>";
}
?>
[/code]
$collection->addAttributeToSelect(array('option_id', 'merk'));
$collection->groupByAttribute('option_id');
Attributes with multiple choices are stored as comma separated values.
So you just need add "merk" attribute in select object:
$collection->addAttributeToSelect('merk');
and when you are iterating the collection, you may retrieve options id by calling your attribute value:
// List of option_id values
$values = explode(',', $product->getMerk());
After retrieving of the values you need to retrieve option label for each option id
$attribute = $product->getResource()->getAttribute('merk');
$optionLabel = $attribute->getSource()->getOptionText($optionId);
To filter by one of the multiple values you may use:
// Creates FIND_IN_SET statement for comma-separated attribute values
$collection->addAttributeToFilter('merk', array('finset' => $optionId));

Resources