Jump to content

how to create a order the developer way


Recommended Posts

Hello, i'm making a module that imports products and orders via a file, i had no problems making the product import, what i want to know is how to create a new order, with the current information i have available in the file?.

 

Order Header:

- Reference

- Description

Order Product:

- Reference

- Quanitity

 

When i try to create a new Order, it requires the address, customer and other information that i don't have since it's not a normally processed order, how can i get around this problem? Along with the products assigned.

$new_order = new Order();
$new_order->reference = 'PRODUCTION PC 001';
$new_order->add();

Thanks.

Link to comment
Share on other sites

A lot of backend processing happens when an order is created.

The way I do it is to create a new cart, add the products, and then use the BoOrder payment method.  If you create one of those, and call the validateOrder function on it, with your cart id, it will do everything for you.

An order needs to have a customer though, and an associated address, no way around that other than creating a dummy customer.  You can probably insert the data into the tables since there are no foreign key checks, but I doubt you'll be able to open them in the back office.

Link to comment
Share on other sites

Hello, thank you, i applied the strategy you sugested, created a default customer with a default address when installing the module, then save the id in the Configuration's table.

For creating the product i used the wirepayment module. Here's a simplified version.

 

public function install()
{
   
    // Create user for orders imported from this module.
    $new_customer = new Customer();
    $new_customer->email = '[email protected]';
    $new_customer->lastname = 'test';
    $new_customer->firstname = 'User';
    $new_customer->passwd = 'no password';
    $new_customer->add();
    $id_customer = $new_customer->id;

    // Create delivery address.
    $new_address = new Address();
    $new_address->alias = 'Test Address';
    $new_address->firstname = 'Test';
    $new_address->lastname = 'Address';
    $new_address->city = 'City';
    $new_address->id_state = 0;
    $new_address->id_customer = $id_customer;
    $new_address->id_country = $this->context->country->id;
    $new_address->address1 = 'Address 1';
    $new_address->address2 = 'Address 2';
    $new_address->add();
    $id_address = $new_address->id;

    return  parent::install() &&
            Configuration::updateValue("dummy_customer", $id_customer) &&
            Configuration::updateValue("dummy_address", $id_address);
}

public function uninstall()
{
    // Delete customer
    $customer = new Customer( Configuration::get('dummy_customer') );
    $customer->delete();

    // Delete address
    $address = new Address( Configuration::get('dummy_address') );
    $address->delete();

    return  parent::uninstall() &&
            Configuration::deleteByName('dummy_customer') &&
            Configuration::deleteByName('dummy_address');
}

public function customHook($params) {

    // Cart information
    $new_cart = new Cart();
    $new_cart->id_customer = Configuration::get('dummy_customer');
    $new_cart->id_address_delivery = Configuration::get('dummy_address');
    $new_cart->id_address_invoice  = Configuration::get('dummy_address');
    $new_cart->id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
    $new_cart->id_currency = $this->context->currency->id;
    $new_cart->id_carrier = 1;

    $new_cart->add();

    // Add the products to the cart
    $result = $new_cart->updateQty( 15, 3); // Added 15 products to product with the id number 3

    // Creating order from cart
    $payment_module = Module::getInstanceByName('ps_wirepayment');
    $result = $payment_module->validateOrder($new_cart->id, Configuration::get('PS_OS_BANKWIRE'), $new_cart->getOrderTotal(), 'Unknown', 'Test');

    // Get the order id after creating it from the cart.
    $id_order = Order::getOrderByCartId($new_cart->id);
    $new_order = new Order($id_order);
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...