'IGAC Product',
'page callback' => 'drupal_get_form',
'page arguments' => array('igac_product_form'),
'access arguments' => array('administer product'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function igac_product_permission() {
return array(
'administer product' => array(
'title' => t('Administer the IGAC product'),
'description' => t('Help administer the product'),
),
);
}
/**
* Callback form API.
*/
function igac_product_form($form, &$form_state) {
$form['info'] = array(
'#markup' => t('Creation to product'),
'#prefix' => '
',
'#suffix' => '
',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sincronizar'),
);
return $form;
}
/**
* Callback form submit.
*/
function igac_product_form_submit(&$form, &$form_state) {
// drupal_set_message('se creo perfecto');
$options = array(
'method' => 'GET',
'headers' => array('Content-Type' => 'text/xml; charset=UTF-8'),
);
$get = drupal_http_request('https://tramites.igac.gov.co/inventory/getAllProducts', $options);
dpr($get);
/* if ($get->code == 200){
$product = json_decode($get->data);
if (!empty($product)) {
foreach ($product as $value) {
dpm($value);
}
}
}*/
}
/**
* Create product and nodes.
*/
function igac_product_set_product() {
global $user;
// $get = restclient_get();
$options = array(
'method' => 'GET',
'headers' => array('Content-Type' => 'text/xml; charset=UTF-8'),
);
$get = drupal_drupal_http_requesthttp_request('https://tramites.igac.gov.co/inventory/getAllProducts', $options);
if ($get->code == 200){
$product = json_decode($get->data);
if (!empty($product)) {
foreach ($product as $value) {
// Created the product.
return igac_product_created_product($value);
// Adde product display.
// igac_product_created_node($product);
}
}
}
//return FALSE;
}
/**
* Make the producto of commerce.
*/
function igac_product_created_product($get_product) {
global $user;
if (commerce_product_load_by_sku($get_product->productCode)) {
$product = commerce_product_load_by_sku($get_product->productCode);
}
else {
$product = commerce_product_new('product');
$product->sku = $get_product->productCode;
}
$product->title = $get_product->name;
$product->language = LANGUAGE_NONE;
$product->uid = $user->uid;
$product->field_category[LANGUAGE_NONE][0]['tid'] = igac_product_created_taxonomy($get_product->category);
$product->field_weight[LANGUAGE_NONE][0]['value'] = $get_product->kgsWeigth;
$product->commerce_price[LANGUAGE_NONE][0] = array(
'amount' => $get_product->price, // $10
'currency_code' => "COP",
);
$product->commerce_stock[LANGUAGE_NONE][0]['value'] = $get_product->quantity;
commerce_product_save($product);
return $product;
}
/**
* Make the taxonomy.
*/
function igac_product_created_taxonomy($name, $vid = 3) {
$taxonomy = taxonomy_get_term_by_name($name);
if ($taxonomy) {
$term = reset($taxonomy);
return $term->tid;
}
$term = new stdClass();
$term->name = $name;
$term->vid = $vid;
taxonomy_term_save($term);
return $term->tid;
}
/**
* Make the nodes.
*/
function igac_product_created_node($get_product) {
global $user;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'products_display')
->fieldCondition('field_product', 'product_id', $get_product->product_id);
$result = $query->execute();
if (isset($result['node']) && $nid = array_values($result['node'])) {
$node = node_load($nid[0]->nid);
}
else {
$node = (object)array('type' => 'products_display');
node_object_prepare($node);
$node->uid = $user->uid;
}
$node->title = $get_product->title;
$node->field_product[LANGUAGE_NONE][]['product_id'] = $get_product->product_id;
$node->language = LANGUAGE_NONE;
node_save($node);
}
/**
* Implements hook_cron().
*/
function igac_product_cron() {
igac_product_set_product();
/* $commerce_product= array();
$entityType = 'commerce_product';
$resultsProduct = entity_load($entityType);
foreach ($resultsProduct as $key) {
$commerce_product[$key->sku]=$key->sku;
}
$productRest =array();
//$get = restclient_get();
$options = array(
'method' => 'GET',
'headers' => array('Content-Type' => 'text/xml; charset=UTF-8'),
);
$get = drupal_http_request('https://tramites.igac.gov.co/inventory/getAllProducts', $options);
if (is_object($get) && $get->code == 200){
$product = json_decode($get->data);
if (!empty($product)) {
foreach ($product as $value) {
$productRest[$value->productCode]=$value->productCode;
}
}
}
$no_exist = array();
foreach ($commerce_product as $key ) {
if (array_search($key, $productRest)) {
}else{
$no_exist[$key]=$key;
}
}
$productTest=array();
$entityTypeS = 'node';
$resultsProductS = entity_load($entityTypeS);
foreach ($resultsProductS as $key) {
$node=$key;
if($node->type=="products_display"){
$product = commerce_product_load($node->field_product['und'][0]['product_id']);
if (array_search($product->sku, $no_exist)) {
//dpm($node->nid);
//dpm($product->sku);
$node_disable = node_load($node->nid);
$node_disable->status = 0;
node_save($node_disable);
}
}
} */
}