How to get id of last purchase with multiple addresses? - magento

I have a payment method that needs to be redirected to the page it like paypal, I can add a code block on the page of success but need to know if petido that was just completed was paid with my payment method, so I can redirect.
Note: several people say it is not possible to redirect payments with multishipping.
I just want to get the id of the application of the current purchase being multishipping.
I got this way, but it's not exactly what I want:
$fromDate = date('Y-m-d') . ' 0:0:0';
$toDate = date('Y-m-d H:i:s');
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('customer_id', array('eq' => array(Mage::getSingleton('customer/session')->getCustomer()->getId())));
$orderCollection = $orderCollection->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
foreach($orderCollection as $order_row){

Solution:
$last_quote_id = Mage::getSingleton('checkout/session')->getLastQuoteId();
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('customer_id', array('eq' => array(Mage::getSingleton('customer/session')->getCustomer()->getId())));
$orderCollection = $orderCollection->addFilter('quote_id', $last_quote_id);
foreach($orderCollection as $order_row){
if($order_row->getPayment()->getMethodInstance()->getTitle() == 'PagSeguro'){ $pagseguro = 1; }
echo $order_row->getId() . ' - ' . $order_row->getPayment()->getMethodInstance()->getTitle() . '<br />';
}

Related

get data from sales order and sales order item in magento

I am working on a magento php code to get the Customer Name, email, product description, sku code, order number and customer id. However I have been feeling a bit stuck when trying to join the "sales/order" and "sales/order_item" models in order to print out the data through a foreach loop and also get only the now date. This is what I got so far:
<?php
set_time_limit(0);
require_once '../app/Mage.php';
Mage::app('uk');
$collection = Mage::getModel('sales/order')
->getCollection()
->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE)
->addAttributeToSelect('*')
->addAttributeToSort('created_at','DESC');
foreach ($collection as $c) {
echo $c->getCustomerName() . "\t" .
$c->getCustomerEmail() . "\t" .
$c->getCreatedAt() . "\r\n";
}
?>
Could you please give me a hand with this?
Thanks a lot,
Nestor
try below code
foreach ($collection as $c) {
$order = Mage::getModel("sales/order")->load($c->getId());
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getItemId();
echo $item->getSku(); -
}
echo $c->getCustomerName() . "\t" .
$c->getCustomerEmail() . "\t" .
$c->getCreatedAt() . "\r\n";
}

Auto redirect to another page In Joomla

I am making payment through paypal. After payment completion it redirect to my website(complete.php) page. I have code in complete.php that will update the credit in database. when i refresh the same page then it automatically update credits in database. I want to update credit only when payment is done. The code i am using in complete.php is:
$db = JFactory::getDBO();
$result = null;
$user = JFactory::getUser();
if ($user->guest) {
return false;
}
$query = 'SELECT credit' .
' FROM #__vodes_credits' .
' WHERE userid = ' . (int) $user->id
;
$db->setQuery($query);
$result = $db->loadResult();
$result_final=$result+20;
$query = 'update #__vodes_credits SET credit='.$result_final.
' WHERE userid = ' . (int) $user->id
;
//echo $query;
//echo $query;
$db->setQuery($query);
$result = $db->loadResult();
if ($db->getErrorNum()) {
JError::raiseWarning( 500, $db->stderr());
}
return $result;
?>
Please help me to sought it out.
I recommend you to create a COOKIE before the payment and then destroy that COOKIE after the first time the user has visited the complete.php, that will work.
Regards.
In case you want to redirect, I am not sure where are you writing your code but try the below code
$app=JFactory::getApplication();
$app->setRedirect('url','msg');

How Do I Output Delivery Instructions for Magento?

I am trying to create a script that I can access via URL so that it outputs delivery details, specifically the fields I need to display are: orderID, status, gift_message and a custom customer address attribute that is called "delivery_instructions". So far I have the following, but I am stuck with trying to get the delivery instructions, any help would be much appreciated.
<?php
require_once 'app/Mage.php';
Mage::app('default');
$myOrder=Mage::getModel('sales/order');
$orders=Mage::getModel('sales/mysql4_order_collection');
$message = Mage::getModel('giftmessage/message');
$customer = Mage::getModel('customer/customer');
$orders->addFieldToFilter('total_paid',Array('gt'=>0)); //Amount paid larger than 0
//$orders->addFieldToFilter('gift_message_id',Array('gt'=>0));
$allIds=$orders->getAllIds();
foreach($allIds as $thisId) {
$myOrder->reset()->load($thisId);
$shippingAddress = $myOrder->getShippingAddress();
$customerDetails = $myOrder->getCustomer();
//echo $myOrder->shippingaddress->getData('delivery_instructions');
$customer_id = $myOrder->getCustomerId() . "',";
echo $customer_id . "',";
echo "'" . $myOrder->getStatus() . "',";
echo "'" . $myOrder->getIncrementID() . "',";
$gift_message_id = $myOrder->getGiftMessageId();
if(!is_null($gift_message_id)) {
$message->load($gift_message_id);
echo $gift_message = $message->getData('message') . "',";
};
$customer = Mage::getModel('customer/customer')->load( $customer_id);
echo $customer = $customer->getData('incrementId');
echo "\r\n";
echo "<br/ >";
}
?>
You are close with this:
//echo $myOrder->shippingaddress->getData('delivery_instructions');
In order to get the shipping address object you need to use:
$myOrder->getShippingAddress()
and to get the delivery_instruction from the shipping address object:
$myOrder->getShippingAddress()->getDeliveryInstructions()
You can create custom address attributes and custom customer attributes.
Customer attributes can be accessed like this:
// A customer is not mandatory for an order.
if($myOrder->getCustomerId()){
$customer = Mage::getModel(’customer/customer’)->load($myOrder->getCustomerId());
$customer->getDeliveryInstructions();
}
Address attributes can be accessed like this:
$myOrder->getShippingAddress()->getDeliveryInstructions();

How to get views / clicks of a specific product

As the title suggests, how do I get the number of views / clicks / impressions of a specific product in Magento. Any help is welcome.
This simple example will give you a list of products that have been viewed between the dates you specify + their view counts:
$fromDate = '2010-01-01';
$toDate = now();
$viewedProducts = Mage::getResourceModel('reports/product_collection')
->addViewsCount($fromDate, $toDate);
foreach($viewedProducts as $product) {
echo "ID: " . $product->getData('entity_id') . " - View Count: " . $product->getData('views') . "<br/>";
}
It helped me, get views just for one product.
$resource = Mage::getResourceModel('reports/event');
$select = $resource->getReadConnection()->select()
->from(array('ev' => $resource->getMainTable()), array(
'product_id' => 'object_id',
'view_count' => new Zend_Db_Expr('COUNT(*)')
))
->join(
array('et' => $resource->getTable('reports/event_type')),
"ev.event_type_id=et.event_type_id AND et.event_name='catalog_product_view'",'')
->group('ev.object_id')
->where('ev.object_id IN(?)', [$entity_id])
->where('ev.logged_at >= ?', $from)
->where('ev.logged_at <= ?', $to);
$views = $resource->getReadConnection()->fetchPairs($select);
$views = !empty($views[$entity_id]) ? $views[$entity_id] : 0;

Magento get products next by price

I need to get a collection of products next by price for specific product in some category.
For example: I have product White Shoes, it is located in Shoes category. I need to get next five products, that has higher price than White Shoes in Shoes category, and five products that has lower price.
Thanks for your help!
This can probably be cleaned up from an efficiency standpoint, but hopefully you get the gist of it. There are two variables you'll need to set, $product which is probably your White Shoes product as a Mage_Catalog_Model_Product object and $category which is your Shoes category as a Mage_Catalog_Model_Category object.
UPDATE
This is a better way to do it without loading the entire product collection for $category.
// Load up to 5 products with price <= $product
$fiveLower = Mage::getModel('catalog/product')->getCollection()
// Add whatever attributes you want here
->addAttributeToSelect(array(
'name',
'product_url',
'small_image',
))
// $category is an instance of Mage_Catalog_Model_Category
->addCategoryFilter($category)
// Both of these are required to get the price
->addMinimalPrice()
->addFinalPrice()
// Filter out the current product by id
->addAttributeToFilter('entity_id', array('neq' => $product->getId()));
// Filter by final_price <= $product and limit to 5.
$fiveLower->getSelect()
->having('final_price <= ?', $product->getFinalPrice())
->order('final_price')
->limit(5, 0);
// Load up to 5 products with price > $product
$fiveHigher = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array(
'name',
'product_url',
'small_image',
))
->addCategoryFilter($category)
->addMinimalPrice()
->addFinalPrice();
$fiveHigher->getSelect()
->having('final_price > ?', $product->getFinalPrice())
->order('final_price')
->limit(5, 0);
echo 'These are the lower priced items:' . PHP_EOL;
foreach ($fiveLower as $item) {
echo $item->getName() . ' - ' . $item->getFinalPrice() . PHP_EOL;
}
echo 'These are the higher priced items:' . PHP_EOL;
foreach ($fiveHigher as $item) {
echo $item->getName() . ' - ' . $item->getFinalPrice() . PHP_EOL;
}
I had similar task, and here what I've done:
$customerGroupId = Mage::helper('customer')->getCustomer()->getGroupId();
$websiteId = Mage::app()->getStore()->getWebsiteId();
$finalPrice = $product->getFinalPrice();
$coreResource = Mage::getSingleton('core/resource');
$adapter = $coreResource->getConnection('catalog_read');
$prevSelect = $adapter->select()
->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
->join(
array('p' => $coreResource->getTableName('catalog/category_product')),
'p.product_id = i.entity_id',
array('product_id')
)
->where('p.category_id = ?', $categoryId)
->where('i.customer_group_id = ?', $customerGroupId)
->where('i.website_id = ?', $websiteId)
->where('p.product_id != ?', $product->getId())
->where('i.final_price < ?', $finalPrice)
->order('i.final_price DESC')
->limit(self::PRODUCTS_BLOCK_SIZE);
$lowerIds = array_reverse($adapter->fetchCol($prevSelect));
$nextSelect = $adapter->select()
->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
->join(
array('p' => $coreResource->getTableName('catalog/category_product')),
'p.product_id = i.entity_id',
array('product_id')
)
->where('p.category_id = ?', $categoryId)
->where('i.customer_group_id = ?', $customerGroupId)
->where('i.website_id = ?', $websiteId)
->where('p.product_id != ?', $product->getId())
->where('i.final_price > ?', $finalPrice)
->order('i.final_price ASC')
->limit(self::PRODUCTS_BLOCK_SIZE);
$higherIds = $adapter->fetchCol($nextSelect);
$lowerSliced = array_slice($lowerIds, -self::PRODUCTS_BLOCK_SIZE);
$requiredFromHigher = self::PRODUCTS_BLOCK_SIZE - count($lowerSliced);
$similarIds = array_merge(
$lowerSliced,
array_slice($higherIds, 0, $requiredFromHigher)
);
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('name')
->addAttributeToSelect('small_image')
->addAttributeToSelect('product_url')
->addAttributeToFilter('entity_id', array('in' => $similarIds))
->setPage(1, self::PRODUCTS_BLOCK_SIZE);

Resources