| 1 |
<?php |
<?php |
| 2 |
// $Id: magento_products.module |
// $Id: magento_products.module |
| 3 |
/** |
/** |
| 4 |
* This module allows to synchronize products with Magento. |
* This module allows to synchronize products with Magento. |
| 5 |
*/ |
*/ |
| 6 |
define (DATE_FORMAT, 'Y-m-d\TH:i:s'); |
define (DATE_FORMAT, 'Y-m-d\TH:i:s'); |
| 7 |
/** |
/** |
| 8 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 9 |
*/ |
*/ |
| 10 |
function magento_products_menu() { |
function magento_products_menu() { |
| 11 |
$items['admin/settings/magento_products'] = array( |
$items['admin/settings/magento_products'] = array( |
| 12 |
'title' => t('Magento Products Settings'), |
'title' => t('Magento Products Settings'), |
| 13 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 14 |
'page arguments' => array('magento_products_settings'), |
'page arguments' => array('magento_products_settings'), |
| 15 |
'access arguments' => array('access administration pages'), |
'access arguments' => array('access administration pages'), |
| 16 |
'type' => MENU_NORMAL_ITEM, |
'type' => MENU_NORMAL_ITEM, |
| 17 |
); |
); |
| 18 |
return $items; |
return $items; |
| 19 |
} |
} |
| 20 |
/** |
/** |
| 21 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 22 |
*/ |
*/ |
| 23 |
function magento_products_form_alter(&$form, $form_state, $form_id) { |
function magento_products_form_alter(&$form, $form_state, $form_id) { |
| 24 |
if ($form_id == 'magento_api_synchro_settings') { |
if ($form_id == 'magento_api_synchro_settings') { |
| 25 |
// Add button for Manual Synchronization to Settings page |
// Add button for Manual Synchronization to Settings page |
| 26 |
$form['magento_synchronize']['synchronize_products'] = array( |
$form['magento_synchronize']['synchronize_products'] = array( |
| 27 |
'#type' => 'submit', |
'#type' => 'submit', |
| 28 |
'#value' => 'Synchronize Products into Nodes', |
'#value' => 'Synchronize Products into Nodes', |
| 29 |
'#submit' => array('magento_products_test'), |
'#submit' => array('magento_products_test'), |
| 30 |
); |
); |
| 31 |
return $form; |
return $form; |
| 32 |
} |
} |
| 33 |
} |
} |
| 34 |
/** |
/** |
| 35 |
* |
* |
| 36 |
* Setting for Product synchronization cache time. |
* Setting for Product synchronization cache time. |
| 37 |
* |
* |
| 38 |
*/ |
*/ |
| 39 |
function magento_products_settings() { |
function magento_products_settings() { |
| 40 |
$form = array(); |
$form = array(); |
| 41 |
$path = drupal_get_path('module', 'magento_products'); |
$path = drupal_get_path('module', 'magento_products'); |
| 42 |
drupal_add_js($path . '/js/show_up.js'); |
drupal_add_js($path . '/js/show_up.js'); |
| 43 |
|
|
| 44 |
$form['magento_products_cache_time'] = array( |
$form['magento_products_cache_time'] = array( |
| 45 |
'#type' => 'radios', |
'#type' => 'radios', |
| 46 |
'#title' => t('Products info cache time'), |
'#title' => t('Products info cache time'), |
| 47 |
'#description' => t('Select how long additional info for products will be stored in cache'), |
'#description' => t('Select how long additional info for products will be stored in cache'), |
| 48 |
'#default_value' => variable_get('magento_products_cache_time', 'CACHE_PERMANENT'), |
'#default_value' => variable_get('magento_products_cache_time', 'CACHE_PERMANENT'), |
| 49 |
'#options' => array('CACHE_PERMANENT' => t('Permanent'), 'CACHE_TEMPORARY' => t('Temporary'), 'CUSTOM' => 'Custom Time'), |
'#options' => array('CACHE_PERMANENT' => t('Permanent'), 'CACHE_TEMPORARY' => t('Temporary'), 'CUSTOM' => 'Custom Time'), |
| 50 |
); |
); |
| 51 |
$form['magento_products_cache_custom_time'] = array( |
$form['magento_products_cache_custom_time'] = array( |
| 52 |
'#prefix' =>'<div id="cache_time" style="display:none">', |
'#prefix' =>'<div id="cache_time" style="display:none">', |
| 53 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 54 |
'#title' => t('Time in hours'), |
'#title' => t('Time in hours'), |
| 55 |
'#default_value' => variable_get('magento_products_cache_custom_time', 24), |
'#default_value' => variable_get('magento_products_cache_custom_time', 24), |
| 56 |
'#element_validate' => array('magento_products_cache_time_validate'), |
'#element_validate' => array('magento_products_cache_time_validate'), |
| 57 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 58 |
); |
); |
| 59 |
return system_settings_form($form); |
return system_settings_form($form); |
| 60 |
} |
} |
| 61 |
/** |
/** |
| 62 |
* Validate time that was entered on settings page. |
* Validate time that was entered on settings page. |
| 63 |
*/ |
*/ |
| 64 |
function magento_products_cache_time_validate($element, &$form_state) { |
function magento_products_cache_time_validate($element, &$form_state) { |
| 65 |
if (is_numeric($element['#value']) ) { |
if (is_numeric($element['#value']) ) { |
| 66 |
if ($element['#value'] <= 0) { |
if ($element['#value'] <= 0) { |
| 67 |
form_error($element, t('This field must be positive.')); |
form_error($element, t('This field must be positive.')); |
| 68 |
} |
} |
| 69 |
}else form_error($element, t('This field must be numeric.')); |
}else form_error($element, t('This field must be numeric.')); |
| 70 |
} |
} |
| 71 |
/** |
/** |
| 72 |
* Implementation of hook_nodeapi(). |
* Implementation of hook_nodeapi(). |
| 73 |
*/ |
*/ |
| 74 |
function magento_products_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
function magento_products_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
| 75 |
global $user; |
global $user; |
| 76 |
if ($node->type == 'product' && $op == 'view' && $a4 == TRUE) { |
if ($node->type == 'product' && $op == 'view' && $a4 == TRUE) { |
| 77 |
// Get information about actual product price |
// Get information about actual product price |
| 78 |
$price = magento_products_get_actual_price($node, $user); |
$price = magento_products_get_actual_price($node, $user); |
| 79 |
drupal_set_message(t('Price of this product') . ' = ' . $price); |
$actual_price = $node->field_actual_price[0]['value']; |
| 80 |
$tier_price = magento_products_get_tier_price($node, $price); |
if ($price != $actual_price) { |
| 81 |
foreach ($tier_price as $qty => $cost) { |
$node->field_actual_price[0]['value'] = $price; |
| 82 |
drupal_set_message('Buy ' . $qty . ' for ' . $cost . ' each'); |
node_save($node); |
| 83 |
} |
} |
| 84 |
// Getting additional product info from Cache or Magento if not cached yet |
// drupal_set_message(t('Price of this product') . ' = ' . $price); |
| 85 |
$info = cache_get('product_info_' . $node->field_product_id[0]['value'],'cache'); |
// actual price can get from $node->field_actual_price[0]['value'] within node template |
| 86 |
// If there is no information in cache or it is expired - reorder it |
$tier_price = magento_products_get_tier_price($node, $price); |
| 87 |
if ($info == 0 || time() > $info->expire) { |
foreach ($tier_price as $qty => $cost) { |
| 88 |
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
//drupal_set_message('Buy ' . $qty . ' for ' . $cost . ' each'); |
| 89 |
$info = magento_api_catalog_product_full_info($node->field_product_id[0]['value'],$StoreView); |
// tear price can get from $node->field_tier_price[0]['value'] within node template |
| 90 |
$time = variable_get('magento_products_cache_time', 'CACHE_PERMANENT'); |
} |
| 91 |
if ($time == 'CUSTOM') { |
// Getting additional product info from Cache or Magento if not cached yet |
| 92 |
$time = variable_get('magento_products_cache_custom_time', 24); |
magento_products_cache_full_info($node); |
| 93 |
cache_set('product_info_' . $node->field_product_id[0]['value'], $info,'cache',strtotime ("+". $time ." hour")); |
} |
|
} else cache_set('product_info_' . $node->field_product_id[0]['value'], $info,'cache',$time); |
|
|
} |
|
|
} |
|
| 94 |
} |
} |
| 95 |
|
|
| 96 |
|
/** |
| 97 |
|
* Getting additional product info from Cache or Magento if not cached yet |
| 98 |
|
*/ |
| 99 |
|
function magento_products_cache_full_info($node) { |
| 100 |
|
$info = cache_get('product_info_' . $node->field_product_id[0]['value'],'cache'); |
| 101 |
|
// If there is no information in cache or it is expired - reorder it |
| 102 |
|
if ($info == 0 || time() > $info->expire) { |
| 103 |
|
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
| 104 |
|
$info = magento_api_catalog_product_full_info($node->field_product_id[0]['value'],$StoreView); |
| 105 |
|
$time = variable_get('magento_products_cache_time', 'CACHE_PERMANENT'); |
| 106 |
|
if ($time == 'CUSTOM') { |
| 107 |
|
$time = variable_get('magento_products_cache_custom_time', 24); |
| 108 |
|
cache_set('product_info_' . $node->field_product_id[0]['value'], $info,'cache',strtotime ("+". $time ." hour")); |
| 109 |
|
} else cache_set('product_info_' . $node->field_product_id[0]['value'], $info,'cache',$time); |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
|
| 113 |
/** |
/** |
| 114 |
* Implementation of hook_magento_event(). |
* Implementation of hook_magento_event(). |
| 115 |
*/ |
*/ |
| 116 |
function magento_products_magento_event($event, $productId){ |
function magento_products_magento_event($event, $productId){ |
| 117 |
// Synchonize updated product |
// Synchonize updated product |
| 118 |
if (($event == 'catalog_product_save' || $event == 'product_status_update') && !empty($productId)) { |
if (($event == 'catalog_product_save' || $event == 'product_status_update') && !empty($productId)) { |
| 119 |
// Get product info |
// Get product info |
| 120 |
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
| 121 |
$product = magento_api_catalog_product_list(array('entity_id' => array('eq' => intval($productId))),$StoreView); |
$product = magento_api_catalog_product_list(array('entity_id' => array('eq' => intval($productId))),$StoreView); |
| 122 |
if ($product) magento_product_synchronize($product[0]); |
if ($product) magento_product_synchronize($product[0]); |
| 123 |
} |
} |
| 124 |
// Delete from Drupal deleted product |
// Delete from Drupal deleted product |
| 125 |
if ($event == 'catalog_product_delete' && !empty($productId)) { |
if ($event == 'catalog_product_delete' && !empty($productId)) { |
| 126 |
// Get node id by product_id |
// Get node id by product_id |
| 127 |
$nid = magento_products_get_nid($productId); |
$nid = magento_products_get_nid($productId); |
| 128 |
if($nid) node_delete($nid); |
if($nid) node_delete($nid); |
| 129 |
} |
} |
| 130 |
} |
} |
| 131 |
/* |
/* |
| 132 |
* Synchronize Magento products to Drupal |
* Synchronize Magento products to Drupal |
| 133 |
*/ |
*/ |
| 134 |
function magento_products_test() { |
function magento_products_test() { |
| 135 |
// Get list of only SIMPLE products and synchonize it |
// Get list of only SIMPLE products and synchonize it |
| 136 |
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
$StoreView = magento_stores_get_default_store_view(magento_stores_get_default_store(magento_stores_get_default_website())); |
| 137 |
$list = magento_api_catalog_product_list(array('type_id' => array('like'=>'simple')),$StoreView); |
$list = magento_api_catalog_product_list(array('type_id' => array('like'=>'simple')),$StoreView); |
| 138 |
if ($list) { |
if ($list) { |
| 139 |
$new_products = array(); |
$new_products = array(); |
| 140 |
// Synchronize every product |
// Synchronize every product |
| 141 |
//magento_product_synchronize($list[100]); die; |
//magento_product_synchronize($list[100]); die; |
| 142 |
foreach ($list as $product) { |
foreach ($list as $product) { |
| 143 |
magento_product_synchronize($product); |
magento_product_synchronize($product); |
| 144 |
$new_products[] = $product['entity_id']; |
$new_products[] = $product['entity_id']; |
| 145 |
} |
} |
| 146 |
// Get products that was deleted in Magento and delete it in Drupal too. |
// Get products that was deleted in Magento and delete it in Drupal too. |
| 147 |
$old_products = magento_products_get_product(); |
$old_products = magento_products_get_product(); |
| 148 |
$deleted_products = array_diff ($old_products, $new_products); |
$deleted_products = array_diff ($old_products, $new_products); |
| 149 |
foreach ($deleted_products as $item) { |
foreach ($deleted_products as $item) { |
| 150 |
$nid = magento_products_get_nid($item); |
$nid = magento_products_get_nid($item); |
| 151 |
node_delete($nid); |
node_delete($nid); |
| 152 |
} |
} |
| 153 |
}else drupal_set_message(t('There was some error with getting Product List from Magento'),'error'); |
}else drupal_set_message(t('There was some error with getting Product List from Magento'),'error'); |
| 154 |
} |
} |
| 155 |
/* Synchronize Magento product to Drupal node |
/* Synchronize Magento product to Drupal node |
| 156 |
* |
* |
| 158 |
* Array that contains information about product. |
* Array that contains information about product. |
| 159 |
*/ |
*/ |
| 160 |
function magento_product_synchronize($product){ |
function magento_product_synchronize($product){ |
| 161 |
$node = new stdClass(); |
$node = new stdClass(); |
| 162 |
$nid = magento_products_get_nid($product['entity_id']); |
$nid = magento_products_get_nid($product['entity_id']); |
| 163 |
// If this product is already in Drupal, get its ID |
// If this product is already in Drupal, get its ID |
| 164 |
if ($nid) { |
if ($nid) { |
| 165 |
$node->nid = $nid; |
$node->nid = $nid; |
| 166 |
$old_node = node_load(array('nid'=>$nid)); |
$old_node = node_load(array('nid'=>$nid)); |
| 167 |
$node->vid = $old_node->vid; |
$node->vid = $old_node->vid; |
| 168 |
} |
} |
| 169 |
$node->type = 'product'; |
|
| 170 |
$node->uid = 1; |
$node->type = 'product'; |
| 171 |
$node->status = 1; |
$node->uid = 1; |
| 172 |
$node->promote = 1; |
$node->status = 1; |
| 173 |
// Set product data to Node fields |
$node->promote = 1; |
| 174 |
$node->field_product_id[0]['value'] = $product['entity_id']; // product ID |
// Set product data to Node fields |
| 175 |
$node->title = $product['name']; // Name |
$node->field_product_id[0]['value'] = $product['entity_id']; // product ID |
| 176 |
$node->field_desc[0]['value'] = $product['description']; // Description |
$node->title = $product['name']; // Name |
| 177 |
$node->field_short_desc[0]['value'] = $product['short_description']; // Short Description |
$node->field_desc[0]['value'] = $product['description']; // Description |
| 178 |
|
$node->field_short_desc[0]['value'] = $product['short_description']; // Short Description |
| 179 |
$node->field_sku[0]['value'] = $product['sku']; // SKU |
|
| 180 |
$node->field_status[0]['value'] = $product['status']; // Status |
$node->field_sku[0]['value'] = $product['sku']; // SKU |
| 181 |
$node->field_weight[0]['value'] = $product['weight']; // Weight |
$node->field_status[0]['value'] = $product['status']; // Status |
| 182 |
$node->field_visibility[0]['value'] = $product['visibility']; // Visibility |
$node->field_weight[0]['value'] = $product['weight']; // Weight |
| 183 |
if ($product['news_from_date']) $node->field_new_from_date[0]['value'] = date(DATE_FORMAT,strtotime($product['news_from_date'])); // Set Product as New from Date |
$node->field_visibility[0]['value'] = $product['visibility']; // Visibility |
| 184 |
else $node->field_new_from_date[0]['value'] = NULL; |
if ($product['news_from_date']) $node->field_new_from_date[0]['value'] = date(DATE_FORMAT,strtotime($product['news_from_date'])); // Set Product as New from Date |
| 185 |
|
else $node->field_new_from_date[0]['value'] = NULL; |
| 186 |
if ($product['news_to_date']) $node->field_new_to_date[0]['value'] = date(DATE_FORMAT,strtotime($product['news_to_date'])); // Set Product as New to Date |
|
| 187 |
else $node->field_new_to_date[0]['value'] = NULL; |
if ($product['news_to_date']) $node->field_new_to_date[0]['value'] = date(DATE_FORMAT,strtotime($product['news_to_date'])); // Set Product as New to Date |
| 188 |
|
else $node->field_new_to_date[0]['value'] = NULL; |
| 189 |
$node->field_price[0]['value'] = $product['price']; // Price |
|
| 190 |
$node->field_special_price[0]['value'] = $product['special_price']; // Special Price |
$node->field_price[0]['value'] = $product['price']; // Price |
| 191 |
if ($product['special_from_date']) $node->field_special_price_from_date[0]['value'] = date(DATE_FORMAT,strtotime($product['special_from_date'])); // Special Price From Date |
$node->field_special_price[0]['value'] = $product['special_price']; // Special Price |
| 192 |
else $node->field_special_price_from_date[0]['value'] = NULL; |
if ($product['special_from_date']) $node->field_special_price_from_date[0]['value'] = date(DATE_FORMAT,strtotime($product['special_from_date'])); // Special Price From Date |
| 193 |
|
else $node->field_special_price_from_date[0]['value'] = NULL; |
| 194 |
if ($product['special_to_date']) $node->field_special_price_to_date[0]['value'] = date(DATE_FORMAT,strtotime($product['special_to_date'])); // Special Price To Date |
|
| 195 |
else $node->field_special_price_to_date[0]['value'] = NULL; |
if ($product['special_to_date']) $node->field_special_price_to_date[0]['value'] = date(DATE_FORMAT,strtotime($product['special_to_date'])); // Special Price To Date |
| 196 |
|
else $node->field_special_price_to_date[0]['value'] = NULL; |
| 197 |
$node->field_stock_availability[0]['value'] = $product['is_in_stock']; // Stock Availability |
|
| 198 |
|
$node->field_stock_availability[0]['value'] = $product['is_in_stock']; // Stock Availability |
| 199 |
// RULES ID and DISCOUNT PRICES |
|
| 200 |
if ($product['rules']) { |
// RULES ID and DISCOUNT PRICES |
| 201 |
$rules = array(); |
if ($product['rules']) { |
| 202 |
$prices = array(); |
$rules = array(); |
| 203 |
foreach ($product['rules'] as $item) { |
$prices = array(); |
| 204 |
if (!in_array($item['rule_id'],$rules)) { |
foreach ($product['rules'] as $item) { |
| 205 |
$node->field_rules_id[] = array('value' => $item['rule_id']); |
if (!in_array($item['rule_id'],$rules)) { |
| 206 |
$rules[] = $item['rule_id']; |
$node->field_rules_id[] = array('value' => $item['rule_id']); |
| 207 |
} |
$rules[] = $item['rule_id']; |
| 208 |
$prices[$item['customer_group_id']] = $item['rule_price']; |
} |
| 209 |
} |
$prices[$item['customer_group_id']] = $item['rule_price']; |
| 210 |
$node->field_discount_price[0]['value'] = serialize($prices); |
} |
| 211 |
}else { |
$node->field_discount_price[0]['value'] = serialize($prices); |
| 212 |
$node->field_discount_price[0]['value'] = NULL; |
}else { |
| 213 |
$node->field_rules_id = NULL; |
$node->field_discount_price[0]['value'] = NULL; |
| 214 |
} |
$node->field_rules_id = NULL; |
| 215 |
// Tier prices |
} |
| 216 |
if ($product['tier_price']) $node->field_tier_price[0]['value'] = serialize($product['tier_price']); |
// Tier prices |
| 217 |
else $node->field_tier_price[0]['value'] = NULL; |
if ($product['tier_price']) $node->field_tier_price[0]['value'] = serialize($product['tier_price']); |
| 218 |
// Product image |
else $node->field_tier_price[0]['value'] = NULL; |
| 219 |
if ($product['images']) { |
// Product image |
| 220 |
// Copy image to Drupal and set it to CCK imagefield |
if ($product['images']) { |
| 221 |
$image = magento_products_copy_image($product['images'][0]['url'],$product['images'][0]['file'],$nid); |
// Copy image to Drupal and set it to CCK imagefield |
| 222 |
$node->field_image[0] = array( |
$image = magento_products_copy_image($product['images'][0]['url'],$product['images'][0]['file'],$nid); |
| 223 |
|
$node->field_image[0] = array( |
| 224 |
'fid' => $image->fid, |
'fid' => $image->fid, |
| 225 |
'title' => $image->filename, |
'title' => $image->filename, |
| 226 |
'filename' => $image->filename, |
'filename' => $image->filename, |
| 229 |
'list' => 1, // always list |
'list' => 1, // always list |
| 230 |
'filemime' => $image->filemime, |
'filemime' => $image->filemime, |
| 231 |
'description' => $image->filename, |
'description' => $image->filename, |
| 232 |
); |
); |
| 233 |
} |
} |
| 234 |
node_save($node); |
|
| 235 |
if ($product['category_ids']) { |
// store non magento fields |
| 236 |
// Save category information to taxomomy |
foreach (get_object_vars($old_node) as $key=>$value) { |
| 237 |
$terms = array(); |
if (!property_exists($node, $key)) { |
| 238 |
$vocab = variable_get('magento_api_catalog_vocabulary_id', 0); |
$node->{$key} = $value; |
| 239 |
foreach($product['category_ids'] as $category) { |
} |
| 240 |
$term = magento_api_get_taxonomy_term_by_catalog_id($category); |
} |
| 241 |
if ($term) $terms[$vocab][$term] = $term; |
|
| 242 |
} |
node_save($node); |
| 243 |
taxonomy_node_save($node, $terms); |
if ($product['category_ids']) { |
| 244 |
} |
// Save category information to taxomomy |
| 245 |
return $result; |
$terms = array(); |
| 246 |
|
$vocab = variable_get('magento_api_catalog_vocabulary_id', 0); |
| 247 |
|
foreach($product['category_ids'] as $category) { |
| 248 |
|
$term = magento_api_get_taxonomy_term_by_catalog_id($category); |
| 249 |
|
if ($term) $terms[$vocab][$term] = $term; |
| 250 |
|
} |
| 251 |
|
taxonomy_node_save($node, $terms); |
| 252 |
|
} |
| 253 |
|
return $result; |
| 254 |
} |
} |
| 255 |
/* Get node ID by product ID |
/* Get node ID by product ID |
| 256 |
* |
* |
| 257 |
* @param $product_id |
* @param $product_id |
| 258 |
* Product ID. |
* Product ID. |
| 259 |
* |
* |
| 260 |
* @return |
* @return |
| 261 |
* Node ID. |
* Node ID. |
| 262 |
*/ |
*/ |
| 263 |
function magento_products_get_nid($product_id) { |
function magento_products_get_nid($product_id) { |
| 264 |
$sql = db_fetch_array(db_query('SELECT nid FROM {content_type_product} WHERE field_product_id_value = %d',$product_id)); |
$sql = db_fetch_array(db_query('SELECT nid FROM {content_type_product} WHERE field_product_id_value = %d',$product_id)); |
| 265 |
return $sql['nid']; |
return $sql['nid']; |
| 266 |
} |
} |
| 267 |
/* Get product list from Drupal nodes |
/* Get product list from Drupal nodes |
| 268 |
* |
* |
| 269 |
* @return |
* @return |
| 270 |
* Array that contains product ID from nodes. |
* Array that contains product ID from nodes. |
| 271 |
*/ |
*/ |
| 272 |
function magento_products_get_product() { |
function magento_products_get_product() { |
| 273 |
$old_products = array(); |
$old_products = array(); |
| 274 |
$query = db_query('SELECT field_product_id_value FROM {content_type_product} ORDER BY field_product_id_value ASC'); |
$query = db_query('SELECT field_product_id_value FROM {content_type_product} ORDER BY field_product_id_value ASC'); |
| 275 |
while ($sql = db_fetch_array($query)) { |
while ($sql = db_fetch_array($query)) { |
| 276 |
array_push($old_products,$sql['field_product_id_value']); |
array_push($old_products,$sql['field_product_id_value']); |
| 277 |
} |
} |
| 278 |
return $old_products; |
return $old_products; |
| 279 |
} |
} |
| 280 |
/* Copy image from outside URL to Drupal path |
/* Copy image from outside URL to Drupal path |
| 281 |
* |
* |
| 284 |
* @param $new_url |
* @param $new_url |
| 285 |
* File path |
* File path |
| 286 |
* @param $nid |
* @param $nid |
| 287 |
* Node id (if existed) |
* Node id (if existed) |
| 288 |
* |
* |
| 289 |
* @return |
* @return |
| 290 |
* Image object. |
* Image object. |
| 291 |
*/ |
*/ |
| 292 |
function magento_products_copy_image($url, $new_url, $nid = NULL) { |
function magento_products_copy_image($url, $new_url, $nid = NULL) { |
| 293 |
// Copy image from outside URL to Drupal |
// Copy image from outside URL to Drupal |
| 294 |
$field = content_fields('field_image', 'product'); |
$field = content_fields('field_image', 'product'); |
| 295 |
$files_path = filefield_widget_file_path($field); |
$files_path = filefield_widget_file_path($field); |
| 296 |
|
|
| 297 |
// Create directory, if not exists |
// Create directory, if not exists |
| 298 |
$dir = $files_path . dirname($new_url); |
$dir = $files_path . dirname($new_url); |
| 299 |
field_file_check_directory($dir, FILE_CREATE_DIRECTORY); |
field_file_check_directory($dir, FILE_CREATE_DIRECTORY); |
| 300 |
|
|
| 301 |
$image = new stdClass(); |
$image = new stdClass(); |
| 302 |
$name = $files_path . $new_url; |
$name = $files_path . $new_url; |
| 303 |
// Check if this file is already in Drupal |
// Check if this file is already in Drupal |
| 304 |
$sql = db_fetch_array(db_query('SELECT * FROM {files} WHERE filepath = "%s" AND filesize = %d', $name, getSizeFile($url))); |
$sql = db_fetch_array(db_query('SELECT * FROM {files} WHERE filepath = "%s" AND filesize = %d', $name, getSizeFile($url))); |
| 305 |
if ($sql) { |
if ($sql) { |
| 306 |
// Check if this file was deleted from file system by some reason |
// Check if this file was deleted from file system by some reason |
| 307 |
// and re-copy it |
// and re-copy it |
| 308 |
if (is_file($name) == FALSE) { |
if (is_file($name) == FALSE) { |
| 309 |
$file_temp = file_get_contents($url); |
$file_temp = file_get_contents($url); |
| 310 |
$file_temp2 = file_save_data($file_temp, $name , FILE_EXISTS_REPLACE); |
$file_temp2 = file_save_data($file_temp, $name , FILE_EXISTS_REPLACE); |
| 311 |
} |
} |
| 312 |
$image->filename = $sql['filename']; |
$image->filename = $sql['filename']; |
| 313 |
$image->filepath = $sql['filepath']; |
$image->filepath = $sql['filepath']; |
| 314 |
$image->uid = $sql['uid']; |
$image->uid = $sql['uid']; |
| 315 |
$image->fid = $sql['fid']; |
$image->fid = $sql['fid']; |
| 316 |
$image->filemime = $sql['filemime']; |
$image->filemime = $sql['filemime']; |
| 317 |
$image->filesize = $sql['filesize']; |
$image->filesize = $sql['filesize']; |
| 318 |
$image->status = $sql['status']; |
$image->status = $sql['status']; |
| 319 |
$image->timestamp = $sql['timestamp']; |
$image->timestamp = $sql['timestamp']; |
| 320 |
return $image; |
return $image; |
| 321 |
}else { |
}else { |
| 322 |
//Delete OLD Images |
//Delete OLD Images |
| 323 |
$result = db_fetch_array(db_query('SELECT f.filepath FROM {content_type_product} AS ctp INNER JOIN {files} AS f ON f.fid = ctp.field_image_fid WHERE nid=%d',$nid)); |
if ($nid) { |
| 324 |
if ($result) { |
$node = node_load($nid); |
| 325 |
db_query('DELETE FROM {files} WHERE filepath = "%s"', $result['filepath']); |
$result = filefield_get_node_files($node, 'field_image'); |
| 326 |
file_delete($result['filepath']); |
if (!empty($result)) { |
| 327 |
} |
foreach ($result as $image_file) { |
| 328 |
// Save file to file system and Drupal |
db_query('DELETE FROM {files} WHERE filepath = "%s"', $image_file['filepath']); |
| 329 |
$file_temp = file_get_contents($url); |
file_delete($image_file['filepath']); |
| 330 |
$file_temp2 = file_save_data($file_temp, $name , FILE_EXISTS_REPLACE); |
} |
| 331 |
|
} |
| 332 |
$image->filename = basename($file_temp2); |
// Save file to file system and Drupal |
| 333 |
$image->filepath = $file_temp2; |
$file_temp = file_get_contents($url); |
| 334 |
$image->uid = 1; |
$file_temp2 = file_save_data($file_temp, $name , FILE_EXISTS_REPLACE); |
| 335 |
$image->filemime = file_get_mimetype($file_temp2); |
|
| 336 |
$image->filesize = filesize($file_temp2); |
$image->filename = basename($file_temp2); |
| 337 |
$image->status = FILE_STATUS_TEMPORARY; |
$image->filepath = $file_temp2; |
| 338 |
$image->timestamp = time(); |
$image->uid = 1; |
| 339 |
|
$image->filemime = file_get_mimetype($file_temp2); |
| 340 |
drupal_write_record('files', $image); |
$image->filesize = filesize($file_temp2); |
| 341 |
file_set_status($image,1); |
$image->status = FILE_STATUS_TEMPORARY; |
| 342 |
} |
$image->timestamp = time(); |
| 343 |
return $image; |
|
| 344 |
|
drupal_write_record('files', $image); |
| 345 |
|
file_set_status($image,1); |
| 346 |
|
} |
| 347 |
|
} |
| 348 |
|
return $image; |
| 349 |
} |
} |
| 350 |
/* |
/* |
| 351 |
* Get filesize by outside URL |
* Get filesize by outside URL |
| 352 |
* |
* |
| 353 |
* @return |
* @return |
| 354 |
* filesize |
* filesize |
| 355 |
*/ |
*/ |
| 356 |
function getSizeFile($url) { |
function getSizeFile($url) { |
| 357 |
if (substr($url, 0, 4) == 'http') { |
if (substr($url, 0, 4) == 'http') { |
| 358 |
$filesize = array_change_key_case(get_headers($url, 1),CASE_LOWER); |
$filesize = array_change_key_case(get_headers($url, 1),CASE_LOWER); |
| 359 |
if ( strcasecmp($filesize[0], 'HTTP/1.1 200 OK') != 0 ) { |
if ( strcasecmp($filesize[0], 'HTTP/1.1 200 OK') != 0 ) { |
| 360 |
$filesize = $filesize['content-length'][1]; |
$filesize = $filesize['content-length'][1]; |
| 361 |
} else { |
} else { |
| 362 |
$filesize = $filesize['content-length']; |
$filesize = $filesize['content-length']; |
|
} |
|
|
} else { |
|
|
$filesize = @filesize($url); |
|
| 363 |
} |
} |
| 364 |
return $filesize; |
} else { |
| 365 |
|
$filesize = @filesize($url); |
| 366 |
|
} |
| 367 |
|
return $filesize; |
| 368 |
} |
} |
| 369 |
/* |
/* |
| 370 |
* Get product actual price |
* Get product actual price |
| 372 |
* Product node |
* Product node |
| 373 |
* @param $user |
* @param $user |
| 374 |
* Current User |
* Current User |
| 375 |
* |
* |
| 376 |
* @return $price |
* @return $price |
| 377 |
* Actual product price |
* Actual product price |
| 378 |
*/ |
*/ |
| 379 |
function magento_products_get_actual_price($node, $user){ |
function magento_products_get_actual_price($node, $user){ |
| 380 |
// Get customer group of current user |
// Get customer group of current user |
| 381 |
$group_id = magento_users_get_user_group($user); |
$group_id = magento_users_get_user_group($user); |
| 382 |
//return $group_id; break; |
//return $group_id; break; |
| 383 |
$price = $node->field_price[0]['value']; |
$price = $node->field_price[0]['value']; |
| 384 |
$time = time(); |
$time = time(); |
| 385 |
// Get special price and check if it is actual and less than original price |
// Get special price and check if it is actual and less than original price |
| 386 |
$special_price = $node->field_special_price[0]['value']; |
$special_price = $node->field_special_price[0]['value']; |
| 387 |
if ($special_price) { |
if ($special_price) { |
| 388 |
if ($special_price < $price) { |
if ($special_price < $price) { |
| 389 |
if (empty($node->field_special_price_from_date[0]['value'])) { |
if (empty($node->field_special_price_from_date[0]['value'])) { |
| 390 |
if ($time <= strtotime($node->field_special_price_to_date[0]['value'])) $price = $special_price; |
if ($time <= strtotime($node->field_special_price_to_date[0]['value'])) $price = $special_price; |
| 391 |
}elseif (empty($node->field_special_price_to_date[0]['value'])) { |
}elseif (empty($node->field_special_price_to_date[0]['value'])) { |
| 392 |
if ($time >= strtotime($node->field_special_price_from_date[0]['value'])) $price = $special_price; |
if ($time >= strtotime($node->field_special_price_from_date[0]['value'])) $price = $special_price; |
| 393 |
} else { |
} else { |
| 394 |
if ($time >= strtotime($node->field_special_price_from_date[0]['value']) && $time <= strtotime($node->field_special_price_to_date[0]['value'])) $price = $special_price; |
if ($time >= strtotime($node->field_special_price_from_date[0]['value']) && $time <= strtotime($node->field_special_price_to_date[0]['value'])) $price = $special_price; |
| 395 |
} |
} |
| 396 |
} |
} |
| 397 |
} |
} |
| 398 |
// Get discount price and check if it is actual for current customer group and less than original price |
// Get discount price and check if it is actual for current customer group and less than original price |
| 399 |
$discount_price = unserialize($node->field_discount_price[0]['value']); |
$discount_price = unserialize($node->field_discount_price[0]['value']); |
| 400 |
if ($discount_price) { |
if ($discount_price) { |
| 401 |
if ($discount_price[$group_id] < $price) $price = $discount_price[$group_id]; |
if ($discount_price[$group_id] < $price) $price = $discount_price[$group_id]; |
| 402 |
} |
} |
| 403 |
return floatval($price); |
return floatval($price); |
| 404 |
} |
} |
| 405 |
/* |
/* |
| 406 |
* Get information about Tier Prices |
* Get information about Tier Prices |
| 408 |
* Product node |
* Product node |
| 409 |
* @param $price |
* @param $price |
| 410 |
* Actual product price |
* Actual product price |
| 411 |
* |
* |
| 412 |
* @return $product_tier |
* @return $product_tier |
| 413 |
* Array that contains information about tier price and qty |
* Array that contains information about tier price and qty |
| 414 |
*/ |
*/ |
| 415 |
function magento_products_get_tier_price($node, $price) { |
function magento_products_get_tier_price($node, $price) { |
| 416 |
global $user; |
global $user; |
| 417 |
$product_tier = array(); |
$product_tier = array(); |
| 418 |
$tier_price = unserialize($node->field_tier_price[0]['value']); |
$tier_price = unserialize($node->field_tier_price[0]['value']); |
| 419 |
if ($tier_price) { |
if ($tier_price) { |
| 420 |
foreach ($tier_price as $tier) { |
foreach ($tier_price as $tier) { |
| 421 |
// Check if tier price less that current product price |
// Check if tier price less that current product price |
| 422 |
if ($tier['price'] <= $price) { |
if ($tier['price'] <= $price) { |
| 423 |
if ($tier['all_groups']) $product_tier[intval($tier['price_qty'])] = floatval($tier['price']); |
if ($tier['all_groups']) $product_tier[intval($tier['price_qty'])] = floatval($tier['price']); |
| 424 |
elseif($tier['cust_group'] == magento_users_get_user_group($user)) $product_tier[intval($tier['price_qty'])] = floatval($tier['price']); |
elseif($tier['cust_group'] == magento_users_get_user_group($user)) $product_tier[intval($tier['price_qty'])] = floatval($tier['price']); |
| 425 |
} |
} |
| 426 |
} |
} |
| 427 |
} |
} |
| 428 |
return $product_tier; |
return $product_tier; |
| 429 |
} |
} |