I had added a custom option Complete in action dropdown(sales->orders). It's working fine and order status changes to complete successfully.
I am integrating all orders with Salesforce. I have need of all of order details by orderid. Item details and grand total is fetched successfully.
Can anyone please help to fetch Customer name and his/her company name how submitted order. Below is my complete code to fetch order details:
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
$_totalData = $order->getData();
$_grand = $_totalData['grand_total'];
$custname = $_totalData->getCustomerName();
$itemcount=count($items);
foreach ($items as $itemId => $item)
{
$sObject2->Item_name__c = $item->getName();
$sObject2->Unit_price__c = $item->getPrice();
$sObject2->Sku__c = $item->getSku();
$sObject2->Quantity__c = $item->getQtyToInvoice();
}
try this
$order->getCustomerName()
You probably don't need to cast $order->getData() to a new variable. This will only serve to chew up memory, especially since there is only one element you need from that data, which can be retrieved with a less intensive method.
Instead, try it this way:
$order = Mage::getModel('sales/order')->load($orderId);
$_grand = $order->getGrandTotal();
$custname = $order->getCustomerName();
foreach ($order->getAllItems() as $itemId => $item)
{
// Do stuff
}
if $order->getCustomerName() doesn't work for you, try:
$order->getBillingAddress()->getName();
$custname = $Order->getCustomer()->getName();
Related
I currently have an array that is building with the correct data by looping an object but it's giving the incorrect format:
$priceResult = array();
foreach($prices->categories as $category){
$priceResult[] = $category->category_name;
$priceResult[] = $category->category_desc;
$priceResult[] = $category->category_code;
foreach($category->products as $product){
$priceResult[] = $product->product_info->item->item_code;
foreach ($product->product_info->details as $details) {
$priceResult[] = $details->description;
$priceResult[] = $details->color;
$priceResult[] = $details->sector;
}
$priceResult[] = $product->product_info->code;
$priceResult[] = $product->product_info->item->description;
$priceResult[] = $product->product_info->item->item_type->quantity;
foreach(get_object_vars($product->prices) as $amount){
$priceResult[] = $amount;
}
}
}
This isn't associative though.
So currently, say I have one category with two products then they all print out as a single array
array({
1:category_name
2:category_desc
3:category_code
4:item_code
5:description
6:color
7:sector
8:code
9:description
10:quantity
11:amount
12:item_code
13:description
14:color
15:sector
16:code
17:description
18:quantity
19:amount
})
I'd like to get a structure where the parent level is the category_code with it's name and description, then each item_code and their own info like so:
array({
category_name
category_desc
category_code
Array(
1: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
2: item_code array(
details array(
description
color
sector
)
code
description
quantity
amount)
)
})
How can I modify this to create the levels like I need so that it formats properly when I export to a spreadsheet
You need to split you code and init new object in the loop.
Consider the following (notice the comment in the code)
$allCategoryResult= array(); // init at first - notice naming as category and not prices
foreach($prices->categories as $category){
$categoryItem = array(); // as current category to populate
// give name to the keys and not just numbers
$categoryItem["name"] = $category->category_name;
$categoryItem["desc"] = $category->category_desc;
$categoryItem["code"] = $category->category_code;
foreach($category->products as $product){
$productItem = array(); // new product, so init new array for him
// fill all the item data with name - maybe you will need to fix the paths here
$productItem["details"] = array(); // init empty array for all the details elements
foreach ($product->product_info->details as $details) {
$detailsItem = array(); // init details array for each detail element
$detailsItem["description"] = $details->description;
$detailsItem["color"] = $details->color;
$detailsItem["sector"] = $details->sector;
$productItem["details"][] = $detailsItem; // add the detail element to the product array
}
$productItem["code"] = $product->product_info->code;
$productItem["itemDescription"] = $product->product_info->item->description;
$productItem["quantity"] = $product->product_info->item->item_type->quantity;
$productItem["amount"] = get_object_vars($product->prices);
$itemCode = $product->product_info->item->item_code;
categoryItem[$itemCode] = $productItem; // add the product to category array by his code
}
$allCategoryResult[] = $categoryItem; //add the category to all category array
}
Writing this without see you actual data is pretty hard - so I guess you will have to modify it to fit your data.
But I hop you get the idea. Good luck!
I am playing around with a PHP shopping cart and I am trying to send the order of the customer via email to the seller.
My problem is that if there are multiple items ordered, only the last one is actually sent in the email but when I echo, all the items are displayed.
Could you please explain why this is happening?
if(isset($_SESSION["products"])){
$total = 0;
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm){
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
$rows = array();
while ($obj = $results->fetch_object()){
$rows[] = $obj;
echo $rows[0]->product_name.' x '.$cart_itm["qty"].' ; '; // here is ok
$prod_name = ($rows[0]->product_name); // here only the last product displays. Why?!
}
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
$cart_items ++;
}
}
$prod_name = str_replace($cyr, $lat, $prod_name);
$random = rand(72891, 92729);
$subject = "New order #$random";
$message = "You have new order from $name $lname with adress $curraddr and order details: $prod_name with a total value of $total dollars.";
$from = "From: admin#mail.xyz";
$to = "mail#mail.com";
mail($to, $subject, $message, $from);
Every time you iterate through the while loop, you overwrite $prod_name. The last time through the loop, you overwrite the variable with the last product.
As for the echo "working," the output of the echo is not being overwritten every time you go through the loop. Instead, you are constantly appending to the end of what you printed last time.
To insert all product names into your email message, you should build the message as you go through the loop. Try reading about string concatenation to learn more.
I want to create stock management system in my website using php. It sounds like 'balancing' stock in database with amount of items that will purchased. If the item's amount is more than the item's stock, the website should displaying an alert. So, the amount of item cannot be more than the item's stock in database.
Here's my code :
$stock = array();
foreach ($_SESSION['cart'] as $k => $v) {
$sql = mysql_query("SELECT book_stock FROM book WHERE book_title='$k'");
while ($row = mysql_fetch_assoc($sql)) {
$stock[] = $row['book_stock'];
}
if ($stock < $v) {
echo "Stock less than the amount";
}
else {
echo "Stock sufficient";
}
}
The $k variable store the item's name in the session cart. And the $v variable store the item's amount in the transaction.
The item's amount will be increased if the visitor click 'Increase' button.
But if i increase the item's amount, the if and else logic like "isn't working". No matter how much the item's amount is added, the if and else logic keep print "Stock Sufficient".
Can anyone help me to solve this? Thanks in advance :)
Edit :
The variable in original code is in indonesian. So when i'm typing this post, i'm editing the variables too here. So if the variables is different, i'm just forget to rename it. So i edit the script again :)
$sql = mysql_query("SELECT book_stock FROM ooku WHERE book_title='$k'");
I am assuming that your query will return unique book (single book) stock detail, you have to use $stok instead of $stok[] Array
while ($row = mysql_fetch_assoc($sql)) {
$stok = $row['book_stock'];
}
I have purchase order form where I am storing product details in session i.e product name, quantity, rate etc. and saving it to database. And at the time of editing purchase order, getting product details from database and storing it in session and appending new product details to session array. It is working fine. But When I submit form then Only last record i.e. newly added record is inserted in database.
here you can assume that I am editing shopping cart after placing order.
My problem is that add,delete items in cart after placing order is logically correct or not? And if yes then How?
This is my code for save and update purchase order.
function save(&$purchase_data,$purchase_id)
{
$success=false;
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
if($purchase_data)
{
if (!$purchase_id or !$this->exists($purchase_id))
{
//$purchase_data['purchase_id'] = $purchase_id = $purchase_data['purchase_id'];
$success = $this->db->insert('purchase_order',$purchase_data);
$post_array['cart']=$this->session->userdata('data');
/*print_r($post_array);
exit;*/
$purchase_id=$this->db->insert_id();
$i=0;
foreach($post_array['cart'] as $item)
{
//echo "<pre>"; print_r($item); echo "</pre>";
$query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
foreach ($query->result() as $row)
{
$product_name=$row->name;
}
$product_id=$item['product_id'];
$quantity=$item['quantity'];
$unit=$item['unit'];
$unit_rate=$item['unit_rate'];
$query = $this->db->query("insert into phppos_productdetails(product_id,product_name,quantity,unit,unit_rate,purchase_id) values ('$product_id','$product_name','$quantity','$unit','$unit_rate','$purchase_id')");
$i++;
}
}
else
{
$this->db->where('purchase_id', $purchase_id);
$success = $this->db->update('purchase_order',$purchase_data);
//$this->session->set_userdata('sess_products');
$post_array['cart']=$this->session->userdata('sess_products');
$i=0;
foreach($post_array['cart'] as $item)
{
echo "<pre>"; print_r($item); echo "</pre>";
$query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
foreach ($query->result() as $row)
{
$product_name=$row->name;
}
$product_id=$item['product_id'];
$quantity=$item['quantity'];
$unit=$item['unit'];
$unit_rate=$item['unit_rate'];
$query = $this->db->query("update phppos_productdetails set product_id='$product_id',product_name='$product_name',quantity='$quantity',unit='$unit',unit_rate='$unit_rate' where purchase_id='$purchase_id'");
$i++;
}
}
}
$this->db->trans_complete();
return $success;
}
you have put (where purchase_id='$purchase_id'")
so when the loop executes last time it updates all records with purchase_id as $purchase_id
Your query:
$query = $this->db->query("update phppos_productdetails set product_name='$product_name',product_id='$product_id',quantity='$quantity',unit='$unit',unit_rate='$unit_rate' where purchase_id='$purchase_id'");"
By seeing above query the parameters used in this query like $product_name is out of inner foreach scope.Hence it takes only last record and insert it into database.
If you place this query in inner foreach loop then it will inserts appropriate records as per you required.
Hope this will helps you.
On a product page I want to show 4 other products selected randomly, but never the product that is already being displayed. The product id of the displayed one is $_product->getId() and all the products go into a $result[] array like this:
foreach($collection as $product){
$result[]=$product->getId();
}
I'm using $need = array_rand($result, 4); to get the ids of the 4 random products, but it might include the id of the product on display. How do I exclude $_product->getId() from the $need[] array? Thank you.
Don't put id of the product you don't want to show into $result:
$currentProductId = $_product->getId();
foreach ($collection as $product) {
if ($product->getId() != $currentProductId) $result[] = $product->getId();
}
Is it acceptable to just not put the current product ID in the array?
foreach($collection as $product) {
if( $product != $_product) $result[] = $product->getId();
}
You might generate your random numbers first, like so:
$rands = array();
while ($monkey == false){
$banana = rand(0,4);
if (in_array($banana, $rands) && $banana != $_product->getId()){ $rands[] = $banana; }
if (sizeOf($rands) == 4){
$monkey = true;
}
}
Then you could pipe them through your product grabber. Obviously, you'd need to figure out the bounds for rand yourself but you know more about your app than I do. Picking your numbers first is much cheaper computationally than pulling records and THEN checking to make sure that they're unique.
Of course, if this is database-backed, you could solve it much more elegantly by writing a new query.
If you use the product id as the index $result[] in result, you can remove the current product from the $result array with unset() before making the call to array_rand() like so:
foreach($collection as $product){
$result[$product->getId()] = $product->getId();
}
unset($result[$_product->getId()]);
$need = array_rand($result, 4);
This approach saves you from having to use the values in $need to look up the product id in your $result[] array, since the values in $need will be your product ids.