Getting/saving Products With Correct Options

Hello. I am trying to add an order correctly by using remote control . But I cannot place the order with correct options. The products should be added with their correct options related that product.



The code part below gets the product. But the product in my shop has Option Combination. Each product variant has its own product code.


<br />
Example:<br />
* Product ID: 12, code: YKC<br />
* Option combination 1: Product Code: YKC-1<br />
* Option combination 2: Product Code YKC-2<br />

```<br />
<br />
For example I get product with product code "YKC-2", but the product is added with its first options which product code is "YKC-1". So, product which is added correct, but product option that is added to cart in INCORRECT. How can be chosen the correct product option according to product code?<br />
<br />
Below ypou can see the code part I use: ([i]Sorry, I am not good at coding[/i])<br />
<br />
```php
<br />
<br />
$product_code= array('YKC-2','TRED1'); //for example<br />
<br />
		foreach ($product_code as $pod)  {<br />
			  //Find product<br />
			  $product_id = db_get_field("SELECT product_id FROM ?:products WHERE product_code = ?s", $pod);<br />
			  if( empty($product_id) ) { // Try option inventory<br />
			  $product_id = db_get_field("SELECT product_id FROM ?:product_options_inventory WHERE product_code=?s", $pod);<br />
			  $option_id = db_get_field("SELECT option_id FROM ?:product_options WHERE product_id = ?i AND option_type = ?s", $product_id, 'S');<br />
			  }<br />
			<br />
			<br />
			<br />
			  $option_data = fn_get_product_option_data($option_id, $product_id);<br />
				  <br />
					  foreach ($option_data['variants'] as $_k => $_v) {<br />
					  $c_hash = fn_generate_cart_id($product_id, array('product_options' => array($option_id => $_v['variant_id'])));<br />
					  $c = fn_get_options_combination(array($option_id => $_v['variant_id']));<br />
					  $cart['products'][$_k]['product_options'] = $c;<br />
					  }<br />
			<br />
			<br />
			 // Add to cart button was pressed for single product on advanced list<br />
			 $_REQUEST['product_data'][$product_id]['amount'] = $amount ? $amount : 1;<br />
			 if( empty($_REQUEST['product_data']) && $_SERVER['REQUEST_METHOD'] != 'POST' ) {<br />
			 $_REQUEST['product_data'][$product_id] = fn_get_product_data($product_id, $auth);<br />
			 }<br />
		}<br />
	<br />
	 $cart['user_id'] = $user_data['user_id'];<br />
	 $auth['user_id'] = $cart['user_id'];<br />
	 $cart['user_data'] = fn_get_user_info($cart['user_id']);<br />
	<br />
		<br />
	 $auth['referer'] = 'http://localhost/index.php?dispatch=checkout.checkout';<br />
	 $auth['points'] = 0;<br />
<br />
	 //payment method<br />
	 $cart['payment_id'] = 2;<br />
	<br />
	 //shipping id<br />
	 $cart['shipping_id'] = 6;<br />
	<br />
	 $cart['payment_surcharge'] = 0;<br />
	 $cart['chosen_shippings'] = array();<br />
	 $cart['chosen_shippings'][0] = fn_get_shipping_params(6);<br />
	<br />
	<br />
	 fn_checkout_update_shipping($cart, array(6));<br />
	 fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);<br />
	 fn_save_cart_content($cart, $auth['user_id']);<br />
	 $cart['change_cart_products'] = true;<br />
	 fn_calculate_cart_content($cart, $auth, 'S', false, 'F', false);<br />
	  <br />
	 list($order_id, $process_payment) = fn_place_order($cart, $auth, 'save');				  <br />

```<br />
<br />
I need help.

I tried lot. Here is the code I tried to add correct options. But result is still same. Incorrect options. How can I add product to cart with correct options?



Code:


$product_code= array('YKC-2','TRED1'); //for example
foreach ($product_code as $pod) {
//Product
$product_id = db_get_field("SELECT product_id FROM ?:products WHERE product_code = ?s", $pod);
if( empty($product_id) ) { // Try option inventory
$product_id = db_get_field("SELECT product_id FROM ?:product_options_inventory WHERE product_code=?s", $pod);
$option_id = db_get_field("SELECT option_id FROM ?:product_options WHERE product_id = ?i AND option_type = ?s", $product_id, 'S');
$combination = db_get_field("SELECT combination FROM ?:product_options_inventory WHERE product_code = ?s", $pod);
$combination = explode('_', $combination);
}

$product_data = fn_get_product_data($product_id, $auth, CART_LANGUAGE);
// $product_option_data = fn_get_product_option_data($option_id, $product_id);
//$product_options = fn_get_product_options($product_id, $auth);

$product_data = array (
'product_id' => array (
'product_id' => $product_data['product_id'],
'product_options' => $product_options,
'amount' => $quantity[1]
)
);

fn_add_product_to_cart($product_data, $cart, $auth);


foreach ($cart['products'] as $id => $product) {
//fn_print_r($cart['products'][$id]['product_options']);
//unset($product['extra']['product_options']);
//$product['extra']['product_options'] = $combination[1];
//fn_print_r($product['extra']['product_options']);

foreach ($cart['products'][$id]['product_options'] as $p =>$r) {
//fn_print_r($cart['products'][$id]['product_options']);
//fn_print_r($cart['products'][$id]['product_options'][$p]);
$cart['products'][$id]['product_options'][$p] = $combination[1];
fn_print_r($cart['products'][$id]['product_options'][$p]);
}

}
}

Could you please tell us the CS-Cart version you use.

[quote name='Alt-team' timestamp='1422947796' post='204127']

Could you please tell us the CS-Cart version you use.

[/quote]

Multivendor 4.2.4

Dear Ooaykac,



Your code is very complicated. Sure that it should be simpler.

But we can not tell you the correct variant, without examining it directly on your server.



Please contact us via manager@alt-team.com and we will help you.



Best regards, Alt-team.