0

I have a module of rest service api, that is used to receive users upload products. Here is my code...

function commerce_services_product_create($data, $flatten_fields) {
    $status =1;
    $commerce_price = array();

    $sku = $data['sku'];
    $title = $data['title'];
    $type = $data['type'];
    $commerce_price = $data['commerce_price'];
    $price = $commerce_price['amount'];

    $cp = commerce_product_new('product');
    $cp->uid = 1;
    $cp->sku = $sku;
    $cp->title = $title;
    $cp->language = LANGUAGE_NONE;  

    $cp->commerce_price = array(
        LANGUAGE_NONE => array( 
            0 => array(
                'amount' => $price ,
                'currency_code' => 'MMK',
     )));

    // for store 
    $cp -> cmp_store = array(LANGUAGE_NONE => $data["cmp_store"]);
    // for category
    $cp->field_category[LANGUAGE_NONE][0]['value'] = 137;

    try{
        commerce_product_save($cp);
        $result = "success!";
    }catch(Exception $ex){
        $result = "not success!, error -> ". $ex;
    }
}

When I pass the following json string to the function, this function create a product but not include cmp_store, mean cmp_store is null.

{
  "sku": "saa_prduct120",
  "title": "saa_prduct",
  "type": "product",
  "commerce_price": {
    "amount": "150000",
    "currency_code": "USD"
  },
 "cmp_store": 2
}

And when I check store drop down button with themer info module, it shows #name as like this, "field_product[und][form][cmp_store][und]".

Above my code I use this code for adding store, mean when users upload their product, they have to choose store. User choose store is passed by json.

 // for adding store
 $cp -> cmp_store = array(LANGUAGE_NONE => $data["cmp_store"]);

And I also try like this,

$new_product ->field_data_cmp_store[LANGUAGE_NONE]['cmp_store']['und'] = $data["cmp_store"];
$new_product -> field_product[cmp_store][und] = $data["cmp_store"];
$new_product -> cmp_store = $data["cmp_store"];
$form_state['values']['cmp_store']['und'] =   $data["cmp_store"];

not ok yet!

1 Answer 1

-1

This might need to work around as below

['field_store']['und']['#options'] = Store_array

1
  • it doesn't work, cmp_store field is still null.
    – SAWAUNG
    Commented Nov 17, 2015 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.