/class-billogram2-product-xml.php

  1. <?php 
  2. include_once("class-billogram2-xml.php"); 
  3. class WCB_Product_XML_Document extends WCB_XML_Document{ 
  4. /** 
  5. * 
  6. */ 
  7. function __construct() { 
  8. parent::__construct(); 
  9. /** 
  10. * Creates a XML representation of a Product 
  11. * 
  12. * @access public 
  13. * @param mixed $product 
  14. * @return mixed 
  15. */ 
  16. public function create($product) { 
  17. //$root = 'Article'; 
  18. $productNode = array(); 
  19. $sku = $product->get_sku(); 
  20. $tax = $product->get_price_including_tax() - $product->get_price_excluding_tax(); 
  21. $taxper = round($tax*100/$product->get_price_excluding_tax()); 
  22. if($sku) { 
  23. $productNode['item_no'] = $sku; 
  24. if($product->post->post_type == 'product_variation' && $product->post->post_parent != 0) { 
  25. $attr = wc_get_product_variation_attributes($product->id); 
  26. //logthis($attr); 
  27. $attribute = ''; 
  28. foreach($attr as $key => $value) { 
  29. $attribute .= $value.'-'; 
  30. $parentProductTitle = explode('#', $product->get_title()); 
  31. $title = $attribute.$parentProductTitle[1]; 
  32. $productNode['title'] = (strlen($title) > 40) ? substr($title, 0, 36).'...' : $title; 
  33. }else{ 
  34. $productNode['title'] = (strlen($product->get_title()) > 40) ? substr($product->get_title(), 0, 36).'...' : $product->get_title(); 
  35. //$productNode['title'] = (strlen($product->get_title()) > 40) ? substr($product->get_title(), 0, 36).'...' : $product->get_title(); 
  36. $productDesc = strip_tags(get_post($product->id)->post_content); 
  37. $productNode['description'] = (strlen($productDesc) > 200) ? substr($productDesc, 0, 196).'...' : $productDesc; 
  38. $productNode['price'] = $product->get_price_excluding_tax() ? round($product->get_price_excluding_tax(), 2) : $product->get_regular_price(); 
  39. $productNode['vat'] = $taxper; 
  40. //$productNode['QuantityInStock'] = $product->get_stock_quantity(); 
  41. $productNode['unit'] = 'unit'; 
  42. //$productNode['ArticleNumber'] = $product->get_sku(); 
  43. return $productNode; 
  44. //return $this->generate($root, $productNode); 
  45. /** 
  46. * Updates a XML representation of a Product 
  47. * 
  48. * @access public 
  49. * @param mixed $product 
  50. * @return mixed 
  51. */ 
  52. public function update($product) {  
  53.  
  54. //$root = 'Article'; 
  55. $productNode = array(); 
  56.  
  57. $tax = $product->get_price_including_tax() - $product->get_price_excluding_tax(); 
  58. $taxper = round($tax*100/$product->get_price_excluding_tax()); 
  59. if($product->post->post_type == 'product_variation' && $product->post->post_parent != 0) { 
  60. $attr = wc_get_product_variation_attributes($product->id); 
  61. //logthis($attr); 
  62. $attribute = ''; 
  63. foreach($attr as $key => $value) { 
  64. $attribute .= $value.'-'; 
  65. $parentProductTitle = explode('#', $product->get_title()); 
  66. $title = $attribute.$parentProductTitle[1]; 
  67. $productNode['title'] = (strlen($title) > 40) ? substr($title, 0, 36).'...' : $title; 
  68. }else{ 
  69. $productNode['title'] = (strlen($product->get_title()) > 40) ? substr($product->get_title(), 0, 36).'...' : $product->get_title(); 
  70. $productDesc = strip_tags(get_post($product->id)->post_content); 
  71. $productNode['description'] = (strlen($productDesc) > 200) ? substr($productDesc, 0, 196).'...' : $productDesc; 
  72. $productNode['price'] = $product->get_price_excluding_tax() ? round($product->get_price_excluding_tax(), 2) : $product->get_regular_price(); 
  73. $productNode['vat'] = $taxper; 
  74. //$productNode['ArticleNumber'] = $product->get_sku(); 
  75. //$productNode['QuantityInStock'] = $product->get_stock_quantity(); 
  76. $productNode['unit'] = 'unit'; 
  77. return $productNode; 
  78. //return $this->generate($root, $productNode); 
  79.  
  80. /** 
  81. * Creates a XML representation of a Product 
  82. * 
  83. * @access public 
  84. * @param mixed $product 
  85. * @return mixed 
  86. */ 
  87. public function create_price($product) { 
  88.  
  89. $root = 'Price'; 
  90. $options = get_option('woocommerce_billogram_general_settings'); 
  91. $price = array(); 
  92.  
  93. if(!isset($meta['pricelist_id'])) { 
  94. $price['PriceList'] = 'A'; 
  95. else{ 
  96. $price['PriceList'] = $meta['pricelist_id']; 
  97. if($options['activate-vat'] == 'on') { 
  98. $price['Price'] = $product->get_price_excluding_tax() ? $product->get_price_excluding_tax() : $product->get_regular_price(); 
  99. logthis('YES'); 
  100. else{ 
  101. $price['Price'] = $product->get_price_excluding_tax() ? $product->get_price_excluding_tax() : $product->get_regular_price(); 
  102. logthis('NO'); 
  103. $price['ArticleNumber'] = $product->get_sku(); 
  104. $price['FromQuantity'] = 1; 
  105.  
  106. return $this->generate($root, $price); 
  107.  
  108. /** 
  109. * Creates a XML representation of a Product 
  110. * 
  111. * @access public 
  112. * @param mixed $product 
  113. * @return mixed 
  114. */ 
  115. public function update_price($product) { 
  116.  
  117. $root = 'Price'; 
  118. $price = array(); 
  119.  
  120. $options = get_option('woocommerce_billogram_general_settings'); 
  121. if($options['activate-vat'] == 'on') { 
  122. $price['Price'] = $product->get_price_excluding_tax() ? $product->get_price_excluding_tax() : $product->get_regular_price(); 
  123. logthis('YES'); 
  124. else{ 
  125. $price['Price'] = $product->get_price_excluding_tax() ? $product->get_price_excluding_tax() : $product->get_regular_price(); 
  126. logthis('NO'); 
  127.  
  128. return $this->generate($root, $price); 
  129.  
  130.  
  131. /** 
  132. * Create product meta key value in well formated text, added in 2.1 
  133. * @access public 
  134. * @param mixed $order 
  135. * @return formated product meta key, value text. 
  136. */ 
  137. public function get_product_meta($metadata) { 
  138. //logthis($metadata); 
  139. $attribute = array(); 
  140. foreach ( $metadata as $meta_key => $meta_value ) { 
  141. // Skip hidden core fields 
  142. //logthis('item meta:'); 
  143. //logthis($meta_value); 
  144. if ( in_array( $meta_key, apply_filters( 'woocommerce_hidden_order_itemmeta', array( 
  145. '_qty',  
  146. '_tax_class',  
  147. '_product_id',  
  148. '_variation_id',  
  149. '_line_subtotal',  
  150. '_line_subtotal_tax',  
  151. '_line_total',  
  152. '_line_tax',  
  153. '_line_tax_data',  
  154. ) ) ) ) { 
  155. continue; 
  156.  
  157. // Skip serialised meta 
  158. if ( is_serialized( $meta_value ) ) { 
  159. continue; 
  160.  
  161. // Get attribute data 
  162. if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta_key ) ) ) { 
  163. $term = get_term_by( 'slug', $meta_value[0], wc_sanitize_taxonomy_name( $meta_key ) ); 
  164. $meta_key = wc_attribute_label( wc_sanitize_taxonomy_name( $meta_key ) ); 
  165. $meta_value[0] = isset( $term->name ) ? $term->name : $meta_value[0]; 
  166. } else { 
  167. $meta_key = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta_key, $_product ), $meta_key ); 
  168.  
  169. array_push($attribute, ' '.$meta_key . ': ' . $meta_value[0]); 
  170. //logthis('attribute'); 
  171. //logthis(explode(', ', $attribute)); 
  172. return implode(', ', $attribute); 
.