Order by cronjob - including Hikmarket vendors

  • Posts: 62
  • Thank you received: 1
6 years 5 months ago #293293

-- HikaShop version -- : 3.4.0
-- HikaMarket version -- : 2.1.1
-- Joomla version -- : 3.8.8
-- PHP version -- : 7.0.30
-- Browser(s) name and version -- : Chrome latest
-- Error-message(debug-mod must be tuned on) -- : none

Hello,

I am currently building a script to create an order through a cronjob. The cronjob retrieves orders made by a client using EDI ( en.wikipedia.org/wiki/Electronic_data_interchange ).

I would like to create the least amount of custom code and use as many standardized code from Hikashop and Hikmarket itself.

Currently I have used the example code of the developers documentation using this:
In a for each edi_product loop

				$product = new stdClass();

				$productClass = hikashop_get('class.product');
				$product = $productClass->get($productRow[0]);
				$product->order_product_id = $product->product_id;
				$product->order_product_name = $product->product_name;
				$product->order_product_code = $product->product_code;
				$product->order_product_quantity = $edi_product->product_quantity;
(...)
				$currencyClass->getPrices($product,$ids,$currency_id,$main_currency,$zone_id,$discount_before_tax);
				$product->order_product_price = $product->prices[0]->price_value;
	
				$fullprice += $product->order_product_quantity*$product->order_product_price;

				$products[] = $product;
Then creating the order
	$order = new stdClass();
	$order->order_user_id = $order_user_id;
	$status = $config->get('order_created_status');
	$order->order_status = $status;
(...)
	$order->cart->products = $products;
	$order->order_full_price = $fullprice;
		$paymentClass = hikashop_get('class.payment');
		$paymentClass->checkPaymentOptions($order);

		$orderClass = hikashop_get('class.order');
		$order->order_id = $orderClass->save($order);

This will create the order fine, but it will not create the sub orders of the vendors. Is there a way to use Hikashop or Hikamarket standardized code to get what needs to be done? Maybe simulating a checkout? I am able to simulate the login of the user in the cronjob if needed.

Perhaps you can steer me in the right direction. I would prefer to use as little custom code as possible for maintainability.

Thank you in advance.

Cheers,
Teeuwis

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
6 years 5 months ago #293298

Hi,

You don't have all the necessary data in the $order for HikaMarket to be able to handle its process properly.
the "finish" process of the checkout provides a lot more things in $order in which HikaMarket can base itself.

I would recommand to do it differently:
- first, use the "addProduct" function of class.cart to add the products to a cart:

$cartClass = hikashop_get('class.cart');
$cart_id = $cartClass->getCurrentCartId();
$cartClass->addProduct($cart_id, array(array('id'=>XX,'qty'=>YY), array('id'=>AA, 'qty'=>BB)));
where XX is the id of a product and YY its quantity and AA the id of another product and BB its quantity (and you can add more products as well if you want.
- second, convert the cart into an order with the "createFromCart" function of class.order:
$orderClass = hikashop_get('class.order');
$order = $orderClass->createFromCart($cart_id);
That will do a lot more things that you didn't do with your code, it's much more simple, and it will avoid a lot of problems for you.

The following user(s) said Thank You: nuntius

Please Log in or Create an account to join the conversation.

  • Posts: 62
  • Thank you received: 1
6 years 5 months ago #293489

Hi Nicolas,

Thank you for this.

When I replace my code using the code you suggested, it generates a nearly empty order. Perhaps I integrated it wrong in my code.

I also get this notice:
Notice: Undefined property: stdClass::$cart_type in /var/www/vhosts/nuntius.nl/cpgh.nuntius.nl/administrator/components/com_hikashop/classes/order.php on line 549

	$hikadir = $local_dir_root.'administrator'.DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php';
	if(!@include_once($hikadir)){ return false; }
	
	$config =& hikashop_config();

	$cartClass = hikashop_get('class.cart');

foreach ($edi_klant as $key => $value) {
(...)
// Here i retrieve all customer data relevant to the EDI
(...)
	$query = $db->getQuery(true)
	    ->select('id, password')
	    ->from('#__users')
	    ->where('username=' . $db->quote($credentials['username']));

	$db->setQuery($query);
	$result = $db->loadObject();

	if ($result)
	{
	   $match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id);

	    if ($match === true)
	    {
  	        $user = JUser::getInstance($result->id);
		$loginresult = $app->login($credentials);
	    }
	    else
	    {
// login failed (...)
	    }
	} else {
	    // Invalid user (...)
	}
	$product_data = array();

	foreach ($edi_bestelling[$key] as $pkey => $edi_product) {

		$query = $db->getQuery(true);

		$query
		    ->select(array($db->qn('product_id'),'COUNT(*)'))
		    ->from($db->qn('#__hikashop_product'))
		    ->where($db->qn('product_ean') . ' = '. $db->q($edi_product->product_ean))
		    ->group('product_id');

		$db->setQuery($query);
		$productRow = $db->loadRow();

		if(is_array($productRow)) {
			if($productRow[1] == 1) {
				$product_data[] = array('id' => $productRow[0], 'qty' => $edi_product->product_quantity);
			} elseif($productRow[1] == 0) {
// No product found
			} elseif($productRow[1] > 1) {
// No unique product for EAN
			}	
		}
	}

	$cart_id = $cartClass->getCurrentCartId();
	$cartClass->addProduct($cart_id, $product_data);

	$orderClass = hikashop_get('class.order');
	$order = $orderClass->createFromCart($cart_id);

	$orderClass->save($order);

	if($order) {
		print "SUCCES";
		print_r($order);
	} else {
		print "FAIL";
		print_r($order);
	}
	$app->logout( $result->id );

Please Log in or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
6 years 5 months ago #293508

Hello,

Please note that Nicolas gave you the basis for an order creation via a cart.
But you will need to provide more information to the cart regarding the addresses, etc. The kind of data that your customer provide during the checkout !

Even if the HikaShop/HikaMarket support can help with the understand and usage of the components ; your request is more related to custom development. Please understand that we cannot analyze and correct your custom code for you.
I would recommend you to take a look at the HikaShop cart class and some code related to the checkout ; it should help you to see what functions you would need to provide the data.
Please also not that "getCurrentCardId" is a front-end function which will give you the cart for the current user ; you might want to use directly the cart_id for a specific user if you're running in the backend.
You might also want to take a look at the function "createOrderFromCart" if you do not want to use cart to create your orders ; but it would require some development.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: nuntius

Please Log in or Create an account to join the conversation.

  • Posts: 62
  • Thank you received: 1
6 years 5 months ago #293881

Hello Jerome,

Thank you for your input. I've managed to develop a working cronjob now.

Just one more question, where can I find related code to custom fields for the order which the normally client fills in the front-end?

I will have to input them manually, but using the code below doesn't seem to work. Where can I find related check-out code for giving custom fields their value?

	$order->klantreferentie = $value->referentie;
(...)
	$orderClass->save($order);

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
6 years 5 months ago #293887

Hi,

That code is fine.
Either there is nothing in $value->referentie or there is some code in (...) which touch to the value in $order->klantreferentie.

The following user(s) said Thank You: nuntius

Please Log in or Create an account to join the conversation.

  • Posts: 62
  • Thank you received: 1
6 years 5 months ago #293967

Nevermind. I debugged it....

And for anyone interested, here is the code.

	$cart = new stdClass();
	$cart->user_id = $order_user_id; // Logged in user

	// Save an entry to DB to be able to get cart_id
	$cartClass->save($cart);
	$cart_id = $cartClass->getCurrentCartId('cart');

	// Add all selected products at once
	$cartClass->addProduct($cart_id, $product_data);

	// Create basic order from cart input
	$orderClass = hikashop_get('class.order');
	$order = $orderClass->createFromCart($cart_id);

	// Retrieve current order to update custom fields
	$updateOrder = new stdClass();
	$updateOrder = $orderClass->get($order->order_id);

	// Custom field(s) value(s)
	$updateOrder->klantreferentie = $value->referentie;

	// Save order with custom field values
	$orderClass->save($updateOrder);

Last edit: 6 years 5 months ago by nuntius.

Please Log in or Create an account to join the conversation.

Moderators: Obsidev
Time to create page: 0.045 seconds
Powered by Kunena Forum