woocommerce.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderWooCommerce{
  9. const ARG_REGULAR_PRICE_FROM = "reg_price_from";
  10. const ARG_REGULAR_PRICE_TO = "reg_price_to";
  11. const ARG_SALE_PRICE_FROM = "sale_price_from";
  12. const ARG_SALE_PRICE_TO = "sale_price_to";
  13. const ARG_IN_STOCK_ONLY = "instock_only";
  14. const ARG_FEATURED_ONLY = "featured_only";
  15. const META_REGULAR_PRICE = "_regular_price";
  16. const META_SALE_PRICE = "_sale_price";
  17. const META_SKU = "_sku"; //can be 'instock' or 'outofstock'
  18. const META_STOCK = "_stock"; //can be 'instock' or 'outofstock'
  19. const SORTBY_NUMSALES = "meta_num_total_sales";
  20. const SORTBY_REGULAR_PRICE = "meta_num__regular_price";
  21. const SORTBY_SALE_PRICE = "meta_num__sale_price";
  22. const SORTBY_FEATURED = "meta__featured";
  23. const SORTBY_SKU = "meta__sku";
  24. const SORTBY_STOCK = "meta_num_stock";
  25. /**
  26. *
  27. * return true / false if the woo commerce exists
  28. */
  29. public static function isWooCommerceExists(){
  30. if(class_exists( 'Woocommerce' ))
  31. return(true);
  32. return(false);
  33. }
  34. /**
  35. * compare wc current version to given version
  36. */
  37. public static function version_check( $version = '1.0' ) {
  38. if(self::isWooCommerceExists()){
  39. global $woocommerce;
  40. if(version_compare($woocommerce->version, $version, '>=')){
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. *
  48. * get wc post types
  49. */
  50. public static function getCustomPostTypes(){
  51. $arr = array();
  52. $arr["product"] = __("Product", 'revslider');
  53. $arr["product_variation"] = __("Product Variation", 'revslider');
  54. return($arr);
  55. }
  56. /**
  57. *
  58. * get price query
  59. */
  60. private static function getPriceQuery($priceFrom, $priceTo, $metaTag){
  61. if(empty($priceFrom))
  62. $priceFrom = 0;
  63. if(empty($priceTo))
  64. $priceTo = 9999999999;
  65. $query = array( 'key' => $metaTag,
  66. 'value' => array( $priceFrom, $priceTo),
  67. 'type' => 'numeric',
  68. 'compare' => 'BETWEEN');
  69. return($query);
  70. }
  71. /**
  72. *
  73. * get meta query for filtering woocommerce posts.
  74. */
  75. public static function getMetaQuery($args){
  76. $regPriceFrom = RevSliderFunctions::getVal($args, self::ARG_REGULAR_PRICE_FROM);
  77. $regPriceTo = RevSliderFunctions::getVal($args, self::ARG_REGULAR_PRICE_TO);
  78. $salePriceFrom = RevSliderFunctions::getVal($args, self::ARG_SALE_PRICE_FROM);
  79. $salePriceTo = RevSliderFunctions::getVal($args, self::ARG_SALE_PRICE_TO);
  80. $inStockOnly = RevSliderFunctions::getVal($args, self::ARG_IN_STOCK_ONLY);
  81. $featuredOnly = RevSliderFunctions::getVal($args, self::ARG_FEATURED_ONLY);
  82. $arrQueries = array();
  83. $tax_query = array();
  84. //get regular price array
  85. if(!empty($regPriceFrom) || !empty($regPriceTo)){
  86. $arrQueries[] = self::getPriceQuery($regPriceFrom, $regPriceTo, self::META_REGULAR_PRICE);
  87. }
  88. //get sale price array
  89. if(!empty($salePriceFrom) || !empty($salePriceTo)){
  90. $arrQueries[] = self::getPriceQuery($salePriceFrom, $salePriceTo, self::META_SALE_PRICE);
  91. }
  92. if($inStockOnly == "on"){
  93. $tax_query[] = array(
  94. 'taxonomy' => 'product_visibility',
  95. 'field' => 'name',
  96. 'terms' => 'outofstock',
  97. 'operator' => 'NOT IN',
  98. );
  99. }
  100. if($featuredOnly == "on"){
  101. $tax_query[] = array(
  102. 'taxonomy' => 'product_visibility',
  103. 'field' => 'name',
  104. 'terms' => 'featured',
  105. );
  106. }
  107. $query = array();
  108. if(!empty($arrQueries)){
  109. $query = array("meta_query"=>$arrQueries);
  110. }
  111. if(!empty($tax_query)){
  112. $query['tax_query'] = $tax_query;
  113. }
  114. return($query);
  115. }
  116. /**
  117. *
  118. * get sortby function including standart wp sortby array
  119. */
  120. public static function getArrSortBy(){
  121. $arrSortBy = array();
  122. $arrSortBy[self::SORTBY_REGULAR_PRICE] = __("Regular Price", 'revslider');
  123. $arrSortBy[self::SORTBY_SALE_PRICE] = __("Sale Price", 'revslider');
  124. $arrSortBy[self::SORTBY_NUMSALES] = __("Number Of Sales", 'revslider');
  125. $arrSortBy[self::SORTBY_FEATURED] = __("Featured Products", 'revslider');
  126. $arrSortBy[self::SORTBY_SKU] = __("SKU", 'revslider');
  127. $arrSortBy[self::SORTBY_STOCK] = __("Stock Quantity", 'revslider');
  128. return($arrSortBy);
  129. }
  130. } //end of the class
  131. ?>