Get order tracking numbers with shipping services from magento 1.9 - magento

How to get track no with orders and shipping services in magento. I need this info in csv file so that I can import this in another system.

Create a file and put this code in file and upload this file to magento root and call this from browser, that will give you a csv.
<?php
error_reporting(E_ALL);
ini_set("memory_limit", "100000M");
ini_set("max_execution_time", 0);
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");
Mage::init();
// Set an Admin Session
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(1);
$session = Mage::getSingleton('admin/session');
$session->setUser($userModel);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fromDate = date('Y-m-d H:i:s', strtotime('2018-02-26'));
$toDate = date('Y-m-d H:i:s', strtotime('2019-04-16'));
/* Get orders collection of pending orders, run a query */
$collection = Mage::getModel('sales/order')
->getCollection()
// ->addFieldToFilter('state',Array('eq'=>Mage_Sales_Model_Order::STATE_NEW))
->addAttributeToSelect('*')
->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate));
// ->setPageSize(2)
// ->setCurPage(1);
//$collection->getSelect()->limit(100);
$data[0] = array(
'Action',
'Order ID',
'Tracking Number',
'Shipping Service',
'Shipping Method',
'Date Shipped',
'SKU',
'Quantity Shipped',
);
foreach ($collection as $order) {
if ($billingAddress = $order->getBillingAddress()) {
$billingStreet = $billingAddress->getStreet();
}
if ($shippingAddress = $order->getShippingAddress()) {
$shippingStreet = $shippingAddress->getStreet();
}
/* tracking start */
$trackNoString='';
$trackNos=array();
foreach ($order->getTracksCollection() as $_track){
$trackNos[]=$_track->getTrackNumber();
}
$trackNoString=implode(",",$trackNos);
/* tracking ends */
$orderData = array(
$order->getStatus(),
$order->getIncrementId(),
$trackNoString,
$order->getShippingDescription(),
$order->getShippingMethod(),
$order->getTrackingDate()
);
foreach ($order->getAllVisibleItems() as $itemId => $item) {
$item_name = str_replace('&', " ", $item->getName());
$itemData = array(
$item->getSku(),
$item->getQtyOrdered(),
);
$data[] = array_merge($orderData, $itemData);
}
};
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="Orders-status-21-5-2019.csv"');
$fp = fopen('php://output', 'wb');
foreach ($data as $line) {
fputcsv($fp, $line, ',');
}
fclose($fp);

Related

Getting error while Add to Cart programmatically with custom options

I am trying to Add Products to Cart with respect to customer (programmatically) but getting error "Invalid request for adding product to quote". I have both Simple products (with custom options) and configurable products. Below is my code. Please help. Many thanks in advance.
public function addtocartAction(){
try {
$cusId = $this->getRequest()->getParam('cusId');
$customer = Mage::getModel('customer/customer')->load($cusId);
$quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
$quoteId = $quote->getId();
//$products = $this->getRequest()->getParam('products');
$products = json_decode('[{"proId": "906","proQty": "1", "options":{"17":"wq","16":"18"}}]');
foreach($products as $product) {
/*if (!$product->getId()) {
throw new Exception();
}*/
foreach ($product->options as $optKey => $optValue) {
$optAll[$optKey] = $optValue;
}
$mainProduct = Mage::getModel('catalog/product')->load($product->proId);
$params = array(
'product' => $product->proId,
'qty' => $product->proQty,
'options' => $optAll
);
echo "<pre />"; print_r($params);
$quote->addProduct($mainProduct, $params);
$quote->setIsActive(1);
$quote->collectTotals()->save();
}
$rslt['success'] = '1';
$rslt['message'] = 'Product has been succefully added to cart';
}
catch(Exception $e){
$rslt['success'] = '0';
$rslt['message'] = $e->getMessage();
}
print_r(json_encode($rslt));
}
Try using cart instead of quote.
This works for me:
$cart = Mage::getModel('checkout/cart');
$mainProduct = Mage::getModel('catalog/product')->load($product->proId);
$params = array(
'product' => $product->proId,
'qty' => $product->proQty,
'options' => $optAll
);
$cart->init();
$cart->addProduct($mainProduct, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Magento sort regions by default_name

I need to sort region drop down in one page checkout page. I found data coming from 'Mage_Directory_Helper_Data' I need to sort this data. I tried by adding below code:
$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($countryIds)
->addOrder('default_name', 'DESC')
->load();
But it did not work. Can anyone please help me. Thank You.
Try
if (empty($json)) {
$countryIds = array();
foreach ($this->getCountryCollection() as $country) {
$countryIds[] = $country->getCountryId();
}
$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($countryIds)
->setOrder('default_name','DESC')
->load();
$regions = array(
'config' => array(
'show_all_regions' => $this->getShowNonRequiredState(),
'regions_required' => $this->getCountriesWithStatesRequired()
)
);
foreach ($collection as $region) {
if (!$region->getRegionId()) {
continue;
}
$regions[$region->getCountryId()][$region->getRegionId()] = array(
'code' => $region->getCode(),
'name' => $this->__($region->getName())
);
}
krsort($regions); // or ksort($regions)
$json = Mage::helper('core')->jsonEncode($regions);
}

How to set the base image as small image for the product progmatically?

I am using following code to set the base image as the small image of the product.
Before running the code I have following information
---
----
[image] => /s/a/santat.jpg
[small_image] =>
[thumbnail] => /s/a/santa-300.jpg
---
---
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addAttributeToFilter( 'sku', array( 'in' => array( '01Santa') ) );
foreach ($products as $product)
{
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage())
{
try{
$product->setSmallImage($product->getImage());
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$product->save();
}
?>
Once the code has run I have following if I print
print_r($product);
[image] => /s/a/santa.jpg
[small_image] => /s/a/santa.jpg
[thumbnail] => /s/a/santa-300.jpg
But still in admin no image is set as small image why?(I am using multi website setup)
You need to change code
$product = Mage::getModel('catalog/product')->load($id);
$mediaGallery = $product->getMediaGallery();
if (isset($mediaGallery['images'])){
foreach ($mediaGallery['images'] as $image){
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('small_image'=>$image['file']), 0);
break;
}
}
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter( 'sku', array( 'in' => array("004XmasVacGrnShtrs") ) );
foreach($products as $product)
{
$productId = $product->getId();
echo "<br>";
//load the product
$product = Mage::getModel('catalog/product')->setStoreId("14")->load($productId);
//get all images
$image = $product->getImage(); //Get product base image
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('small_image'=>$image), 14); //14 is store id
}
echo "done";
?>

Magento: Product thumb isn't loading, loading placeholder instead

I am trying to get all product thumbs for a category product collection like that
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId(1)
->setPageSize(10)
->addAttributeToFilter(
'status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
)
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addCategoryFilter($category);
$productThumbs = array();
try {
foreach ($products as $product) {
$thumbUrl = $product->getThumbnailUrl(200, 60);
$productThumbs[] = array(
'entity_id' => $product->getId(),
'thumb_url' => $thumbUrl
);
}
} catch (Exception $e) {
error_log($e->getMessage());
}
It always return placeholder image urls like that...
http://magentohost/magento/media/catalog/product/cache/0/thumbnail/200x60/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/thumbnail.jpg
Don't know what I am doing wrong... Please guide.
I found that we need to load the $product before getting its thumb, like this
$product= Mage::getModel('catalog/product')->load($prod_id);
and then we can retrieve its thumb using
$thumbUrl = $product->getThumbnailUrl(200, 60);

How to get all level categories names and ids for a particular store from multistore magento

I want to get all level categories for a particular store from a multi store website.How can I get this.
Till now I have followed this
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$path = Mage::getBaseDir();
$fp = fopen($path.'/media/stageAllLevelCategories.csv','w');
if ($ids)
{
foreach ($ids as $id)
{
$cat = Mage::getModel('catalog/category');
$cat->load($id);
if($id != 3):
$name = $cat->getName();
echo " ";
$catId = $cat->getId();
echo "</br>";
fputcsv($fp,array($name,$catId ),',','"');
endif;
}
}
fclose($fp);
?>
$collection_orders = Mage::getModel('mycomp_logistic/myorder')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('customer_id', array('in' => array_values($arrayCustomersId)))
->addAttributeToFilter('payment_done_online', array('eq' => ($filter['is_from_where'] == 'from_web')))
->addAttributeToFilter('pdv_ritiro_id', array('eq' => $filter['pdv_ritiro_code']))
->addAttributeToFilter('pdv_ordinante_id', array('eq' => $filter['pdv_ordinante_code']))
->addAttributeToFilter('rif_ordine_vsp', array('eq' => $filter['num_ord']))
->addAttributeToFilter('data_ordine', array('from' => $daydate))
->addAttributeToFilter('num_bolla', array('eq' => $filter['cod_bolla']))
->addAttributeToFilter('sscc', array('eq' => $filter['sscc']))
->addAttributeToFilter('stato', array('eq' => $filter['st_art']));
$collection_orders->getSelect()->joinLeft( array('myarticle' => '<table_name>'),
'myarticle.rif_ordine_vsp = main_table.rif_ord_app',
array('*'));
Hope this will work for you. :)

Resources