Magento 1.9 Create Order From Cart via Skript - php

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.

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

Avoid duplicate items when creating contact in Php-Ews

I'm using https://github.com/Garethp/php-ews/ library to access to my public contact folder on Exchange server.
This is how i create a contact.
$api = API::withUsernameAndPassword($server, $user, $pass);
$folder = $api->getFolderByDisplayName('Public', Enumeration\DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$contattiTotali = $api->getFolderByDisplayName('Contacts', $folder->getFolderId());
$id=$contattiTotali->getFolderId()->getId();
$api->setFolderId($contattiTotali->getFolderId());
$api->createContacts(array(
'GivenName' => 'Homer',
'Surname' => 'Simpson',
'EmailAddresses' => array(
'Entry' => array('Key' => Enumeration\EmailAddressKeyType::EMAIL_ADDRESS_1, '_value' => 'h.simpson#gmail.com')
),
//Creating multiple entries
'PhoneNumbers' => array(
'Entry' => array(
array('Key' => Enumeration\PhoneNumberKeyType::HOME_PHONE, '_value' => '000'),
array('Key' => Enumeration\PhoneNumberKeyType::BUSINESS_PHONE, '_value' => '111'),
)
),
'PhysicalAddresses' => array(
'Entry' => array(
'Key' => Enumeration\PhysicalAddressKeyType::HOME,
'street' => '123 Street',
'city' => '123 City',
'state' => '123 State',
'countryOrRegion' => '123 Country',
'postalCode' => '12345',
)
),
));
The code, actually, works fine, but if i execute it several times, it duplicates the contact.
Is there a way to check if the contact (email address is good enough) already exists before creating a new one?
The easiest way to telling if a contact already exists with a particular email address is to use the ResolveName operation eg
$request = new EWSType_ResolveNamesType();
$request->UnresolvedEntry = "address#domain.com";
$request->ReturnFullContactData = true;
$return = $ews->ResolveNames($request);
if ($return->ResponseMessages->ResolveNamesResponseMessage->ResponseCode == "NoError") {
return $return->ResponseMessages->ResolveNamesResponseMessage->ResolutionSet->Resolution->Mailbox->EmailAddress;
}

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?

Woocommerce API order id (class-wc-order.php)

I needed to add some params to the order_data array in(v2/class-wc-api-order.php) woocommere and i've created a plugin so my changes wouldn't be overwritten by every update but i cant manage to get the id from the class?
I want $id to be the same the id i send in with my API
$id =
$order = wc_get_order( $id );
$olddata = array(
'first_name' => $order->shipping_first_name,
'last_name' => $order->shipping_last_name,
'company' => $order->shipping_company,
'address_1' => $order->shipping_address_1,
'address_2' => $order->shipping_address_2,
'city' => $order->shipping_city,
'state' => $order->shipping_state,
'postcode' => $order->shipping_postcode,
'country' => $order->shipping_country,
);

how to create Order using magento v2_soap api in php

Could you tell me how can I create Order using magento v2_soap api?
It's not possible with default single api you need to create you own custom api
OR
You need to call multiple api to place order as follows -
$proxy = new SoapClient('http://mywebside.com/api/v2_soap/?wsdl');
$sessionId = $proxy->login($user, $password);
$cartId = $proxy->shoppingCartCreate($sessionId, 1);
// load the customer list and select the first customer from the list
//$customerList = $proxy->customerCustomerList($sessionId, array());
//$customer = (array) $customerList[188];
//Do not change this credentials
$customer['customer_id'] = 199; // customer id
$customer['created_at'] = '2016-02-03 19:24:41';
$customer['updated_at'] = '2016-04-22 03:33:33';
$customer['store_id'] = 1;
$customer['website_id'] = 1;
$customer['created_in'] = 'Default Store View';
$customer['email'] = 'test#gmail.com';
$customer['firstname'] = 'test';
$customer['lastname'] = 'test';
$customer['group_id'] = 1;
$customer['password_hash'] = 'assassaXXXXO';
$customer['mode'] = 'customer';
$proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);
// load the product list and select the first product from the list
//$productList = $proxy->catalogProductList($sessionId);
// $product = (array) $productList[0];
$product= array(array(
'product_id' => '43001',
'sku' => 'SKU420',
'qty' => '2',
),
array(
'product_id' => '43002',
'sku' => 'SKUZ42B2',
'qty' => '1',
));
try{
$proxy->shoppingCartProductAdd($sessionId, $cartId, $product);
} catch (SoapFault $e) {
$error['product'] = $e->getMessage();
}
$address = array(
array(
'mode' => 'shipping',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => '',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
array(
'mode' => 'billing',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => '',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
);
// add customer address
try{
$proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
} catch (SoapFault $e) {
$error['shipping'] = $e->getMessage();
}
try{
// add shipping method
$proxy->shoppingCartShippingMethod($sessionId, $cartId, 'freeshipping_freeshipping');
} catch (SoapFault $e) {
$result = $e->getMessage();
}
// add payment method
enter code here
$paymentMethod = array(
'method' => 'cashondelivery'
);
$proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
// place the order
$orderId = $proxy->shoppingCartOrder($sessionId, $cartId, null, null);
There is a cart object where you can attach a customer and products.
information on cart_product.add (SOAP v1) or shoppingCartProductAdd (SOAP v2) is in Magento API documenattion
http://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html

Categories