PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/wpsc_query.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1601 lines | 962 code | 240 blank | 399 comment | 276 complexity | 125b44286227ab11f5c45fddfbdb4ef3 MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce query class and product display functions
  4. *
  5. * This is the wpsc equivalent of the wp query class, needed for the wpsc templates to work
  6. * The Cart class handles adding, removing and adjusting items in the cart, and totaling up the cost of the items in the cart.
  7. * The Cart Items class handles the same, but for cart items themselves.
  8. *
  9. * This code contains modified methods from the wp_query object of WordPress, located in wp-includes/query.php, these parts are to do with the loops we use that mirror the functionality of the wordpress loop
  10. * As such, they can be used in the same way, if needed.
  11. *
  12. * @package wp-e-commerce
  13. * @since 3.7
  14. * @subpackage wpsc-cart-classes
  15. */
  16. /**
  17. * wpsc display categories function
  18. * Used to determine whether to display products on the page
  19. * @return boolean - true for yes, false for no
  20. */
  21. function wpsc_display_categories() {
  22. global $wp_query;
  23. $output = false;
  24. if(!is_numeric(get_option('wpsc_default_category'))) {
  25. if(is_numeric($wp_query->query_vars['category_id'])) {
  26. $category_id = $wp_query->query_vars['category_id'];
  27. } else if(is_numeric($_GET['category'])) {
  28. $category_id = $_GET['category'];
  29. }
  30. // if we have no categories, and no search, show the group list
  31. //exit('product id '.$product_id.' catid '.$category_id );
  32. if(is_numeric(get_option('wpsc_default_category')) || (is_numeric($product_id)) || ($_GET['product_search'] != '')) {
  33. $output = true;
  34. }
  35. if((get_option('wpsc_default_category') == 'all+list')|| (get_option('wpsc_default_category') == 'list')){
  36. $output = true;
  37. }
  38. }
  39. if($category_id > 0) {
  40. $output = false;
  41. }
  42. return $output;
  43. }
  44. /**
  45. * wpsc display products function
  46. * Used to determine whether to display products on the page
  47. * @return boolean - true for yes, false for no
  48. */
  49. function wpsc_display_products() {
  50. global $wpsc_query;
  51. //we have to display something, if we are not displaying categories, then we must display products
  52. $output = true;
  53. if(wpsc_display_categories() && ($wpsc_query->query_vars['custom_query'] == false)) {
  54. if(get_option('wpsc_default_category') == 'list') {
  55. $output = false;
  56. }
  57. if(isset($_GET['range']) || isset($_GET['category'])){
  58. $output = true;
  59. }
  60. }
  61. return $output;
  62. }
  63. /**
  64. * this page url function, returns the URL of this page
  65. * @return string - the URL of the current page
  66. */
  67. function wpsc_this_page_url() {
  68. global $wpsc_query;
  69. //echo "<pr".print_r($wpsc_query->category,true)."</pre>";
  70. if($wpsc_query->is_single === true) {
  71. return wpsc_product_url($wpsc_query->product['id']);
  72. } else {
  73. $output = wpsc_category_url($wpsc_query->category);
  74. if($wpsc_query->query_vars['page'] > 1) {
  75. //
  76. if(get_option('permalink_structure')) {
  77. $output .= "page/{$wpsc_query->query_vars['page']}/";
  78. } else {
  79. $output = add_query_arg('page_number', $wpsc_query->query_vars['page'], $output);
  80. }
  81. }
  82. return $output;
  83. }
  84. }
  85. /**
  86. * is single product function, determines if we are viewing a single product
  87. * @return boolean - true, or false...
  88. */
  89. function wpsc_is_single_product() {
  90. global $wpsc_query;
  91. if($wpsc_query->is_single === true) {
  92. $state = true;
  93. } else {
  94. $state = false;
  95. }
  96. return $state;
  97. }
  98. /**
  99. * category class function, categories can have a specific class, this gets that
  100. * @return string - the class of the selected category
  101. */
  102. function wpsc_category_class() {
  103. global $wpdb, $wp_query;
  104. $category_nice_name = '';
  105. if($wp_query->query_vars['product_category'] != null) {
  106. $catid = $wp_query->query_vars['product_category'];
  107. } else if(is_numeric($_GET['category'])) {
  108. $catid = $_GET['category'];
  109. } else if(is_numeric($GLOBALS['wpsc_category_id'])) {
  110. $catid = $GLOBALS['wpsc_category_id'];
  111. } else {
  112. $catid = get_option('wpsc_default_category');
  113. if($catid == 'all+list') {
  114. $catid = 'all';
  115. }
  116. }
  117. if((int)$catid > 0) {
  118. $category_nice_name = $wpdb->get_var("SELECT `nice-name` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `id` ='".(int)$catid."' LIMIT 1");
  119. } else if($catid == 'all') {
  120. $category_nice_name = 'all-categories';
  121. }
  122. //exit("<pre>".print_r(get_option('wpsc_default_category'),true)."</pre>");
  123. return $category_nice_name;
  124. }
  125. /**
  126. * category transition function, finds the transition between categories
  127. * @return string - the class of the selected category
  128. */
  129. function wpsc_current_category_name() {
  130. global $wpsc_query;
  131. return $wpsc_query->product['category'];
  132. }
  133. /**
  134. * category transition function, finds the transition between categories
  135. * @return string - the class of the selected category
  136. */
  137. function wpsc_category_transition() {
  138. global $wpdb, $wp_query, $wpsc_query;
  139. $current_product_index = (int)$wpsc_query->current_product;
  140. $previous_product_index = ((int)$wpsc_query->current_product - 1);
  141. if($previous_product_index >= 0) {
  142. $previous_category_id = $wpsc_query->products[$previous_product_index]['category_id'];
  143. } else {
  144. $previous_category_id = 0;
  145. }
  146. $current_category_id = $wpsc_query->product['category_id'];
  147. if($current_category_id != $previous_category_id) {
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. /**
  154. * wpsc have products function, the product loop
  155. * @return boolean true while we have products, otherwise, false
  156. */
  157. function wpsc_have_products() {
  158. global $wpsc_query;
  159. return $wpsc_query->have_products();
  160. }
  161. /**
  162. * wpsc the product function, gets the next product,
  163. * @return nothing
  164. */
  165. function wpsc_the_product() {
  166. global $wpsc_query;
  167. $wpsc_query->the_product();
  168. }
  169. /**
  170. * wpsc in the loop function,
  171. * @return boolean - true if we are in the loop
  172. */
  173. function wpsc_in_the_loop() {
  174. global $wpsc_query;
  175. return $wpsc_query->in_the_loop;
  176. }
  177. /**
  178. * wpsc rewind products function, rewinds back to the first product
  179. * @return nothing
  180. */
  181. function wpsc_rewind_products() {
  182. global $wpsc_query;
  183. return $wpsc_query->rewind_products();
  184. }
  185. /**
  186. * wpsc the product id function,
  187. * @return integer - the product ID
  188. */
  189. function wpsc_the_product_id() {
  190. global $wpsc_query;
  191. return $wpsc_query->product['id'];
  192. }
  193. /**
  194. * wpsc edit the product link function
  195. * @return string - a link to edit this product
  196. */
  197. function wpsc_edit_the_product_link( $link = null, $before = '', $after = '', $id = 0 ) {
  198. global $wpsc_query, $current_user, $table_prefix;
  199. if ( $link == null ) {
  200. $link = __('Edit');
  201. }
  202. $product_id = $wpsc_query->product['id'];
  203. if ( $id > 0 ) {
  204. $product_id = $id;
  205. }
  206. $siteurl = get_option('siteurl');
  207. get_currentuserinfo();
  208. $output = '';
  209. if($current_user->{$table_prefix . 'capabilities'}['administrator'] == 1) {
  210. $output = $before . "<a class='wpsc_edit_product' href='{$siteurl}/wp-admin/admin.php?page=wpsc-edit-products&amp;product_id={$product_id}'>" . $link . "</a>" . $after;
  211. }
  212. return $output;
  213. }
  214. /**
  215. * wpsc the product title function
  216. * @return string - the product title
  217. */
  218. function wpsc_the_product_title() {
  219. global $wpsc_query;
  220. //return stripslashes($wpsc_query->the_product_title());
  221. return htmlentities(stripslashes($wpsc_query->the_product_title()), ENT_QUOTES, "UTF-8");
  222. }
  223. /**
  224. * wpsc product description function
  225. * @return string - the product description
  226. */
  227. function wpsc_the_product_description() {
  228. global $wpsc_query, $allowedtags;
  229. $description_allowed_tags = $allowedtags + array(
  230. 'img' => array(
  231. 'src' => array(),'width' => array(),'height' => array(),
  232. ),
  233. 'span' => array(
  234. 'style' => array()
  235. ),
  236. 'ul' => array(),
  237. 'li' => array(),
  238. 'table' => array(),
  239. 'tr'=>array(
  240. 'class'=>array(),
  241. ),
  242. 'th' => array(
  243. 'class'=>array(),
  244. ),
  245. 'td' => array(
  246. 'class'=>array(),
  247. ),
  248. );
  249. return wpautop(wptexturize( wp_kses(stripslashes($wpsc_query->product['description']), $description_allowed_tags )));
  250. }
  251. /**
  252. * wpsc additional product description function
  253. * TODO make this work with the tabbed multiple product descriptions, may require another loop
  254. * @return string - the additional description
  255. */
  256. function wpsc_the_product_additional_description() {
  257. global $wpsc_query;
  258. return $wpsc_query->product['additional_description'];
  259. }
  260. /**
  261. * wpsc product permalink function
  262. * @return string - the URL to the single product page for this product
  263. */
  264. function wpsc_the_product_permalink( $category_id = null ) {
  265. global $wpsc_query;
  266. if ( !isset( $category_id ) || !absint( $category_id ) ) {
  267. $category_id = $wpsc_query->category;
  268. } else {
  269. $category_id = absint( $category_id );
  270. }
  271. return wpsc_product_url( $wpsc_query->product['id'], $category_id );
  272. }
  273. /**
  274. * wpsc product price function
  275. * @return string - the product price
  276. */
  277. function wpsc_the_product_price($no_decimals = false) {
  278. global $wpsc_query;
  279. $price = calculate_product_price($wpsc_query->product['id'], $wpsc_query->first_variations);
  280. if(($wpsc_query->product['special_price'] > 0) && (($wpsc_query->product['price'] - $wpsc_query->product['special_price'] ) >= 0) && ($variations_output[1] === null)) {
  281. $output = nzshpcrt_currency_display($price, $wpsc_query->product['notax'],true, $wpsc_query->product['id']);
  282. } else {
  283. $output = nzshpcrt_currency_display($price, $wpsc_query->product['notax'], true);
  284. }
  285. if($no_decimals == true) {
  286. $output = array_shift(explode(".", $output));
  287. }
  288. // echo 'NO DECIMALS VALUE:'.$no_decimals;
  289. //echo "<pre>".print_r($wpsc_query->product,true)."</pre>";
  290. return $output;
  291. }
  292. /**
  293. * wpsc external link function
  294. * @return string - the product price
  295. */
  296. function wpsc_product_external_link($id){
  297. global $wpdb, $wpsc_query;
  298. $id = absint($id);
  299. $externalLink = $wpdb->get_var("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id`='{$id}' AND `meta_key`='external_link' LIMIT 1");
  300. return $externalLink;
  301. }
  302. /**
  303. * wpsc product sku function
  304. * @return string - the product price
  305. */
  306. function wpsc_product_sku($id){
  307. global $wpdb;
  308. $id = absint($id);
  309. $sku = $wpdb->get_var("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id`='{$id}' AND `meta_key`='sku' LIMIT 1");
  310. return $sku;
  311. }
  312. /**
  313. * wpsc product creation time function
  314. * @return string - the product price
  315. */
  316. function wpsc_product_creation_time($format = null) {
  317. global $wpsc_query;
  318. if($format == null) {
  319. $format = "Y-m-d H:i:s";
  320. }
  321. return mysql2date($format, $wpsc_query->product['date_added']);
  322. }
  323. /**
  324. * wpsc product has stock function
  325. * TODO this may need modifying to work with variations, test this
  326. * @return boolean - true if the product has stock or does not use stock, false if it does not
  327. */
  328. function wpsc_product_has_stock() {
  329. // Is the product in stock?
  330. global $wpsc_query;
  331. if((count($wpsc_query->first_variations) == 0) && ($wpsc_query->product['quantity_limited'] == 1) && ($wpsc_query->product['quantity'] < 1)) {
  332. return false;
  333. } else {
  334. return true;
  335. }
  336. }
  337. /**
  338. * wpsc product remaining stock function
  339. * @return integer - the amount of remaining stock, or null if product is stockless
  340. */
  341. function wpsc_product_remaining_stock() {
  342. // how much stock is left?
  343. global $wpsc_query;
  344. if((count($wpsc_query->first_variations) == 0) && ($wpsc_query->product['quantity_limited'] == 1) && ($wpsc_query->product['quantity'] > 0)) {
  345. return $wpsc_query->product['quantity'];
  346. } else {
  347. return null;
  348. }
  349. }
  350. /**
  351. * wpsc is donation function
  352. * @return boolean - true if it is a donation, otherwise false
  353. */
  354. function wpsc_product_is_donation() {
  355. // Is the product a donation?
  356. global $wpsc_query;
  357. if($wpsc_query->product['donation'] == 1) {
  358. return true;
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * wpsc product on special function
  365. * @return boolean - true if the product is on special, otherwise false
  366. */
  367. function wpsc_product_on_special() {
  368. // function to determine if the product is on special
  369. global $wpsc_query;
  370. //echo "<pre>".print_r($wpsc_query,true)."</pre>";
  371. if(($wpsc_query->product['special_price'] > 0) && (($wpsc_query->product['price'] - $wpsc_query->product['special_price']) >= 0) && (count($wpsc_query->first_variations) < 1)) {
  372. return true;
  373. } else {
  374. return false;
  375. }
  376. }
  377. /**
  378. * wpsc product has file function
  379. * @return boolean - true if the product has a file
  380. */
  381. function wpsc_product_has_file() {
  382. global $wpsc_query, $wpdb;
  383. if(is_numeric($wpsc_query->product['file']) && ($wpsc_query->product['file'] > 0)) {
  384. return true;
  385. }
  386. return false;
  387. }
  388. /**
  389. * wpsc product is modifiable function
  390. * @return boolean - true if the product has a file
  391. */
  392. function wpsc_product_is_customisable() {
  393. global $wpsc_query, $wpdb;
  394. $engraved_text = get_product_meta($wpsc_query->product['id'], 'engraved');
  395. $can_have_uploaded_image = get_product_meta($wpsc_query->product['id'], 'can_have_uploaded_image');
  396. if(($engraved_text == 'on') || ($can_have_uploaded_image == 'on')) {
  397. return true;
  398. }
  399. return false;
  400. }
  401. /**
  402. * wpsc product has personal text function
  403. * @return boolean - true if the product has a file
  404. */
  405. function wpsc_product_has_personal_text() {
  406. global $wpsc_query, $wpdb;
  407. $engraved_text = get_product_meta($wpsc_query->product['id'], 'engraved');
  408. if($engraved_text == 'on') {
  409. return true;
  410. }
  411. return false;
  412. }
  413. /**
  414. * wpsc product has personal file function
  415. * @return boolean - true if the product has a file
  416. */
  417. function wpsc_product_has_supplied_file() {
  418. global $wpsc_query, $wpdb;
  419. $can_have_uploaded_image = get_product_meta($wpsc_query->product['id'], 'can_have_uploaded_image');
  420. if($can_have_uploaded_image == 'on') {
  421. return true;
  422. }
  423. return false;
  424. }
  425. /**
  426. * wpsc product postage and packaging function
  427. * @return string - currently only valid for flat rate
  428. */
  429. function wpsc_product_postage_and_packaging() {
  430. global $wpsc_query;
  431. return nzshpcrt_currency_display($wpsc_query->product['pnp'], 1, true);
  432. }
  433. /**
  434. * wpsc normal product price function
  435. * TODO determine why this function is here
  436. * @return string - returns some form of product price
  437. */
  438. function wpsc_product_normal_price() {
  439. global $wpsc_query;
  440. $price = calculate_product_price($wpsc_query->product['id'], $wpsc_query->first_variations, true);
  441. if(($wpsc_query->product['special_price'] > 0) && (($wpsc_query->product['price'] - $wpsc_query->product['special_price']) >= 0) && ($variations_output[1] === null)) {
  442. $output = nzshpcrt_currency_display($price, $wpsc_query->product['notax'],true,$wpsc_query->product['id']);
  443. } else {
  444. $output = nzshpcrt_currency_display($price, $wpsc_query->product['notax'], true);
  445. }
  446. $output = apply_filters('wpsc_price_display_changer', $output);
  447. return $output;
  448. }
  449. /**
  450. * wpsc product image function
  451. * if no parameters are passed, the image is not resized, otherwise it is resized to the specified dimensions
  452. * @param integer width
  453. * @param integer height
  454. * @return string - the product image URL, or the URL of the resized version
  455. */
  456. function wpsc_the_product_image($width = null, $height = null) {
  457. // show the full sized image for the product, if supplied with dimensions, will resize image to those.
  458. global $wpsc_query, $wpdb;
  459. $image_file_name = null;
  460. if($wpsc_query->product['image_file'] != null) {
  461. $image_file_name = $wpsc_query->product['image_file'];
  462. } else if ($wpsc_query->product['image'] != null) {
  463. if(is_numeric($wpsc_query->product['image'])){
  464. $image_file_name = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` WHERE `id`= '".$wpsc_query->product['image']."' LIMIT 1");
  465. }else{
  466. $image_file_name = $wpsc_query->product['image'];
  467. }
  468. $wpsc_query->product['image_file'] = $wpsc_query->product['image'];
  469. }
  470. if ($wpsc_query->product['thumbnail_state'] == 3) {
  471. $image_path = WPSC_THUMBNAIL_DIR . $image_file_name;
  472. } else {
  473. $image_path = WPSC_IMAGE_DIR . $image_file_name;
  474. }
  475. $image_file_name_parts = explode(".",$image_file_name);
  476. $extension = array_pop($image_file_name_parts);
  477. if($image_file_name != null) {
  478. if(($width > 0) && ($height > 0) && ($width <= 1024) && ($height <= 1024)) {
  479. $cache_filename = basename("product_img_{$wpsc_query->product['image']}_{$height}x{$width}");
  480. if(file_exists(WPSC_CACHE_DIR.$cache_filename.'.'.$extension)) {
  481. $original_modification_time = filemtime($image_path);
  482. $cache_modification_time = filemtime(WPSC_CACHE_DIR.$cache_filename.'.'.$extension);
  483. if($original_modification_time < $cache_modification_time) {
  484. $use_cache = true;
  485. }
  486. }
  487. if($use_cache == true) {
  488. $cache_url = WPSC_CACHE_URL;
  489. if(is_ssl()) {
  490. $cache_url = str_replace("http://", "https://", $cache_url);
  491. }
  492. $image_url = $cache_url.$cache_filename.'.'.$extension;
  493. } else {
  494. $image_url = "index.php?image_id=".$wpsc_query->product['image']."&amp;width=".$width."&amp;height=".$height;
  495. }
  496. return $image_url;
  497. } else {
  498. if ($wpsc_query->product['thumbnail_state'] == 3) {
  499. $image_url = WPSC_THUMBNAIL_URL.$image_file_name;
  500. } else {
  501. $image_url = WPSC_IMAGE_URL.$image_file_name;
  502. }
  503. if(is_ssl()) {
  504. $image_url = str_replace("http://", "https://", $image_url);
  505. }
  506. return $image_url;
  507. }
  508. } else {
  509. return false;
  510. }
  511. }
  512. /**
  513. * wpsc product thumbnail function
  514. * @return string - the URL to the thumbnail image
  515. */
  516. function wpsc_the_product_thumbnail() {
  517. // show the thumbnail image for the product
  518. global $wpsc_query;
  519. return $wpsc_query->the_product_thumbnail();
  520. }
  521. /**
  522. * wpsc product comment link function
  523. * @return string - javascript required to make the intense debate link work
  524. */
  525. function wpsc_product_comment_link() {
  526. // add the product comment link
  527. global $wpsc_query;
  528. if (get_option('wpsc_enable_comments') == 1) {
  529. $enable_for_product = get_product_meta($wpsc_query->product['id'], 'enable_comments');
  530. if ((get_option('wpsc_comments_which_products') == 1 && $enable_for_product == '') || $enable_for_product == 'yes') {
  531. $original = array("&","'",":","/","@","?","=");
  532. $entities = array("%26","%27","%3A","%2F","%40","%3F","%3D");
  533. $output = "<div class=\"clear comments\">
  534. <script src='http://www.intensedebate.com/js/getCommentLink.php?acct=".get_option("wpsc_intense_debate_account_id")."&postid=product_".$wpsc_query->product['id']."&posttitle=".urlencode($wpsc_query->product['name'])."&posturl=".str_replace($original, $entities, wpsc_product_url($wpsc_query->product['id'], null, false))."&posttime=".urlencode(date('Y-m-d h:i:s', time()))."&postauthor=author_".$wpsc_query->product['id']."' type='text/javascript' defer='defer'></script>
  535. </div>";
  536. }
  537. }
  538. return $output;
  539. }
  540. /**
  541. * wpsc product comments function
  542. * @return string - javascript for the intensedebate comments
  543. */
  544. function wpsc_product_comments() {
  545. global $wpsc_query;
  546. // add the product comments
  547. if (get_option('wpsc_enable_comments') == 1) {
  548. $enable_for_product = get_product_meta($wpsc_query->product['id'], 'enable_comments');
  549. if ((get_option('wpsc_comments_which_products') == 1 && $enable_for_product == '') || $enable_for_product == 'yes') {
  550. $output = "<script>
  551. var idcomments_acct = '".get_option('wpsc_intense_debate_account_id')."';
  552. var idcomments_post_id = 'product_".$wpsc_query->product['id']."';
  553. var idcomments_post_url = encodeURIComponent('".wpsc_product_url($wpsc_query->product['id'], null, false)."');
  554. </script>
  555. <span id=\"IDCommentsPostTitle\" style=\"display:none\"></span>
  556. <script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>
  557. ";
  558. }
  559. }
  560. return $output;
  561. }
  562. /**
  563. * wpsc have custom meta function
  564. * @return boolean - true while we have custom meta to display
  565. */
  566. function wpsc_have_custom_meta() {
  567. global $wpsc_query;
  568. return $wpsc_query->have_custom_meta();
  569. }
  570. /**
  571. * wpsc the custom meta function
  572. * @return nothing - iterate through the custom meta vallues
  573. */
  574. function wpsc_the_custom_meta() {
  575. global $wpsc_query;
  576. $wpsc_query->the_custom_meta();
  577. }
  578. /**
  579. * wpsc have variation groups function
  580. * @return boolean - true while we have variation groups
  581. */
  582. function wpsc_have_variation_groups() {
  583. global $wpsc_query;
  584. return $wpsc_query->have_variation_groups();
  585. }
  586. /**
  587. * wpsc the variation group function
  588. * @return nothing - iterate through the variation groups
  589. */
  590. function wpsc_the_variation_group() {
  591. global $wpsc_query;
  592. $wpsc_query->the_variation_group();
  593. }
  594. /**
  595. * wpsc have variations function
  596. * @return boolean - true while we have variations
  597. */
  598. function wpsc_have_variations() {
  599. global $wpsc_query;
  600. return $wpsc_query->have_variations();
  601. }
  602. /**
  603. * wpsc the variation function
  604. * @return nothing - iterate through the variations
  605. */
  606. function wpsc_the_variation() {
  607. global $wpsc_query;
  608. $wpsc_query->the_variation();
  609. }
  610. function wpsc_product_has_multicurrency(){
  611. global $wpdb, $wpsc_query;
  612. $sql = "SELECT `meta_key`, `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id`=".$wpsc_query->product['id']." AND `meta_key` LIKE 'currency%'";
  613. $results = $wpdb->get_results($sql, ARRAY_A);
  614. if(count($results) > 0){
  615. return true;
  616. }else{
  617. return false;
  618. }
  619. // exit('<pre>'.print_r($results, true).'</pre>');
  620. }
  621. function wpsc_display_product_multicurrency(){
  622. global $wpdb, $wpsc_query;
  623. $output = '';
  624. $sql = "SELECT `meta_key`, `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id`=".$wpsc_query->product['id']." AND `meta_key` LIKE 'currency%'";
  625. $results = $wpdb->get_results($sql, ARRAY_A);
  626. if(count($results) > 0){
  627. foreach((array)$results as $curr){
  628. $isocode = str_ireplace("currency[", "", $curr['meta_key']);
  629. $isocode = str_ireplace("]", "", $isocode);
  630. $currency_data = $wpdb->get_row("SELECT `symbol`,`symbol_html`,`code` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode`='".$isocode."' LIMIT 1",ARRAY_A) ;
  631. if($currency_data['symbol'] != '') {
  632. $currency_sign = $currency_data['symbol_html'];
  633. } else {
  634. $currency_sign = $currency_data['code'];
  635. }
  636. $output .='<span class="wpscsmall pricefloatright pricedisplay">'.$currency_sign.' '.nzshpcrt_currency_display($curr["meta_value"],false,false,false,true).'</span><br />';
  637. //exit('<pre>'.print_r($currency_sign, true).'</pre>');
  638. }
  639. }
  640. return $output;
  641. }
  642. /**
  643. * wpsc variation group name function
  644. * @return string - the variaton group name
  645. */
  646. function wpsc_the_vargrp_name() {
  647. // get the variation group name;
  648. global $wpsc_query;
  649. return $wpsc_query->variation_group['name'];
  650. }
  651. /**
  652. * wpsc variation group form ID function
  653. * @return string - the variation group form id, for labels and the like
  654. */
  655. function wpsc_vargrp_form_id() {
  656. // generate the variation group form ID;
  657. global $wpsc_query;
  658. $form_id = "variation_select_{$wpsc_query->product['id']}_{$wpsc_query->variation_group['variation_id']}";
  659. return $form_id;
  660. }
  661. /**
  662. * wpsc variation group ID function
  663. * @return integer - the variation group ID
  664. */
  665. function wpsc_vargrp_id() {
  666. global $wpsc_query;
  667. return $wpsc_query->variation_group['variation_id'];
  668. }
  669. /**
  670. * wpsc the variation name function
  671. * @return string - the variation name
  672. */
  673. function wpsc_the_variation_name() {
  674. global $wpsc_query;
  675. return stripslashes($wpsc_query->variation['name']);
  676. }
  677. /**
  678. * wpsc the variation stock function
  679. * @return string - HTML attribute to disable select options and radio buttons
  680. */
  681. function wpsc_the_variation_stock() {
  682. global $wpsc_query, $wpdb;
  683. $out_of_stock = false;
  684. if(($wpsc_query->variation_group_count == 1) && ($wpsc_query->product['quantity_limited'] == 1)) {
  685. $product_id = $wpsc_query->product['id'];
  686. $variation_group_id = $wpsc_query->variation_group['variation_id'];
  687. $variation_id = $wpsc_query->variation['id'];
  688. $priceandstock_id = $wpdb->get_var("SELECT `priceandstock_id` FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `product_id` = '{$product_id}' AND `value_id` IN ( '$variation_id' ) AND `all_variation_ids` IN('$variation_group_id') LIMIT 1");
  689. $variation_stock_data = $wpdb->get_var("SELECT `stock` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` = '{$priceandstock_id}' LIMIT 1");
  690. }
  691. return $variation_stock_data;
  692. }
  693. /**
  694. * wpsc the variation price function
  695. * @return string - the variation price
  696. */
  697. function wpsc_the_variation_price() {
  698. global $wpdb, $wpsc_query;
  699. if(count($wpsc_query->variation_groups) == 1) {
  700. //echo "<pre>".print_r($wpsc_query->variation, true)."</pre>";
  701. $product_id = $wpsc_query->product['id'];
  702. $variation_group_id = $wpsc_query->variation_group['variation_id'];
  703. $variation_id = $wpsc_query->variation['id'];
  704. $priceandstock_id = $wpdb->get_var("SELECT `priceandstock_id` FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `product_id` = '{$product_id}' AND `value_id` IN ( '$variation_id' ) AND `all_variation_ids` IN('$variation_group_id') LIMIT 1");
  705. $variation_price = $wpdb->get_var("SELECT `price` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` = '{$priceandstock_id}' LIMIT 1");
  706. $output = nzshpcrt_currency_display($variation_price, $wpsc_query->product['notax'], true);
  707. } else {
  708. $output = false;
  709. }
  710. return $output;
  711. }
  712. /**
  713. * wpsc the variation ID function
  714. * @return integer - the variation ID
  715. */
  716. function wpsc_the_variation_id() {
  717. global $wpsc_query;
  718. return $wpsc_query->variation['id'];
  719. }
  720. /**
  721. * wpsc the variation out_of_stock function
  722. * @return string - HTML attribute to disable select options and radio buttons
  723. */
  724. function wpsc_the_variation_out_of_stock() {
  725. global $wpsc_query, $wpdb;
  726. $out_of_stock = false;
  727. //$wpsc_query->the_variation();
  728. if(($wpsc_query->variation_group_count == 1) && ($wpsc_query->product['quantity_limited'] == 1)) {
  729. $product_id = $wpsc_query->product['id'];
  730. $variation_group_id = $wpsc_query->variation_group['variation_id'];
  731. $variation_id = $wpsc_query->variation['id'];
  732. $priceandstock_id = $wpdb->get_var("SELECT `priceandstock_id` FROM `".WPSC_TABLE_VARIATION_COMBINATIONS."` WHERE `product_id` = '{$product_id}' AND `value_id` IN ( '$variation_id' ) AND `all_variation_ids` IN('$variation_group_id') LIMIT 1");
  733. $variation_stock_data = $wpdb->get_var("SELECT `stock` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE `id` = '{$priceandstock_id}' LIMIT 1");
  734. if($variation_stock_data < 1) {
  735. $out_of_stock = true;
  736. }
  737. }
  738. if($out_of_stock == true) {
  739. return "disabled='disabled'";
  740. } else {
  741. return '';
  742. }
  743. }
  744. /**
  745. * wpsc custom meta name function
  746. * @return string - the custom metal name
  747. */
  748. function wpsc_custom_meta_name() {
  749. global $wpsc_query;
  750. return $wpsc_query->custom_meta_values['meta_key'];
  751. }
  752. /**
  753. * wpsc custom meta value function
  754. * @return string - the custom meta value
  755. */
  756. function wpsc_custom_meta_value() {
  757. global $wpsc_query;
  758. return $wpsc_query->custom_meta_values['meta_value'];
  759. }
  760. /**
  761. * wpsc product rater function
  762. * @return string - HTML to display the product rater
  763. */
  764. function wpsc_product_rater() {
  765. global $wpsc_query;
  766. if(get_option('product_ratings') == 1) {
  767. $output .= "<div class='product_footer'>";
  768. $output .= "<div class='product_average_vote'>";
  769. $output .= "<strong>".__('Avg. Customer Rating', 'wpsc').":</strong>";
  770. $output .= wpsc_product_existing_rating($wpsc_query->product['id']);
  771. $output .= "</div>";
  772. $output .= "<div class='product_user_vote'>";
  773. //$vote_output = nzshpcrt_product_vote($wpsc_query->product['id'],"onmouseover='hide_save_indicator(\"saved_".$wpsc_query->product['id']."_text\");'");
  774. $output .= "<strong><span id='rating_".$wpsc_query->product['id']."_text'>".__('Your Rating', 'wpsc').":</span>";
  775. $output .= "<span class='rating_saved' id='saved_".$wpsc_query->product['id']."_text'> ".__('Saved', 'wpsc')."</span>";
  776. $output .= "</strong>";
  777. $output .= wpsc_product_new_rating($wpsc_query->product['id']);
  778. $output .= "</div>";
  779. $output .= "</div>";
  780. }
  781. return $output;
  782. }
  783. function wpsc_product_existing_rating($product_id) {
  784. global $wpdb;
  785. $get_average = $wpdb->get_results("SELECT AVG(`rated`) AS `average`, COUNT(*) AS `count` FROM `".WPSC_TABLE_PRODUCT_RATING."` WHERE `productid`='".$product_id."'",ARRAY_A);
  786. $average = floor($get_average[0]['average']);
  787. $count = $get_average[0]['count'];
  788. $output .= " <span class='votetext'>";
  789. for($l=1; $l<=$average; ++$l) {
  790. $output .= "<img class='goldstar' src='". WPSC_URL."/images/gold-star.gif' alt='$l' title='$l' />";
  791. }
  792. $remainder = 5 - $average;
  793. for($l=1; $l<=$remainder; ++$l) {
  794. $output .= "<img class='goldstar' src='". WPSC_URL."/images/grey-star.gif' alt='$l' title='$l' />";
  795. }
  796. $output .= "<span class='vote_total'>&nbsp;(<span id='vote_total_$prodid'>".$count."</span>)</span> \r\n";
  797. $output .= "</span> \r\n";
  798. return $output;
  799. }
  800. function wpsc_product_new_rating($product_id) {
  801. global $wpdb;
  802. $cookie_data = explode(",",$_COOKIE['voting_cookie'][$product_id]);
  803. $vote_id = 0;
  804. if(is_numeric($cookie_data[0])){
  805. $vote_id = absint($cookie_data[0]);
  806. }
  807. $previous_vote = 1;
  808. if($vote_id > 0) {
  809. $previous_vote = $wpdb->get_var("SELECT `rated` FROM `".WPSC_TABLE_PRODUCT_RATING."` WHERE `id`='".$vote_id."' LIMIT 1");
  810. }
  811. //print("<pre>".print_r($previous_vote, true)."</pre>");
  812. //print("<pre>".print_r(func_get_args(), true)."</pre>");
  813. $output = "<form class='wpsc_product_rating' method='post'>\n";
  814. //$output .= " <input type='hidden' name='product_id' value='{$product_id}' />\n";
  815. $output .= " <input type='hidden' name='wpsc_ajax_action' value='rate_product' />\n";
  816. $output .= " <input type='hidden' class='wpsc_rating_product_id' name='product_id' value='{$product_id}' />\n";
  817. $output .= " <select class='wpsc_select_product_rating' name='product_rating'>\n";
  818. $output .= " <option ". (($previous_vote == '1') ? "selected='selected'" : '')." value='1'>1</option>\n";
  819. $output .= " <option ". (($previous_vote == '2') ? "selected='selected'" : '')." value='2'>2</option>\n";
  820. $output .= " <option ". (($previous_vote == '3') ? "selected='selected'" : '')." value='3'>3</option>\n";
  821. $output .= " <option ". (($previous_vote == '4') ? "selected='selected'" : '')." value='4'>4</option>\n";
  822. $output .= " <option ". (($previous_vote == '5') ? "selected='selected'" : '')." value='5'>5</option>\n";
  823. $output .= " </select>\n";
  824. $output .= " <input type='submit' value='".__('Save','wpsc')."'>";
  825. $output .= " </form>";
  826. return $output;
  827. }
  828. /**
  829. * wpsc has breadcrumbs function
  830. * @return boolean - true if we have and use them, false otherwise
  831. */
  832. function wpsc_has_breadcrumbs() {
  833. global $wpsc_query;
  834. if(($wpsc_query->breadcrumb_count > 0) && (get_option("show_breadcrumbs") == 1)){
  835. return true;
  836. } else {
  837. return false;
  838. }
  839. }
  840. /**
  841. * wpsc have breadcrumbs function
  842. * @return boolean - true if we have breadcrimbs to loop through
  843. */
  844. function wpsc_have_breadcrumbs() {
  845. global $wpsc_query;
  846. return $wpsc_query->have_breadcrumbs();
  847. }
  848. /**
  849. * wpsc the breadcrumbs function
  850. * @return nothing - iterate through the breadcrumbs
  851. */
  852. function wpsc_the_breadcrumb() {
  853. global $wpsc_query;
  854. $wpsc_query->the_breadcrumb();
  855. }
  856. /**
  857. * wpsc breadcrumb name function
  858. * @return string - the breadcrumb name
  859. */
  860. function wpsc_breadcrumb_name() {
  861. global $wpsc_query;
  862. return $wpsc_query->breadcrumb['name'];
  863. }
  864. /**
  865. * wpsc breadcrumb URL function
  866. * @return string - the breadcrumb URL
  867. */
  868. function wpsc_breadcrumb_url() {
  869. global $wpsc_query;
  870. if($wpsc_query->breadcrumb['url'] == '') {
  871. return false;
  872. } else {
  873. return $wpsc_query->breadcrumb['url'];
  874. }
  875. }
  876. /**
  877. * wpsc currency sign function
  878. * @return string - the selected currency sign for the store
  879. */
  880. function wpsc_currency_sign() {
  881. global $wpdb;
  882. $currency_sign_location = get_option('currency_sign_location');
  883. $currency_type = get_option('currency_type');
  884. $currency_symbol = $wpdb->get_var("SELECT `symbol_html` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`='".$currency_type."' LIMIT 1") ;
  885. return $currency_symbol;
  886. }
  887. /**
  888. * wpsc has pages function
  889. * @return boolean - true if we have pages
  890. */
  891. function wpsc_has_pages() {
  892. global $wpsc_query;
  893. if($wpsc_query->page_count > 0) {
  894. return true;
  895. } else {
  896. return false;
  897. }
  898. }
  899. /**
  900. * wpsc have pages function
  901. * @return boolean - true while we have pages to loop through
  902. */
  903. function wpsc_have_pages() {
  904. global $wpsc_query;
  905. return $wpsc_query->have_pages();
  906. }
  907. /**
  908. * wpsc the page function
  909. * @return nothing - iterate through the pages
  910. */
  911. function wpsc_the_page() {
  912. global $wpsc_query;
  913. $wpsc_query->the_page();
  914. }
  915. /**
  916. * wpsc page number function
  917. * @return integer - the page number
  918. */
  919. function wpsc_page_number() {
  920. global $wpsc_query;
  921. return $wpsc_query->page['number'];
  922. }
  923. /**
  924. * this is for the multi adding property, it checks to see whether multi adding is enabled;
  925. *
  926. */
  927. function wpsc_has_multi_adding(){
  928. if(get_option('multi_add') == 1 && (get_option('addtocart_or_buynow') != 1)){
  929. return true;
  930. }else{
  931. return false;
  932. }
  933. }
  934. /**
  935. * wpsc page is selected function
  936. * @return boolean - true if the page is selected
  937. */
  938. function wpsc_page_is_selected() {
  939. // determine if we are on this page
  940. global $wpsc_query;
  941. return $wpsc_query->page['selected'];
  942. }
  943. /**
  944. * wpsc page URL function
  945. * @return string - the page URL
  946. */
  947. function wpsc_page_url() {
  948. // generate the page URL
  949. global $wpsc_query;
  950. return $wpsc_query->page['url'];
  951. }
  952. /**
  953. * wpsc product count function
  954. * @return string - the page URL
  955. */
  956. function wpsc_product_count() {
  957. global $wpsc_query;
  958. return $wpsc_query->product_count;
  959. }
  960. /**
  961. * wpsc total product count function
  962. * @return int - total number of products
  963. */
  964. function wpsc_total_product_count() {
  965. global $wpsc_query;
  966. return $wpsc_query->total_product_count;
  967. }
  968. /**
  969. * The WPSC Query class.
  970. *
  971. * @since 3.7
  972. */
  973. class WPSC_Query {
  974. var $query;
  975. var $query_vars = array();
  976. var $queried_object;
  977. var $queried_object_id;
  978. var $request;
  979. // This selected category, for the breadcrumbs
  980. var $category;
  981. var $category_id_list = array();
  982. var $category_product = array();
  983. // product loop variables.
  984. var $products;
  985. var $product_count = 0;
  986. var $total_product_count = 0;
  987. var $current_product = -1;
  988. var $in_the_loop = false;
  989. var $product;
  990. // variation groups: i.e. colour, size
  991. var $variation_groups;
  992. var $variation_group_count = 0;
  993. var $current_variation_group = -1;
  994. var $variation_group;
  995. // for getting the product price
  996. var $first_variations;
  997. //variations inside variation groups: i.e. (red, green, blue) or (S, M, L, XL)
  998. var $variations;
  999. var $variations_count = 0;
  1000. var $current_variation = -1;
  1001. var $variation;
  1002. // Custom meta values
  1003. var $custom_meta;
  1004. var $custom_meta_count = 0;
  1005. var $current_custom_meta = -1;
  1006. var $custom_meta_values;
  1007. // Breadcrumbs
  1008. var $breadcrumbs;
  1009. var $breadcrumb_count = 0;
  1010. var $current_breadcrumb = -1;
  1011. var $breadcrumb;
  1012. // Pages
  1013. var $pages;
  1014. var $page_count = 0;
  1015. var $current_page = -1;
  1016. var $page;
  1017. var $found_products = 0;
  1018. var $max_num_pages = 0;
  1019. var $is_single = false;
  1020. var $is_search = false;
  1021. /**
  1022. * The WPSC query constructor, if no query is passed to it, it makes one from the WP_Query data
  1023. *
  1024. * @since 3.7
  1025. *
  1026. * @param string $query URL query string.
  1027. * @return WPSC_Query
  1028. */
  1029. function WPSC_Query ($query = '') {
  1030. if (empty($query)) {
  1031. $query = $this->create_default_query();
  1032. }
  1033. $this->parse_query($query);
  1034. $this->get_products();
  1035. //echo("<pre>".print_r($this,true)."</pre>");
  1036. }
  1037. function create_default_query() {
  1038. global $wp_query;
  1039. // get the product_url_name or ID
  1040. if(isset($wp_query->query_vars['product_url_name']) && ($wp_query->query_vars['product_url_name'] != '')) {
  1041. $query['product_url_name'] = $wp_query->query_vars['product_url_name'];
  1042. } else if(is_numeric($_GET['category'])) {
  1043. $query['product_id'] = $_GET['product_id'];
  1044. }
  1045. // get the category ID
  1046. if($wp_query->query_vars['category_id'] > 0) {
  1047. $query['category_id'] = $wp_query->query_vars['category_id'];
  1048. } else if(is_numeric($_GET['category'])) {
  1049. $query['category_id'] = $_GET['category'];
  1050. }
  1051. // get the category ID
  1052. if($wp_query->query_vars['category_id'] > 0) {
  1053. $query['category_id'] = $wp_query->query_vars['category_id'];
  1054. }
  1055. // get the page number
  1056. if($wp_query->query_vars['wpsc_page'] > 0) {
  1057. $query['page'] = $wp_query->query_vars['wpsc_page'];
  1058. } else if(is_numeric($_GET['page_number'])) {
  1059. $query['page'] = $_GET['page_number'];
  1060. }
  1061. if(isset($_GET['order'])) {
  1062. $query['sort_order'] = $_GET['order'];
  1063. $_SESSION['wpsc_product_order'] = $_GET['order'];
  1064. } else if(isset($_GET['product_order'])) {
  1065. $query['sort_order'] = $_GET['product_order'];
  1066. $_SESSION['wpsc_product_order'] = $_GET['order'];
  1067. } else if(isset($_SESSION['wpsc_product_order'])) {
  1068. $query['sort_order'] = $_SESSION['wpsc_product_order'];
  1069. }
  1070. if(isset($_GET['items_per_page'])) {
  1071. $query['number_per_page'] = $_GET['items_per_page'];
  1072. $_SESSION['wpsc_number_per_page'] = $_GET['items_per_page'];
  1073. } else if(isset($_SESSION['wpsc_product_order'])) {
  1074. $query['number_per_page'] = $_SESSION['wpsc_number_per_page'];
  1075. }
  1076. //$query['sort_order']
  1077. //$query['number_per_page']
  1078. return $query;
  1079. }
  1080. /**
  1081. * Resets query flags to false.
  1082. *
  1083. * The query flags are what page info wp-eCommerce was able to figure out, same as the equivalent method on WordPress
  1084. **/
  1085. function init_query_flags() {
  1086. $this->is_search = false;
  1087. $this->is_feed = false;
  1088. $this->is_404 = false;
  1089. }
  1090. /**
  1091. * Initiates object properties and sets default values, same as the equivalent method on WordPress
  1092. *
  1093. * @since 3.7
  1094. * @access public
  1095. **/
  1096. function init () {
  1097. $this->category = null;
  1098. unset($this->products);
  1099. unset($this->query);
  1100. $this->query_vars = array();
  1101. unset($this->queried_object);
  1102. unset($this->queried_object_id);
  1103. $this->product_count = 0;
  1104. $this->current_product = -1;
  1105. $this->in_the_loop = false;
  1106. $this->variation_groups = null;
  1107. $this->variation_group = null;
  1108. $this->variations = null;
  1109. $this->variation = null;
  1110. $this->custom_meta = null;
  1111. $this->custom_meta_values = null;
  1112. $this->breadcrumbs = null;
  1113. $this->breadcrumb = null;
  1114. $this->pages = null;
  1115. $this->page = null;
  1116. $this->init_query_flags();
  1117. }
  1118. /**
  1119. * Fills in the query variables, which do not exist within the parameter.
  1120. *
  1121. * @since 3.7
  1122. * @access public
  1123. *
  1124. * @param array $array Defined query variables.
  1125. * @return array Complete query variables with undefined ones filled in empty.
  1126. */
  1127. function fill_query_vars($array) {
  1128. /// remove the comments at the ends of lines in this array once all of these work, until then, only the ones with "// works" work
  1129. $keys = array(
  1130. 'product_id' // works
  1131. , 'product_url_name' // works
  1132. , 'product_name'
  1133. , 'category_id' // works
  1134. , 'category_url_name' // works
  1135. , 'tag'
  1136. , 'price'
  1137. , 'limit_of_items'
  1138. , 'sort_order'
  1139. , 'number_per_page' // works
  1140. , 'page'
  1141. , 'custom_query'
  1142. //, 'sku'
  1143. );
  1144. foreach ($keys as $key) {
  1145. if ( !isset($array[$key]))
  1146. $array[$key] = '';
  1147. }
  1148. return $array;
  1149. }
  1150. /**
  1151. * Parse a query string and set query type booleans.
  1152. *
  1153. * @since 3.7
  1154. * @access public
  1155. *
  1156. * @param string|array $query
  1157. */
  1158. function parse_query ($query) {
  1159. if ( !empty($query) || !isset($this->query) ) {
  1160. $this->init();
  1161. if ( is_array($query) )
  1162. $this->query_vars = $query;
  1163. else
  1164. parse_str($query, $this->query_vars);
  1165. $this->query = $query;
  1166. }
  1167. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1168. $qv = &$this->query_vars;
  1169. // we need a category ID
  1170. if(!($qv['category_id'] > 0) && ($qv['category_url_name'] == '')) {
  1171. $qv['category_id'] = get_option('wpsc_default_category');
  1172. }
  1173. // we need a number of items per page
  1174. if(!($qv['number_per_page'] > 0) && ($qv['number_per_page'] != 'all')) {
  1175. $qv['number_per_page'] = get_option('wpsc_products_per_page');
  1176. }
  1177. $qv['product_id'] = absint($qv['product_id']);
  1178. $qv['product_url_name'] = trim($qv['product_url_name']);
  1179. $qv['product_name'] = trim($qv['product_name']);
  1180. $qv['category_id'] = absint($qv['category_id']);
  1181. $qv['category_url_name'] = trim($qv['category_url_name']);
  1182. $qv['tag'] = trim($qv['tag']);
  1183. $qv['price'] = absint($qv['price']);
  1184. $qv['limit_of_items'] = absint($qv['limit_of_items']);
  1185. $qv['sort_order'] = trim($qv['sort_order']);
  1186. $qv['number_per_page'] = absint($qv['number_per_page']);
  1187. $qv['page'] = absint($qv['page']);
  1188. $qv['custom_query'] = (bool)$qv['custom_query'];
  1189. }
  1190. function &get_products() {
  1191. global $wpdb, $wp_query;
  1192. do_action_ref_array('pre_get_products', array(&$this));
  1193. if(($this->query_vars['category_url_name'] != '')) {
  1194. $category_data = $wpdb->get_row("SELECT `id`, `image_height`, `image_width` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `active`='1' AND `nice-name` = '{$this->query_vars['category_url_name']}' LIMIT 1", ARRAY_A);
  1195. $this->query_vars['category_id'] = $category_data['id'];
  1196. $this->category = $this->query_vars['category_id'];
  1197. } else if($this->query_vars['category_id'] > 0) {
  1198. $category_data = $wpdb->get_row("SELECT `image_height`, `image_width` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `active`='1' AND `id` = '{$this->query_vars['category_id']}' LIMIT 1", ARRAY_A);
  1199. }
  1200. // Show subcategory products on parent category page?
  1201. $show_subcatsprods_in_cat = get_option( 'show_subcatsprods_in_cat' );
  1202. $this->category_id_list = array( $this->query_vars['category_id'] );
  1203. if ( $show_subcatsprods_in_cat && $this->query_vars['category_id'] > 0 ) {
  1204. $this->category_id_list = array_merge( (array)$this->category_id_list, (array)wpsc_list_subcategories( $this->query_vars['category_id'] ) );
  1205. }
  1206. //exit('Here:<pre>'.print_r($category_id_list, true).'</pre>');
  1207. if(is_array($category_data)) {
  1208. $this->category_product['image_height'] = $category_data['image_height'];
  1209. $this->category_product['image_width'] = $category_data['image_width'];
  1210. }
  1211. if($this->query_vars['product_url_name'] != null) {
  1212. $product_id = $wpdb->get_var("SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '".stripslashes($this->query_vars['product_url_name'])."' ) ORDER BY `product_id` DESC LIMIT 1");
  1213. } else {
  1214. $product_id = absint($this->query_vars['product_id']);
  1215. }
  1216. if(($product_id > 0)) {
  1217. $product_list = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='".(int)$product_id."' AND `active` IN('1') AND `publish` IN('1') LIMIT 1",ARRAY_A);
  1218. }
  1219. if(isset($_SESSION['price_range']) && isset($_GET['range'])){
  1220. if (is_numeric($_GET['range']) || isset($_SESSION['price_range'])) {
  1221. $price_ranges = $_SESSION['price_range'];
  1222. $selected_price_point = absint($_GET['range']);
  1223. $next_price_point = $selected_price_point + 1;
  1224. //echo "<pre>".print_r($ranges,true)."</pre>";
  1225. $product_range_sql_parts = array();
  1226. $variation_sql_parts = array();
  1227. $product_sql_parts = array();
  1228. if(isset($price_ranges[$selected_price_point])) {
  1229. $product_range_sql_parts[] = "(`price` - `special_price`) >= '".absint($price_ranges[$selected_price_point])."'";
  1230. $variation_sql_parts[] = "`price` >= '".absint($price_ranges[$selected_price_point])."'";
  1231. if(isset($price_ranges[$next_price_point])) {
  1232. $product_range_sql_parts[] = "(`price` - `special_price`) < '".absint($price_ranges[$next_price_point])."'";
  1233. $variation_sql_parts[] = "`price` < '".absint($price_ranges[$next_price_point])."'";
  1234. }
  1235. $variation_product_ids = (array)$wpdb->get_col("SELECT DISTINCT `product_id` FROM `".WPSC_TABLE_VARIATION_PROPERTIES."` WHERE ".implode(" AND ", $variation_sql_parts)."");
  1236. if(count($variation_product_ids) > 0 ) {
  1237. $product_sql_parts[] = "( (".implode(" AND ", $product_range_sql_parts).") OR `id` IN('".implode("', '", $variation_product_ids)."') )";
  1238. } else {
  1239. $product_sql_parts += $product_range_sql_parts;
  1240. }
  1241. }
  1242. $product_sql_parts[] = "`active` IN ('1')";
  1243. $product_sql_parts[] = "`publish` IN('1')";
  1244. $range_sql="SELECT * FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE ".implode(" AND ", $product_sql_parts)."";
  1245. $product_list = $wpdb->get_results($range_sql,ARRAY_A);
  1246. }
  1247. }
  1248. //exit('Here:<pre>'.print_r($this->query_vars, true).'</pre>');
  1249. if(count($product_list) > 0 && !isset($_GET['range'])) {
  1250. // if is a single product
  1251. $this->is_single = true;
  1252. $this->products = $product_list;
  1253. $this->category = $this->query_vars['category_id'];
  1254. } elseif(count($product_list) > 0 && isset($_GET['range'])) {
  1255. $this->is_single = false;
  1256. $this->products = $product_list;
  1257. $this->category = $this->query_vars['category_id'];
  1258. }else{
  1259. // Otherwise
  1260. //if we are using pages, how many items per page and what page?
  1261. if((get_option('use_pagination') == 1)) {
  1262. $products_per_page = $this->query_vars['number_per_page'];
  1263. if($this->query_vars['page'] > 0) {
  1264. $startnum = ($this->query_vars['page']-1)*$products_per_page;
  1265. } else {
  1266. $startnum = 0;
  1267. }
  1268. } else {
  1269. $startnum = 0;
  1270. }
  1271. // search section is done here
  1272. if(function_exists('gold_shpcrt_search_sql') && ($_GET['product_search'] != '')) {
  1273. $search_sql = gold_shpcrt_search_sql();
  1274. if($search_sql != '') {
  1275. // this cannot currently list products that are associated with no categories
  1276. $rowcount = $wpdb->get_var("SELECT COUNT( DISTINCT `".WPSC_TABLE_PRODUCT_LIST."`.`id`) AS `count` FROM `".WPSC_TABLE_PRODUCT_LIST."`,`".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` WHERE `".WPSC_TABLE_PRODUCT_LIST."`.`publish`='1' AND `".WPSC_TABLE_PRODUCT_LIST."`.`active`='1' AND `".WPSC_TABLE_PRODUCT_LIST."`.`id` = `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`product_id` AND `".WPSC_TABLE_PRODUCT_LIST."`.donation = '0' $search_sql");
  1277. if (isset($_SESSION['item_per_page']))
  1278. $products_per_page = $_SESSION['item_per_page'];
  1279. //exit($products_per_page);
  1280. if(!is_numeric($products_per_page) || ($products_per_page < 1)) { $products_per_page = $rowcount; }
  1281. if($startnum >= $rowcount) {
  1282. $startnum = 0;
  1283. }
  1284. $sql = "SELECT DISTINCT `".WPSC_TABLE_PRODUCT_LIST."`.* FROM `".WPSC_TABLE_PRODUCT_LIST."`,`".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` WHERE `".WPSC_TABLE_PRODUCT_LIST."`.`publish`='1' AND `".WPSC_TABLE_PRODUCT_LIST."`.`active`='1' AND `".WPSC_TABLE_PRODUCT_LIST."`.`id` = `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`product_id` $no_donations_sql $search_sql ORDER BY `".WPSC_TABLE_PRODUCT_LIST."`.`special` DESC LIMIT $startnum, $products_per_page";
  1285. }
  1286. } else if (($wp_query->query_vars['ptag'] != null) || ( $_GET['ptag']!=null)) {
  1287. // search by tags is done here
  1288. if($wp_query->query_vars['ptag'] != null) {
  1289. $tag = $wp_query->query_vars['ptag'];
  1290. } else {
  1291. $tag = $_GET['ptag'];
  1292. }
  1293. $results = $wpdb->get_results("SELECT * FROM `{$wpdb->terms}` WHERE slug='$tag'");
  1294. $term_id = $results[0]->term_id;
  1295. $results = $wpdb->get_results("SELECT * FROM `{$wpdb->term_taxonomy}` WHERE term_id = '".$term_id."' AND taxonomy='product_tag'");
  1296. $taxonomy_id = $results[0]->term_taxonomy_id;
  1297. $results = $wpdb->get_results("SELECT * FROM `{$wpdb->term_relationships}` WHERE term_taxonomy_id = '".$taxonomy_id."'");
  1298. foreach ($results as $result) {
  1299. $product_ids[] = $result->object_id;
  1300. }
  1301. $product_id = implode(",",$product_ids);
  1302. $sql = "SELECT * FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id IN (".$product_id.") AND `publish` IN('1') AND `active` IN('1')"; //Transom - added publish & active
  1303. } else {
  1304. // select by category is done here
  1305. if(is_numeric($this->query_vars['category_id']) && ($this->query_vars['category_id'] > 0)) {
  1306. /*
  1307. * The reason this is so complicated is because of the product ordering, it is done by category/product association
  1308. * If you can see a way of simplifying it and speeding it up, then go for it.
  1309. */
  1310. $rowcount = $wpdb->get_var("SELECT COUNT( DISTINCT `".WPSC_TABLE_PRODUCT_LIST."`.`id`) AS `count` FROM `".WPSC_TABLE_PRODUCT_LIST."` LEFT JOIN `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` ON `".WPSC_TABLE_PRODUCT_LIST."`.`id` = `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`product_id` WHERE `".WPSC_TABLE_PRODUCT_LIST."`.`publish`='1' AND `".WPSC_TABLE_PRODUCT_LIST."`.`active` = '1' AND `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`category_id` IN ('".implode("','", $this->category_id_list)."') $no_donations_sql");
  1311. if(!is_numeric($products_per_page) || ($products_per_page < 1)) { $products_per_page = $rowcount; }
  1312. if($startnum >= $rowcount) {
  1313. $startnum = 0;
  1314. }
  1315. if($this->query_vars['sort_order']=='DESC') {
  1316. $order = 'DESC';
  1317. } else {
  1318. $order = 'ASC';
  1319. }
  1320. // Invert this for alphabetical ordering.
  1321. if (get_option('wpsc_sort_by')=='name') {
  1322. $order_by = "`products`.`name` $order";
  1323. } else if (get_option('wpsc_sort_by') == 'price') {
  1324. $order_by = "`products`.`price` $order";
  1325. } else {
  1326. //$order = 'ASC';
  1327. if( $order == 'ASC'){
  1328. $product_id_order = 'DESC';
  1329. }else{
  1330. $product_id_order = 'ASC';
  1331. }
  1332. $order_by = " `category`.`name`, `order_state` DESC,`order`.`order` $order, `products`.`id` $product_id_order";
  1333. //$order_by = " `order_state` DESC, `products`.`id` $product_id_order,`order`.`order` $order";
  1334. }
  1335. $sql = "SELECT DISTINCT `products`. * , `category`.`name` AS `category` , `cat_assoc`.`category_id` , `order`.`order` , IF( ISNULL( `order`.`order` ) , 0, 1 ) AS `order_state`
  1336. FROM `".WPSC_TABLE_PRODUCT_LIST."` AS `products`
  1337. LEFT JOIN `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` AS `cat_assoc`
  1338. ON `products`.`id` = `cat_assoc`.`product_id`
  1339. LEFT JOIN `".WPSC_TABLE_PRODUCT_CATEGORIES."` AS `category`
  1340. ON `cat_assoc`.`category_id` = `category`.`id`
  1341. LEFT JOIN `".WPSC_TABLE_PRODUCT_ORDER."` AS `order`
  1342. ON (
  1343. (`products`.`id` = `order`.`product_id`)
  1344. AND
  1345. (`cat_assoc`.`category_id` = `order`.`category_id`)
  1346. )
  1347. WHERE `products`.`publish` = '1'
  1348. AND `products`.`active` = '1'
  1349. AND `cat_assoc`.`category_id` IN ( '".implode("','", $this->category_id_list)."' ) $no_donations_sql
  1350. GROUP BY `products`.`id`
  1351. ORDER BY $order_by LIMIT $startnum, $products_per_page";
  1352. } else {
  1353. if ($this->query_vars['sort_order']=='DESC') {
  1354. $order = 'DESC';
  1355. } else {
  1356. $order = 'ASC';
  1357. }
  1358. if (get_option('wpsc_sort_by')=='name') {
  1359. $order_by = "`".WPSC_TABLE_PRODUCT_LIST."`.`name` $order";
  1360. } else if (get_option('wpsc_sort_by') == 'pric