Script to add manual order to Woocommerce - php

I am trying to add manual orders to woocommerce. Webshop is where I have stock values. I have store also. When customer buy in store, I have to add an order. When I want to add it via Orders->Add order it do not work properly, as I need to add tax value manual (Automattic, why?).
I'd like to have hidden page, to add orders.
I've seen Programmatically creating new order in Woocommerce
Here is what I tried:
I got order.php file in main folder:
<?php
/*
* Create order dynamically
*/
require(dirname(__FILE__) . '/wp-load.php');
echo 'ok?';
function create_vip_order() {
global $woocommerce;
$address = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test#gmail.com',
'phone' => '123456789',
'address_1' => '123 Main st.',
'city' => 'San Diego',
'state' => 'Ca',
'postcode' => '92121',
);
// Now we create the order
$order = wc_create_order();
// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( get_product( '376' ), 1 ); // This is an existing SIMPLE product
$order->set_address( $address, 'billing' );
//
$order->calculate_totals();
$order->update_status("Completed", 'Imported order', TRUE);
}
add_action( 'init', 'create_vip_order' );

Finally code that I need is:
<?php
/*
* Create order manual
*/
require(dirname(__FILE__) . '/wp-load.php');
function create_new_order() {
global $woocommerce;
$address = array(
'first_name' => 'Zakup',
'last_name' => 'Sklepowy',
'email' => 'test#test.pl',
'phone' => '123',
'address_1' => 'ul. Przykladowa 1',
'address_2' => 'm. 2',
'city' => 'Wroclaw',
'postcode' => '50-123',
);
$order = wc_create_order();
$product = new WC_Product(wc_get_product_id_by_sku('*sku_here*'));
$order->add_product( $product, 1 );
$order->set_address( $address, 'billing' );
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method( $payment_gateways['cod'] );
// Calculate totals
$order->calculate_totals();
$order->update_status('completed', 'In Store ', TRUE);
}
create_new_order();
?>

Related

Woo commerce Partials Payments programmatically order save issue

I am working on partials payments by creating order programmatically.
I am stuck with the issue when I am doing partials payments of a product in order. I created the order total successfully but woo commerce add remaining amount as a coupons, whereas I did not any coupons.
Here is my Function:
public function createNewOrder($order_array){
$address = array(
'first_name' => $order_array['first_name'],
'last_name' => $order_array['last_name'],
'company' => $order_array['company'],
'email' => $order_array['email'],
'phone' => $order_array['phone'],
'address_1' => $order_array['address_1'],
'address_2' => $order_array['address_2'],
'city' => $order_array['city'],
'state' => $order_array['state'],
'postcode' => $order_array['postcode'],
'country' => $order_array['country']
);
$args = array(
'customer_id' => $order_array['customer_id'],
'status' => $order_array['order_status'],
);
$order = wc_create_order($args);
$productArray = json_decode(stripslashes($order_array["productArray"]), true);
// This is an existing SIMPLE product
foreach ($productArray as $key => $value) {
$itemData=$value['id'];
$wc_deposit_enabled = get_post_meta( $itemData, '_wc_deposit_enabled', true );
$wc_deposit_amount = get_post_meta( $itemData, '_wc_deposit_amount', true );
$wc_amount = get_post_meta( $itemData,'_price',true );
if ($wc_deposit_enabled!="") {
$order->add_product( get_product($itemData), $value['quantity'], array(
'totals' => array(
'subtotal' => $wc_amount,
'total' => $wc_deposit_amount,
'subtotal_tax' => 0,
'tax' => 0,
)));
[enter image description here][1]
}else{
$order->add_product( get_product($itemData), $value['quantity']);
}
}
if(!empty($order_array['order_status']))
{
$order->update_status($order_array['order_status']);
}
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->save();
}

Change order shipping address for a custom product type in WooCommerce

I want set an alternative address if the cart has a product type that is equal on a specific type.
<?php
add_filter( 'woocommerce_checkout_create_order', 'mbm_alter_shipping', 10, 1 );
function mbm_alter_shipping ($order) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $product => $values) {
$product->get_product();
if ($product->is_type('octopus')) {
$address = array(
'company' => 'Test',
'email' => 'test#test.com',
'phone' => '777-777-777-777',
'city' => 'London',
'state' => '',
'postcode' => '12345',
'country' => 'UK'
);
}
$order->set_address( $address, 'shipping' );
}
return $order;
}
?>
But when trying to place an order, I get an Internal Server Error and the order is not placed.
What I am doing wrong? Any help is appreciated.
The woocommerce_checkout_create_order is an action hook, your code is outdated and full of mistakes… Try to use the following instead:
add_action( 'woocommerce_checkout_create_order', 'alter_order_shipping_address', 10, 2 );
function alter_order_shipping_address( $order, $data ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Targetting a custom product type
if ( $cart_item['data']->is_type('octopus') ) {
// Changing shipping address
$order->set_address( array(
'company' => 'Test',
'email' => 'test#test.com',
'phone' => '777-777-777-777',
'city' => 'London',
'state' => '',
'postcode' => '12345',
'country' => 'UK'
), 'shipping' );
break; // stop the loop
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Magento 1.9 Create Order From Cart via Skript

I try to create an order with item from cart.
I've found a skript that worked on ssh in magento root, but when I call the skript via checkout/cart->skript-php I got 500 Server Error.
My Skript is here:
<?php
require_once 'app/Mage.php';
Mage::app();
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
$product = Mage::getSingleton('checkout/cart')->getItems();
//$product = Mage::getModel('catalog/product')->load(87561); /* 6 => Some product ID */
$buyInfo = array('qty' => 1);
$quote->addProduct($product, new Varien_Object($buyInfo));
$billingAddress = array(
'firstname' => 'Me',
'lastname' => 'And',
'company' => 'None',
'email' => 'asdf#asdf.com',
'street' => array(
'Sample Street Line_1',
'Sample Street Line_2'
),
'city' => 'City',
'region_id' => '',
'region' => 'State/Province',
'postcode' => '12345',
'country_id' => 'DE',
'telephone' => '1234567890',
'fax' => '123456987',
'customer_password' => '',
'confirm_password' => '',
'save_in_address_book' => '0',
'use_for_shipping' => '1',
);
$quote->getBillingAddress()
->addData($billingAddress);
$quote->getShippingAddress()
->addData($billingAddress)
->setShippingMethod('solutioo')
->setPaymentMethod('cashondelivery')
->setCollectShippingRates(true)
->collectTotals();
$quote->setCheckoutMethod('guest')
->setCustomerId(null)
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
$quote->getPayment()->importData( array('method' => 'cashondelivery'));
$quote->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
//$order = $service->getOrder();
2 Questions:
1- How should I call the skript from Magento Cart so it will work?
2- Is this the right way to get the product
Hope somebody can help me, ... Thanks in advance.

How do you add a new Line Item(non product)/Fee to a existing Order?

o/
Working on a minor project, and I'm creating a WC order from the admin area (programmatically), and i want to add a line item(Non product/fee to the order after it been created, but how exactly do i do that?
I create a order by doing:
$address = array(
'first_name' => $sBillingFirstName,
'last_name' => $sBillingLastName,
'company' => $sBillingCompanyName,
'email' => $sBillingEmail,
'phone' => $sBillingPhone,
'address_1' => $sBillingAddress1,
'address_2' => $sBillingAddress2,
'city' => $sBillingCity,
'state' => '',
'postcode' => $sBillingZipcode,
'country' => $sBillingCountry,
);
$oOrder = wc_create_order();
$oOrder->set_address( $address, 'billing' );
$oOrder->set_address( $address, 'shipping' );
and normally i would just do add_fee, as seen below to add a "non product" lineitem/fee, but since i dont have a cart object - what am i supposed to do?
wc->cart->add_fee("deposit", 200);
but i don't have a cart object, so how do i go about creating a the deposit in my new order?

How to add order delivery date and time in WooCommerce order?

Currently i am working on a REST API for place order and i have
created order using following code.
<?php
$order = wc_create_order(array('customer_id' => $user_id));
$billing_addre = array(
'first_name' => $_POST['f_name'],
'last_name' => $_POST['l_name'],
'company' => '',
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'address_1' => $_POST['address'],
'address_2' => '',
'city' => $_POST['city'],
'state' => $_POST['state'],
'postcode' => $_POST['p_code'],
'country' => $_POST['country']
);
$order->set_address( $billing_addre, 'billing' );
?>
it's working good but i would like to add order delivery date and time
during create order, i added it by using custom field like
<?php
update_post_meta( $order->id, 'delivery_date', $_REQUEST["delivery_date"] );
?>
but that is no best way,( i don't do this by any plugin ) if anyone know better solution for this.
Thank You!

Categories