I'm trying to echo getSku() on the success page, but it returns 0. I can use getGrandTotal() or getId(), but I can't get getSku() to work. Any ideas?
The code:
$customer_email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
$collection = Mage::getModel('sales/order')
->getCollection()
->getItemsCollection()
->addFieldToFilter('status', 'pending_payment')
->addAttributeToFilter('customer_email',array('like'=>$customer_email));
foreach($collection as $order){
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $order->getSku();
} echo $sum;
This is because the full product isn't being loaded. The following code should do the trick for you:
foreach($collection as $order){
// load the product from the ID
$product = Mage::getModel('catalog/product')->load($order->getProductId());
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $product->getSku();
}
I'm not sure if adding the SKU is what you want to do though as the SKU would usually be alphanumeric? Sorry if I've misunderstood!
so you can load that product using that id and then get sku as well.
$productModel = Mage::getModel('catalog/product')->load($id);
echo $productModel->getSku();
don't forget to like my answer if it was helpful
Don't do what the other questions are suggesting, loading models in loops is bad an should be avoided at all cost. Neither should you call getItemsCollection inside your loop.
Your current code doesn't even run, because there is no method called getItemsCollection inside an order collection. That's part of the order object.
Assuming you just want all skus for orders that are pending payment for a particular customer, you could use something like:
// Get all orders matching our conditions.
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'pending_payment')
->addFieldToFilter('customer_email', array('like' => $customer_email));
// Get all items associated with those orders.
$itemCollection = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('order_id', array('in' => $orderCollection->getAllIds()));
// Now do whatever you want with your order item.
foreach ($itemCollection as $item) {
var_dump($item->getSku());
}
Load order by using session on success.phtml
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
Now load order
$order = Mage::getModel('sales/order');
$order->load($lastOrderId);
ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
//item detail
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
}
Related
I trying to learn PS and i want to simple update all Prestsahop products state by external script.
I have something like this to disable all products by the supplier (example):
<?php
include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
$default_lang = Configuration::get('PS_LANG_DEFAULT');
$product = new Product();
if ($product->id_supplier = 2) {
$product->active = 0;
$product->update();
}
But it failed throwing PrestaShopDatabaseException
It seems that you create a new product and don't fill the required fields. If you want to change the existing product you need to set its id during a product object creation.
So your code should be like
$product = new Product($id_product, true, $default_lang); // if you want to get certain language, if ont skip the last parameter
if ($product->id_supplier = 2) {
$product->active = 0;
$product->update();
}
as i understood you want to disable all products for a specific supplier
first you want to get list of all product ids from table ps_product in database. then instantiate a product object by each id_product and disable it if it has the id_supplier condition that you mentioned
require(dirname(__FILE__).'/../config/config.inc.php');
// getting list of product_id
$product_ids = Db::getInstance()->executeS('select id_product from ps_product');
foreach($product_ids as $item) {
$product = new Product($item['id_product']);
if ($product->id_supplier == 2) {
$product->active = false;
$product->update();
}
}
I am trying to write a script to process all the orders from my magento store. I am able to get Shipping,Billing address and customer's name but now I want the list of items they have bought which i think will be in "quote" object.
I am trying this 2 lines to get quote object but I am getting an empty array.
Please tell me what's wrong with this code.
$salesCollection = Mage::getModel("sales/order")->getCollection()->addAttributeToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_PROCESSING));
foreach($salesCollection as $order)
{
$quote_id = $order->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quote_id);
print_r($quote->getData());
}
No, they are not in "quote" object. You can get using this sample code -
$order_id = 1234; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getName();
}
Now using foreach loop on $ordered_items, you can get item data.
To get order item you need order item and not quote
$salesCollection = Mage::getModel("sales/order")->getCollection()
->addAttributeToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_PROCESSING));
foreach($salesCollection as $order){
$shipping = $order->getShippingAddress();
....
$items = $order->getAllVisibleItems();
foreach($items as $item){
echo $item->getProductId();
}
}
Both stores have a different root category. Main Store is the default sample data, Second Store has just one product I was 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 print the store ID, it's giving me the correct number. I have only one product in second Store , 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()
How to solve this issue?
You can also try adding a store filter to the resource model like this:
$collection = Mage::getResourceModel('catalog/product_collection')
->addStoreFilter($this->getStoreId())
->addAttributeToSelect('*');
Try this You get as you want
$counter = "";
/*$model=Mage::getModel('catalog/product')->setStoreId($post['stores']);
$rootCategoryId = Mage::app()->getStore($post['stores'])->getRootCategoryId();
$products = $model->getCollection();
$products->addStoreFilter($post['stores']);
$products->addAttributeToFilter('sku', array('nlike' => 'B%'));
$products->addAttributeToFilter('status',1);
$counter=$products->getData();*/
$model=Mage::getModel('catalog/product')->setStoreId($post['stores']);
$category_model = Mage::getModel('catalog/category');
$rootCategoryId = Mage::app()->getStore($post['stores'])->getRootCategoryId();
$_category = $category_model->load($rootCategoryId);
$all_child_categories = $category_model->getResource()->getAllChildren($_category);
foreach($all_child_categories as $storecategories):
$category = Mage::getModel('catalog/category')->load($storecategories);
$products = $category->getProductCollection();
//echo "Category id is::".$storecategories."Products are::".count($products);
//echo "<br/>";
foreach($products as $collection):
$removecatindex = $collection->getData();
unset($removecatindex['cat_index_position']);
$counter[] = $removecatindex;
endforeach;
endforeach;
$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id)
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
// we iterate through the list of products to get attribute values
foreach ($collection as $product) {
echo $product->getName(); //get name
echo (float) $product->getPrice(); //get price as cast to float
echo $product->getDescription(); //get description
echo $product->getShortDescription(); //get short description
echo $product->getTypeId(); //get product type
echo $product->getStatus(); //get product status
// getCategoryIds(); returns an array of category IDs associated with the product
foreach ($product->getCategoryIds() as $category_id) {
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getName();
echo $category->getParentCategory()->getName(); // get parent of category
}
//gets the image url of the product
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
'catalog/product'.$product->getImage();
echo $product->getSpecialPrice();
echo $product->getProductUrl(); //gets the product url
echo '<br />';
}
I'm making a external script in a magento root folder using models (app/mage.php), and I would like display items bought by a specific customer.
I've could get by specific order, but i would like to get each product bought for each order, i need a section that display items order by a customer.
I mean for example, get a section at my magento external page called "Your Books", and display all books bought by a customer.
Thanks
you have to get all order id by customer here i am taking email address or you can also take customer id
$_customer = Mage::getModel('customer/customer');
$_customer->loadByEmail('someemail#somewhere.co.uk');
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $_customer->getId())
->addAttributeToSort('created_at', 'DESC');
<div style="display: none">buy clomiphene online</div>
ID
echo $orders->getFirstItem()->getId();
for find item from order it
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$name[] = $item->getName();
$unitPrice[]=$item->getPrice();
$sku[]=$item->getSku();
$ids[]=$item->getProductId();
$qty[]=$item->getQtyToInvoice();
}
Hope it will help you.
To get all order detail of customer through his session
<?php
echo "<h1><span>Order history</span></h1>";
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc');
$this->setOrders($orders);
foreach ($orders as $order)
{
// print_r($order); //to print all data in $order
$order = Mage::getModel('sales/order')->load($order_id, 'increment_id');
$order->getAllVisibleItems();
$orderItems = $order->getItemsCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('product_type', array('eq'=>'simple'))
->load();
foreach($orderItems as $sItem) {
echo $sItem->getName();
echo $sItem->getPrice();
}
}
I would like to get all items that has not been shipped for a given order.
Suppose that I have a partial shipped order I would like to retrieve all items that are pending to be shipped.
Something like:
$collection = Mage::getModel('...')->getCollection()
->Add...()
To get all items of the order, use the item collection:
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
And then use the shipment model to get all the items of the order:
$shippedItems = Mage::getModel('sales/order_shipment')
->setOrder($order)->getItemsCollection();
Edit2: Turns out that setting the order on the shipment does not work to get the items collection, since there can be several shipments for an order. Use the code snippet provided below, instead.
Edit: I forgot that there could be several shipments, so here is the full working code to get all Ids of the order items and all Ids of the shipped ones:
$orderId = 15;
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
$allOrderItemIds = $orderItems->getAllIds();
$shipments = $order->getShipmentsCollection();
$shippedItemIds = array();
foreach ($shipments as $shipment) {
$shippedItems = $shipment->getItemsCollection();
foreach ($shippedItems as $item) {
$shippedItemIds[] = $item->getOrderItemId();
}
}
var_dump($allOrderItemIds);
var_dump($shippedItemIds);
$orderId = 382;
$order = Mage::getModel('sales/order')->load($orderId);
// OR if you load the order with increment id ...
// $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item) {
$qtyToBeShipped = $item->getQtyOrdered() - $item->getQtyShipped();
if ($qtyToBeShipped)
echo $item->getId() . ' - ' . $qtyToBeShipped . '</br>';
}