Magento - Set Original Price in sales order - php

I have create sales order programmatically.Sales order is generated successfully with all prices, but the Original price column is getting blank. I tried to set in Quote object as well as Order object but no luck.
Can you tell me how can I set original price in sales order.
Thanks!
CODE--
$productModel = Mage::getModel('catalog/product');
$productObj = $productModel->setStore($storeId)->setStoreId($storeId)->load($key);
$productObj->setSkipCheckRequiredOption(true);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$product_price = $productModel->getPrice();
$quoteItem->setOriginalCustomPrice($product_price);
$quoteItem->setOriginalPrice('2.2'); //NOT SET IN DB
$quoteItem->setCustomPrice($product_price);
$quoteItem->setQty($qty);
$quoteItem->setQuote($quoteObj);
$quoteObj->addItem($quoteItem);
$productObj->unsSkipCheckRequiredOption();
$quoteItem->checkData();
$quoteObj->getShippingAddress()->setCollectShippingRates(true);
$quoteObj->getShippingAddress()->collectShippingRates();
$quoteObj->collectTotals();
$quoteObj->setIsActive(0);
$quoteObj->save();

Related

How to add custom product sorting option based on wishlist in Woocommerce?

Customers in our woocommerce store have their own wishlist. I can get product's ids of logged in customer with following code:
$user_id = get_current_user_id();
global $wpdb;
$wishlist_id = $wpdb->get_var("SELECT id FROM wp_wl_lists WHERE author = $user_id");
$products = $wpdb->get_col("SELECT product_id FROM wp_wl_items WHERE wishlist_id = $wishlist_id");
The thing I want to do is to make my own sorting option with products from customer wishlist first...
Have any ideas how to make it?
Thanks

Magento Add product to cart by Session ID/Visitor ID

I am running a script outside my Magento, here i will have Session ID/Visitor ID and also i have Product ID, Qty.
By Session ID/Visitor ID i can get the Customer ID if the customer is loggedin & also able to add product to cart.
$product_id = 123; // Product ID
$qty = 1; // Qty
$storeId = 1; // Store ID
$customer_id = 1; // Customer ID
$product = Mage::getModel('catalog/product')->load($product_id);
$quote = Mage::getModel('sales/quote')->setStoreId($storeId)->loadByCustomer($customer_id);
$quote->addProduct($product, $qty);
$quote->setIsActive(1);
$quote->collectTotals()->save();
Now my question is
If the customer is Guest customer then how can i add the product to his/her cart ?
Any idea ?

Need help on some logic to perform mysql insert query?

I'm developing a shopping cart using php, now I stuck at some point as follow.
There are tables like: customers, products, orders, order_detail
products table:
id | name | quantity
order_detail table:
order_id | product_id | quantity
I completely done for add product to cart and customer can submit the order to database, admin can remove or delete orders from backend and customers can remove orders list or delete orders from frontend if that ordered still not confirm payment.
Example:
"customer1" order 10 products, order ID is #907987899 and "product1" customer1 order quantity = 2, "product2" customer1 order quantity = 5 and so on.....
if the ordered still not confirm then customer still can remove product from the ordered list, and also can delete the entire ordered.
My problem now is: if Customers or Admin cancel or delete the ordered that still not confirm payment then I want to return product quantity from order_detail table list back to products table. by the time when customer submitted the order then product quantity is subtract from product table.
Here is my code that return quantity back to products:
require '../../init.php';
require $ROOT . '/functions/basic.php';
if(isset($_POST['id']))
{
$id = htmlspecialchars($_POST['id']);
$delete = dbQuery("DELETE FROM orders WHERE order_id = '$id'");
if($delete == true)
{
//unlink($ROOT . '/upload/products/' . $image);
$select_product = dbAll("SELECT product_id, quantity FROM order_detail WHERE order_id = '$id'");
foreach($select_product as $product)
{
$quantity = $product['quantity'];
$product_id = $product['product_id'];
$return_product_quantity = dbQuery("UPDATE products SET quantity = (quantity + $quantity) WHERE product_id = $product_id");
}
if($return_product_quantity == true)
{
$delete_order_detail = dbQuery("DELETE FROM order_detail WHERE order_id = '$id'");
if($delete_order_detail == true)
{
echo "OK";
}
else
{
echo "Can't delete order detail information";
}
}
}
else
{
echo "Can't delete order row";
}
}
but the query it seem not work as my required, so please help me how to handle this kind of logic to perform my required.
Thanks in advance.
You are trying to use the variable quantity in your UPDATE even though it has no value. Try this instead:
$return_product_quantity = dbQuery("UPDATE products
SET quantity = ((SELECT quantity FROM products WHERE product_id = $product_id) + $quantity)
WHERE product_id = $product_id");

How to create order Manually in woocommerce?

i had develop a phonegap application with my woocommerce store.
now i want to create order manually.
So anyone can tell me when new order is created in woocommerce store how many data inserted in which tables in database. i mean data at the time of new order generation in which table and in which fashion are they stored. Please help
A order in WooCommerce is stored as as WordPress post with post_type = 'shop_order' in the table wp_posts.
Each order item in the order is stored in wp_woocommerce_order_items, with additional information about each order item line in the table wp_woocommerce_order_itemmeta.
To add a new order:
Insert a new order into the wp_posts table
Add the requested order lines into wp_woocommerce_order_items
For each of the order lines, add the order line details into wp_woocommerce_order_itemmeta.
Example data from a MySQL dump:
INSERT INTO wp_posts VALUES (38,1,'2014-06-09 10:02:00','2014-06-09 10:02:00','','Order – June 9, 2014 # 10:02 AM','','publish','closed','closed','','order','','','2014-06-09 10:02:43','2014-06-09 10:02:43','',0,'http://example.com/?post_type=shop_order&p=38',0,'shop_order','',0);
INSERT INTO wp_woocommerce_order_items VALUES (33,'My product','line_item',38);
INSERT INTO wp_woocommerce_order_itemmeta VALUES (167,33,'_qty','1');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (168,33,'_tax_class','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (169,33,'_product_id','20');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (170,33,'_variation_id','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (171,33,'_line_subtotal','13');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (172,33,'_line_subtotal_tax','');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (173,33,'_line_total','13');
INSERT INTO wp_woocommerce_order_itemmeta VALUES (174,33,'_line_tax','');
I would recommend that you try manually creating an order in the database with the settings you want the generated order to have, that way you see the relations and the various settings that you will need to set manually.
I spent many hours on this code - so I want to share it.
It creates a new order, with one product, adds shipment to the order with the flat rate of shipment, by the postcode of the customer.
I use it to create subscription orders for my clients.
function create_order($user_id, $product_id)
{
$order = wc_create_order();
$order->set_created_via("subscription");
$product = wc_get_product($product_id);
$order->add_product($product, $quantity);
$order_id = $order->get_id();
$postcode = get_user_meta($user_id, 'shipping_postcode', true);
$package = array('destination' => array('country' => 'IL', 'postcode' => $postcode));
$zone = WC_Shipping_Zones::get_zone_matching_package($package);
$method = WC_Shipping_Zones::get_shipping_method( $zone->get_id() );
This was the hardest to find out. The get_shipping_method returns class name. The next line create instance of it.
$shipping = new $method;
Now get the price of the shipment:
$rate = new WC_Shipping_Rate();
$rate->id = 'flat_rate';
$rate->label = 'shipment';
$rate->cost = $shipping->cost;
$rate->calc_tax = 'per_order'; // Not sure about this one
// Adding it to the order
$order->add_shipping($rate);
$order->calculate_totals();
// assign the order to the current user
update_post_meta($order->get_id(), '_customer_user', $user_id);
// payment_complete
$order->payment_complete();`
Copy billing and shipment info
foreach (array('billing_first_name',
'billing_last_name',
'billing_phone',
'billing_address_1',
'billing_address_2',
'shipping_first_name',
'shipping_last_name',
'shipping_address_1',
'shipping_address_2',
'shipping_city',
'shipping_postcode') as $key) {
$values = get_user_meta($user_id, $key);
update_post_meta($order_id, "_" . $key, $values[0]);
}

how to update a table and insert a new record to another table in one query?

I have two table in mysql that is product and sell. The product contain the stock and the sell contain the purchasing record. Now i want to sell a product from the stock when i insert the quantity of selling item into the sell table then the same amount should be decrease in the stock that is product table. Please help me for the query.
Sell table insertion.
$cus_id = $_POST['customer'];
$reg_id = $_POST['cus_region'];
$cat_id = $_POST['product'];
$name = $_POST['name'];
$price = $_POST['unit_price'];
$quantity = $_POST['quantity'];
$date = $_POST['datepicker'];
$total = $price * $quantity;
$payment = $_POST['payment'];
$remainig = $total - $payment;
$query = "INSERT INTO sell SET cus_id = '".$cus_id."',reg_id = '".$reg_id."', cat_id = '".$cat_id."',product = '".$name."',selling_price = '".$price."',selling_date = '".$date."',item_quantity = '".$quantity."',amount_paid= '".$payment."',total_price = '".$total."', remaining = '".$remainig."'";
$res = mysql_query($query);
Two way to resolve that,
1) if you are getting the quantity of product you can make second query to deduct the quantity from stack table (product table)
2) You can use trigger for such operations, while you insert in to sales table that same quantity you can reduce from the product table
You have to just change or decrease the quantity of the product in PRODUCT table. So, you have to use UPDATE query.
This can solve your issue:
1) I consider that your cat_id is the unique product key to identify a product in your PRODUCT table.
2) I consider that you have quantity column in PRODUCT table, which store the quatity of the product. And 1 product is being sold at a time.
UPDATE TABLE_NAME SET quantity=quantity - 1
WHERE cat_id = "SOME_INT_ID_WITHOUT_QUOTES"

Categories