PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-lister-for-ebay/classes/integration/ProductWrapper_woo.php

https://bitbucket.org/sanders_nick/my-maxi-skirt
PHP | 436 lines | 224 code | 89 blank | 123 comment | 36 complexity | 9ce9f9bf989dabad3acef55210066de9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * wrapper functions to access products on WooCommerce
  4. */
  5. class ProductWrapper {
  6. const plugin = 'woo';
  7. const post_type = 'product';
  8. const taxonomy = 'product_cat';
  9. const menu_page_position = 57;
  10. // get custom post type
  11. static function getPostType() {
  12. return self::post_type;
  13. }
  14. // get product catrgories taxonomy
  15. static function getTaxonomy() {
  16. return self::taxonomy;
  17. }
  18. // get product price
  19. static function getPrice( $post_id ) {
  20. $sale_price = get_post_meta( $post_id, '_sale_price', true);
  21. if ( floatval($sale_price) > 0 ) return $sale_price;
  22. return get_post_meta( $post_id, '_price', true);
  23. }
  24. // set product price
  25. static function setPrice( $post_id, $price ) {
  26. update_post_meta( $post_id, '_price', $price);
  27. update_post_meta( $post_id, '_regular_price', $price);
  28. }
  29. // get product sku
  30. static function getSKU( $post_id ) {
  31. return get_post_meta( $post_id, '_sku', true);
  32. }
  33. // set product sku
  34. static function setSKU( $post_id, $sku ) {
  35. return update_post_meta( $post_id, '_sku', $sku);
  36. }
  37. // get product stock (deprecated)
  38. static function getStock( $post_id ) {
  39. return get_post_meta( $post_id, '_stock', true);
  40. }
  41. // set product stock (deprecated)
  42. static function setStock( $post_id, $stock ) {
  43. return update_post_meta( $post_id, '_stock', $stock);
  44. }
  45. // get product weight
  46. static function getWeight( $post_id, $include_weight_unit = false ) {
  47. return get_post_meta( $post_id, '_weight', true);
  48. }
  49. // get product weight as major weight and minor
  50. static function getEbayWeight( $post_id ) {
  51. $weight_value = self::getWeight( $post_id );
  52. $weigth_unit = get_option( 'woocommerce_weight_unit' );
  53. // convert value to major and minor if unit is gram or ounces
  54. if ( 'g' == $weigth_unit ) {
  55. $kg = intval( $weight_value / 1000 );
  56. $g = $weight_value - $kg * 1000 ;
  57. $weight_major = $kg;
  58. $weight_minor = $g;
  59. } elseif ( 'oz' == $weigth_unit ) {
  60. $lbs = intval( $weight_value / 16 );
  61. $oz = $weight_value - $lbs * 16 ;
  62. $weight_major = $lbs;
  63. $weight_minor = $oz;
  64. } else {
  65. $weight_major = $weight_value;
  66. $weight_minor = 0;
  67. }
  68. return array( $weight_major, $weight_minor );
  69. }
  70. // get product dimensions array
  71. static function getDimensions( $post_id ) {
  72. $dimensions = array();
  73. $unit = get_option( 'woocommerce_dimension_unit' );
  74. $dimensions['length'] = get_post_meta( $post_id, '_length', true);
  75. $dimensions['height'] = get_post_meta( $post_id, '_height', true);
  76. $dimensions['width'] = get_post_meta( $post_id, '_width', true);
  77. $dimensions['length_unit'] = $unit;
  78. $dimensions['height_unit'] = $unit;
  79. $dimensions['width_unit'] = $unit;
  80. return $dimensions;
  81. }
  82. // get product featured image
  83. static function getImageURL( $post_id ) {
  84. // this seems to be neccessary for listing previews on some installations
  85. if ( ! function_exists('get_post_thumbnail_id'))
  86. require_once( ABSPATH . 'wp-includes/post-thumbnail-template.php');
  87. $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large');
  88. return $large_image_url[0];
  89. }
  90. // get all product attributes
  91. static function getAttributes( $post_id ) {
  92. global $woocommerce;
  93. $attributes = array();
  94. $product = new WC_Product( $post_id );
  95. $attribute_taxnomies = $product->get_attributes();
  96. global $wpl_logger;
  97. $wpl_logger->info('attribute_taxnomies: '.print_r($attribute_taxnomies,1));
  98. foreach ($attribute_taxnomies as $attribute) {
  99. $terms = wp_get_post_terms( $post_id, $attribute['name'] );
  100. $wpl_logger->info('terms: '.print_r($terms,1));
  101. if ( is_wp_error($terms) ) {
  102. // echo "post id: $post_id <br>";
  103. // echo "attribute name: " . $attribute['name']."<br>";
  104. // echo "attribute: " . print_r( $attribute )."<br>";
  105. // echo "error: " . $terms->get_error_message();
  106. continue;
  107. }
  108. if ( count( $terms ) > 0 ) {
  109. $attribute_name = $woocommerce->attribute_label( $attribute['name'] );
  110. $attributes[ $attribute_name ] = $terms[0]->name;
  111. }
  112. }
  113. return $attributes;
  114. // Array
  115. // (
  116. // [Platform] => Nintendo DS
  117. // [Genre] => Puzzle
  118. // )
  119. }
  120. // check if product has variations
  121. static function hasVariations( $post_id ) {
  122. $product = new WC_Product( $post_id );
  123. $variations = $product->get_available_variations();
  124. if ( ! is_array($variations) ) return false;
  125. if ( 0 == count($variations) ) return false;
  126. return true;
  127. }
  128. // get all product variations
  129. static function getVariations( $post_id ) {
  130. global $woocommerce;
  131. $product = new WC_Product( $post_id );
  132. $available_variations = $product->get_available_variations();
  133. $attributes = $product->get_variation_attributes();
  134. // echo "<pre>";print_r($available_variations);die();echo"</pre>";
  135. // echo "<pre>";print_r($attributes);die();echo"</pre>";
  136. // (
  137. // [pa_size] => Array
  138. // (
  139. // [0] => x-large
  140. // [1] => large
  141. // [2] => medium
  142. // [3] => small
  143. // )
  144. // [pa_colour] => Array
  145. // (
  146. // [0] => yellow
  147. // [1] => orange
  148. // )
  149. // )
  150. // build array of attribute labels
  151. $attribute_labels = array();
  152. foreach ( $attributes as $name => $options ) {
  153. $label = $woocommerce->attribute_label($name);
  154. if ($label == '') $label = $name;
  155. $id = "attribute_".sanitize_title($name);
  156. $attribute_labels[ $id ] = $label;
  157. } // foreach $attributes
  158. // print_r($attribute_labels);die();
  159. // (
  160. // [attribute_pa_size] => Size
  161. // [attribute_pa_colour] => Colour
  162. // )
  163. // loop variations
  164. foreach ($available_variations as $var) {
  165. // find child post_id for this variation
  166. $var_id = $var['variation_id'];
  167. // build variation array for wp-lister
  168. $newvar = array();
  169. $newvar['post_id'] = $var_id;
  170. // $newvar['term_id'] = $var->term_id;
  171. $attributes = $var['attributes'];
  172. $newvar['variation_attributes'] = array();
  173. foreach ($attributes as $key => $value) { // this loop will only run once for one dimensional variations
  174. // $newvar['name'] = $value; #deprecated
  175. // v2
  176. $taxonomy = str_replace('attribute_', '', $key); // attribute_pa_color -> pa_color
  177. $term = get_term_by('slug', $value, $taxonomy );
  178. if ( $term ) {
  179. // handle proper attribute taxonomies
  180. $newvar['variation_attributes'][ @$attribute_labels[ $key ] ] = $term->name;
  181. } else {
  182. // handle fake custom product attributes
  183. $newvar['variation_attributes'][ @$attribute_labels[ $key ] ] = $value;
  184. // echo "no term found for $value<br>";
  185. }
  186. }
  187. // $newvar['group_name'] = $attribute_labels[ $key ]; #deprecated
  188. $newvar['price'] = self::getPrice( $var_id );
  189. $newvar['stock'] = self::getStock( $var_id );
  190. $newvar['weight'] = self::getWeight( $var_id );
  191. $newvar['dimensions'] = self::getDimensions( $var_id );
  192. $newvar['sku'] = self::getSKU( $var_id );
  193. list( $weight_major, $weight_minor ) = self::getEbayWeight( $var_id );
  194. if ( ($weight_major == 0) && ($weight_minor == 0) ) {
  195. list( $weight_major, $weight_minor ) = self::getEbayWeight( $post_id );
  196. }
  197. $newvar['weight_major'] = $weight_major;
  198. $newvar['weight_minor'] = $weight_minor;
  199. $var_image = self::getImageURL( $var_id );
  200. $newvar['image'] = ($var_image == '') ? self::getImageURL( $post_id ) : $var_image;
  201. // add to collection
  202. $variations[] = $newvar;
  203. }
  204. return $variations;
  205. // echo "<pre>";print_r($variations);die();echo"</pre>";
  206. /* the returned array looks like this:
  207. [0] => Array
  208. (
  209. [post_id] => 1126
  210. [variation_attributes] => Array
  211. (
  212. [Size] => large
  213. [Colour] => yellow
  214. )
  215. [name] => yellow
  216. [group_name] => Colour
  217. [price] =>
  218. [stock] =>
  219. [weight] =>
  220. [sku] =>
  221. [image] => http://www.example.com/wp-content/uploads/2011/09/days-end.jpg
  222. )
  223. [1] => Array
  224. (
  225. [post_id] => 1253
  226. [variation_attributes] => Array
  227. (
  228. [Size] => large
  229. [Colour] => orange
  230. )
  231. [name] => orange
  232. [group_name] => Colour
  233. [price] =>
  234. [stock] =>
  235. [weight] =>
  236. [sku] =>
  237. [image] => http://www.example.com/wp-content/uploads/2011/09/days-end.jpg
  238. )
  239. */
  240. }
  241. // get a list of all available attribute names
  242. static function getAttributeTaxonomies() {
  243. global $woocommerce;
  244. // $attribute_taxonomies = $woocommerce->get_attribute_taxonomies();
  245. $attribute_taxonomies = $woocommerce->get_attribute_taxonomy_names();
  246. // print_r($attribute_taxonomies);
  247. $attributes = array();
  248. foreach ($attribute_taxonomies as $tax) {
  249. $attrib = new stdClass();
  250. $attrib->name = $woocommerce->attribute_label( $tax );
  251. $attrib->label = $woocommerce->attribute_label( $tax );
  252. $attributes[] = $attrib;
  253. }
  254. // print_r($attributes);die();
  255. return $attributes;
  256. }
  257. // check if current page is products list page
  258. static function isProductsPage() {
  259. global $pagenow;
  260. if ( ( isset( $_GET['post_type'] ) ) &&
  261. ( $_GET['post_type'] == self::getPostType() ) &&
  262. ( $pagenow == 'edit.php' ) ) {
  263. return true;
  264. }
  265. return false;
  266. }
  267. /*
  268. * private functions (WooCommerce only)
  269. */
  270. // find variation by attributes (private)
  271. static function findVariationID( $parent_id, $VariationSpecifics ) {
  272. global $wpl_logger;
  273. $variations = self::getVariations( $parent_id );
  274. foreach ($variations as $var) {
  275. $diffs = array_diff_assoc( $var['variation_attributes'], $VariationSpecifics );
  276. if ( count($diffs) == 0 ) {
  277. $wpl_logger->info('findVariationID('.$parent_id.') found: '.$var['post_id']);
  278. $wpl_logger->info('VariationSpecifics: '.print_r($VariationSpecifics,1));
  279. return $var['post_id'];
  280. }
  281. }
  282. return false;
  283. }
  284. }
  285. // testing area...
  286. /**
  287. * Columns for Products page
  288. **/
  289. add_filter('manage_edit-product_columns', 'wpl_woocommerce_edit_product_columns', 11 );
  290. function wpl_woocommerce_edit_product_columns($columns){
  291. $columns['listed'] = '<img src="'.WPLISTER_URL.'/img/hammer-dark-16x16.png" title="'.__('Listing status', 'wplister').'" />';
  292. return $columns;
  293. }
  294. /**
  295. * Custom Columns for Products page
  296. **/
  297. add_action('manage_product_posts_custom_column', 'wplister_woocommerce_custom_product_columns', 3 );
  298. function wplister_woocommerce_custom_product_columns( $column ) {
  299. global $post, $woocommerce;
  300. // $product = new WC_Product($post->ID);
  301. switch ($column) {
  302. case "listed" :
  303. $listingsModel = new ListingsModel();
  304. $status = $listingsModel->getStatusFromPostID( $post->ID );
  305. if ( ! $status ) break;
  306. switch ($status) {
  307. case 'published':
  308. case 'changed':
  309. $ebayUrl = $listingsModel->getViewItemURLFromPostID( $post->ID );
  310. echo '<a href="'.$ebayUrl.'" title="View on eBay" target="_blank"><img src="'.WPLISTER_URL.'/img/ebay-16x16.png" alt="yes" /></a>';
  311. break;
  312. case 'prepared':
  313. echo '<img src="'.WPLISTER_URL.'/img/hammer-orange-16x16.png" title="prepared" />';
  314. break;
  315. case 'verified':
  316. echo '<img src="'.WPLISTER_URL.'/img/hammer-green-16x16.png" title="prepared" />';
  317. break;
  318. case 'ended':
  319. echo '<img src="'.WPLISTER_URL.'/img/hammer-16x16.png" title="ended" />';
  320. break;
  321. default:
  322. echo '<img src="'.WPLISTER_URL.'/img/hammer-16x16.png" alt="yes" />';
  323. break;
  324. }
  325. break;
  326. } // switch ($column)
  327. }
  328. // hook into save_post to mark listing as changed when a product is updated
  329. function wplister_on_woocommerce_product_quick_edit_save( $post_id, $post ) {
  330. if ( !$_POST ) return $post_id;
  331. if ( is_int( wp_is_post_revision( $post_id ) ) ) return;
  332. if( is_int( wp_is_post_autosave( $post_id ) ) ) return;
  333. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
  334. // if ( !isset($_POST['woocommerce_quick_edit_nonce']) || (isset($_POST['woocommerce_quick_edit_nonce']) && !wp_verify_nonce( $_POST['woocommerce_quick_edit_nonce'], 'woocommerce_quick_edit_nonce' ))) return $post_id;
  335. if ( !current_user_can( 'edit_post', $post_id )) return $post_id;
  336. if ( $post->post_type != 'product' ) return $post_id;
  337. // global $woocommerce, $wpdb;
  338. // $product = new WC_Product( $post_id );
  339. $lm = new ListingsModel();
  340. $lm->markItemAsModified( $post_id );
  341. // Clear transient
  342. // $woocommerce->clear_product_transients( $post_id );
  343. }
  344. add_action( 'save_post', 'wplister_on_woocommerce_product_quick_edit_save', 10, 2 );