Get orders and use them in cycle magento - magento

i want to get all the orders from all the customers on admin side to export them in csv, but i am having problems getting them, in the foreach below dont know why it does not convert $collection to $col and give me the instance, can you help me please?
require_once('app/Mage.php');
Mage::app('admin');
Mage::getSingleton("core/session", array("name" => "adminhtml"));
Mage::register('isSecureArea',true);
$collection = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*');
$min_diff = '60';
$from_date = date("Y-m-d H:i:s", strtotime("-".$min_diff." minute"));
$to_date = date("Y-m-d H:i:s");
$collection->addAttributeToFilter('updated_at', array(
'from' => $from_date,
'to' => $to_date
));
Mage::log("80",null,"ordenes.log");
foreach ($collection as $col) { //falls here Mage::log($col,null,"ordenes.log");
echo '<tr>';
echo "<td>".$col->getIncrementId()."</td>";
echo "<td>".$col->getCreatedAt()."</td>";
echo "<td>".$col->getUpdatedAt()."</td>";
echo "<td>".$col->getState()."</td>";
echo "<td>".$col->getStatus()."</td>";
echo "<td>".$col->getHpcOrderId()."</td>";
echo "<td>".$col->getHpcOrderFrom()."</td>";
echo '</tr>';
}
echo '</table>';
echo "<br />supplement order ends";

The Problem is in your addAttributeToFilter() filter. The collection is getting empty data.
If you remove date filter then your code is working. Or try to increase the From & To date difference.
require_once('app/Mage.php');
Mage::app('admin');
Mage::getSingleton("core/session", array("name" => "adminhtml"));
Mage::register('isSecureArea',true);
$collection = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*');
$min_diff = '60';
$from_date = date("Y-m-d H:i:s", strtotime("-".$min_diff." minute"));
$to_date = date("Y-m-d H:i:s");
/*$collection->addAttributeToFilter('updated_at', array(
'from' => $from_date,
'to' => $to_date
));*/
Mage::log("80",null,"ordenes.log");
foreach ($collection as $col) { //falls here Mage::log($col,null,"ordenes.log");
echo '<tr>';
echo "<td>".$col->getIncrementId()."</td>";
echo "<td>".$col->getCreatedAt()."</td>";
echo "<td>".$col->getUpdatedAt()."</td>";
echo "<td>".$col->getState()."</td>";
echo "<td>".$col->getStatus()."</td>";
echo "<td>".$col->getHpcOrderId()."</td>";
echo "<td>".$col->getHpcOrderFrom()."</td>";
echo '</tr>';
}
echo '</table>';
echo "<br />supplement order ends";

Related

Get order tracking numbers with shipping services from magento 1.9

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);

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";
?>

Twitter OAUTH and Codeigniter

I'm trying to display tweets on a website built on codeigniter but can't seem to pull in the user tweets. To do this, I'm pulling the following script into my header as an include and then printing the tweet in the appropriate section with the code below. I've already set up the access tokens and consumer keys as well. Any ideas on why this is working?
Include file in header
<?php
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "ACCESS TOKEN HERE";
$oauth_access_token_secret = "ACCESS TOKEN SECRET HERE";
$consumer_key = "CONSUMER KEY HERE";
$consumer_secret = "CONSUMER KEY SECRET HERE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json, false);
$latest_tweet = $twitter_data[0];
?>
Print tweet
<span class="tweet">"<?php if(!empty($latest_tweet)){echo $latest_tweet->text;} else{echo "Welcome to Time Equities!";} ?>"</span>

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. :)

Uploading an image with Code Igniter?

I'm trying to set a site up so I can upload images using a web form in Code Igniter(CI). I'm not getting any errors but the file is not being saved either. I wanted to know if those that were successful in uploading images could help explain what might be the issue?
View:
<?php
echo form_open_multipart('admin/galleryUpload') . "\n";
echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
foreach ($gallery as $picture)
{
$order[] = $picture->order;
}
$order[] = count($order) + 1;
echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'> </div>";
$conf = array('name' => 'alt_text', 'size' => '75');
echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
echo form_hidden('propertyID', "$propertyID");
echo form_submit('upload', 'Upload');
echo form_close();
?>
Controller:
class Admin extends Controller
{
function galleryUpload()
{
if (! $this->session->userdata('is_admin'))
{
redirect('admin/index');
}
else
{
$this->load->model('admin_model');
$this->admin_model->imgUpload();
}
}
}
Model:
class Admin_model extends Model
{
function imgUpload()
{
$id = $this->input->post('propertyID');
$order = $this->input->post('order');
$alt_text = $this->input->post('alt_text');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
//'upload_path' => '../' . $this->imgPath($id),
'upload_path' => '../img/galleries/temp/',
'max_size' => '5000', // 5MB files max
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->imgPath($id) . '/thumbs',
'maintain_ratio' => TRUE,
'width' => '60'
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
From the user guide:
By default the upload routine expects the file to come from a form
field called userfile, and the form must be a multipart type
So you either have to change the name of the form field to userfile:
form_upload('userfile')
or pass the name of your form field to do_upload:
$this->upload->do_upload('uploadImg');

Resources