WCB_Order_XML_Document

The WooCommerce Billogram Integration WCB Order XML Document class.

Defined (1)

The class is defined in the following location(s).

/class-billogram2-order-xml.php  
  1. class WCB_Order_XML_Document extends WCB_XML_Document{ 
  2. /** 
  3. */ 
  4. function __construct() { 
  5. parent::__construct(); 
  6. /** 
  7. * Creates a n XML representation of an Order 
  8. * @access public 
  9. * @param mixed $arr 
  10. * @param $customerNumber 
  11. * @return mixed 
  12. */ 
  13. public function create($arr, $customerNumber) { 
  14.  
  15. $order_options = get_option('woocommerce_billogram_order_settings'); 
  16. $options = get_option('woocommerce_billogram_general_settings'); 
  17.  
  18. //Add for woosubscription support 
  19. if (class_exists("WC_Subscriptions_Order") && WC_Subscriptions_Order::order_contains_subscription( $arr )) 
  20. $subscription = WC_Subscriptions_Order::order_contains_subscription( $arr ); 
  21.  
  22. //$root = 'Order'; 
  23. $signKey = uniqid(); 
  24. $siteurl = admin_url('admin-ajax.php').'?action=billogram_callback'; 
  25. //$siteurl = plugins_url( '/woocommerce-billogram-integration/billogram-callback.php' ); 
  26. //logthis("siteurl: ". $siteurl); 
  27.  
  28. $order['invoice_date'] = substr($arr->order_date, 0, 10); 
  29. //$order['due_date'] = date("Y-m-d", strtotime($arr->order_date ." +15 day") ); 
  30. if($order_options['due-days'] != '') { 
  31. $order['due_days'] = $order_options['due-days']; 
  32. $order['currency'] = 'SEK'; 
  33. if($arr->billing_company) { 
  34. $order['customer']['name'] = $arr->billing_company; 
  35. }else{ 
  36. $order['customer']['name'] = $arr->billing_first_name . " " . $arr->billing_last_name; 
  37. $order['customer']['customer_no'] = $customerNumber; 
  38. $order['customer']['phone'] = $arr->billing_phone; 
  39. $order['customer']['address']['street_address'] = $arr->billing_address_1; 
  40. $order['customer']['address']['city'] = $arr->billing_city; 
  41. /**$order['customer']['address']['country'] = 'SE';*/ 
  42. /**$order['customer']['address']['country'] = $this->countries[$arr->billing_country];*/ 
  43. $order['customer']['address']['zipcode'] = $arr->billing_postcode; 
  44. $order['customer']['email'] = $arr->billing_email; 
  45. //$order['currency'] = $arr->get_order_currency(); 
  46. if($order_options['admin-fee'] != '') { 
  47. $order['invoice_fee'] = $order_options['admin-fee']; 
  48.  
  49. $invoicerows = array(); 
  50. //loop all items 
  51. $index = 0; 
  52. foreach($arr->get_items() as $item) { 
  53. /**$key = "items" . $index;*/ 
  54. $invoicerow = array(); 
  55. //logthis('item:'); 
  56. //logthis($item); 
  57.  
  58. //fetch product 
  59. //$pf = new WC_Product_Factory(); 
  60. $product = new WC_Product($productId); 
  61.  
  62. //if variable product there might be a different SKU 
  63. if(empty($item['variation_id'])) { 
  64. $productId = $item['product_id']; 
  65. $invoicerow['title'] = (strlen($item['name']) > 40) ? substr($item['name'], 0, 36).'...' : $item['name']; 
  66. //$description = $item['name']; 
  67. $productDesc = strip_tags(get_post($productId)->post_content); 
  68. $description = (strlen($productDesc) > 200) ? substr($productDesc, 0, 196).'...' : $productDesc; 
  69. else{ 
  70. $productId = $item['variation_id']; 
  71. $product_xml = new WCB_Product_XML_Document(); 
  72. //logthis($item['item_meta']); 
  73. $item_variation = $product_xml->get_product_meta($item['item_meta']); 
  74. //$_product = apply_filters( 'woocommerce_order_item_product', $arr->get_product_from_item( $item ), $item ); 
  75. //$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product ); 
  76. //$description = $item['name'].' - '.$item_meta->display($flat = true, $return = true); 
  77.  
  78. $invoicerow['title'] = (strlen($item['name']. ' ('. $item_variation . ')') > 40) ? substr($item['name']. ' ('. $item_variation . ')', 0, 36).'...' : $item['name']. ' ('. $item_variation.')'; 
  79. $productDesc = strip_tags(get_post($productId)->post_content); 
  80. $description = (strlen('('. $item_variation. ')'. $productDesc) > 200) ? substr('('. $item_variation. ')'. $productDesc, 0, 196).'...' : '('. $item_variation. ')'. $productDesc; 
  81.  
  82. $product = new WC_Product($productId); 
  83.  
  84. $invoicerow['description'] = $description; 
  85.  
  86. $invoicerow['price'] = round($item['line_subtotal']/$item['qty'], 2); 
  87.  
  88. $discount = $product->get_regular_price - $product->get_sale_price; 
  89. if($product->is_on_sale()) { 
  90. $invoicerow['discount'] = round($item['qty']*($discount), 2); 
  91. }else{ 
  92. $invoicerow['discount'] = 0; 
  93.  
  94. if(class_exists('WC_Subscriptions_Order')) { 
  95. if(WC_Subscriptions_Product::is_subscription( $productId )) { 
  96. if(WC_Subscriptions_Product::get_trial_length( $productId ) > 0 && $discount > $invoicerow['price']) { 
  97. logthis('Order line item is synced with price "0", because order line is a subscription with a free trial period!'); 
  98. $invoicerow['discount'] = 0; 
  99. $price_string = html_entity_decode(strip_tags(WC_Subscriptions_Product::get_price_string($productId))); 
  100.  
  101. if($product->is_on_sale()) { 
  102. $price = $product->get_sale_price(); 
  103. }else{ 
  104. $price = $product->get_regular_price(); 
  105.  
  106. $invoicerow['description'] = substr_replace($price_string, $price.html_entity_decode(get_woocommerce_currency_symbol()), 0, 2).' '.$invoicerow['description']; 
  107. $invoicerow['description'] = (strlen($invoicerow['description']) > 200) ? substr($invoicerow['description'], 0, 196).'...' : $invoicerow['description']; 
  108.  
  109.  
  110.  
  111. $tax = $product->get_price_including_tax() - $product->get_price_excluding_tax(); 
  112. if($tax > 0) { 
  113. $taxper = round($tax*100/$product->get_price_excluding_tax()); 
  114. $invoicerow['vat'] = $taxper; 
  115. }else{ 
  116. $invoicerow['vat'] = 0; 
  117. $invoicerow['count'] = $item['qty'];  
  118.  
  119. /**$index += 1;*/ 
  120. $invoicerows[] = $invoicerow; 
  121. if ($arr->get_total_shipping() > 0 ) { 
  122. $invoicerowShipping = array(); 
  123. $invoicerowShipping['title'] = 'Shipping and Handling: '.$arr->get_shipping_method() ; 
  124. $invoicerowShipping['price'] = $arr->get_total_shipping(); 
  125. $tax = $arr->get_shipping_tax(); 
  126. $taxper = round($tax*100/$arr->get_total_shipping()); 
  127. //echo $taxper; die(); 
  128. $invoicerowShipping['vat'] = $taxper; 
  129. $invoicerowShipping['count'] = 1; 
  130. //$invoicerowShipping['unit'] = 'unit'; 
  131. $invoicerows[] = $invoicerowShipping; 
  132. if (count( WC()->cart->applied_coupons ) > 0 ) { 
  133. //logthis(WC()->cart->applied_coupons); 
  134. $invoicerowDiscount = array(); 
  135. foreach (WC()->cart->applied_coupons as $code ) { 
  136. logthis('Coupon type: '.wc_get_coupon_type($code)); 
  137. $invoicerowDiscount['title'] = 'Coupon: '.$code; 
  138. $coupounAmount = WC()->cart->coupon_discount_amounts[ $code ]; 
  139. $coupounTaxAmount = WC()->cart->coupon_discount_tax_amounts[ $code ]; 
  140. $invoicerowDiscount['price'] = -round($coupounAmount, 2); 
  141. //$invoicerowDiscount['price'] = 0; 
  142. $tax = $coupounTaxAmount; 
  143. if($tax > 0) 
  144. $taxper = round($tax*100/$coupounAmount); 
  145. else 
  146. $taxper = 0; 
  147. $invoicerowDiscount['vat'] = $taxper; 
  148. //$invoicerowDiscount['vat'] = 0; 
  149. $invoicerowDiscount['count'] = 1; 
  150. //$invoicerow['unit'] = 'unit'; 
  151. $invoicerows[] = $invoicerowDiscount; 
  152.  
  153. $order['items'] = $invoicerows; 
  154. $order['callbacks']['sign_key'] = $signKey; 
  155. $order['callbacks']['url'] = $siteurl; 
  156. $order['info']['order_no'] = $arr->id; 
  157. $order['info']['order_date'] = substr($arr->order_date, 0, 10); 
  158. $order['info']['delivery_date'] = NULL; 
  159.  
  160. //logthis($order); 
  161.  
  162. return $order; 
  163.  
  164.  
  165.  
  166. /** 
  167. * Creates a n XML representation of an Order 
  168. * @access public 
  169. * @param mixed $arr 
  170. * @param $customerNumber 
  171. * @return mixed 
  172. */ 
  173. public function create_scheduled_subscription($amount_to_charge, $arr, $customerNumber) { 
  174.  
  175. $order_options = get_option('woocommerce_billogram_order_settings'); 
  176. $options = get_option('woocommerce_billogram_general_settings'); 
  177.  
  178. //$root = 'Order'; 
  179. $signKey = uniqid(); 
  180. $siteurl = admin_url('admin-ajax.php').'?action=billogram_callback_subscription'; 
  181. //$siteurl = plugins_url( '/woocommerce-billogram-integration/billogram-callback.php' ); 
  182. //logthis("siteurl: ". $siteurl); 
  183.  
  184.  
  185. $order['invoice_date'] = date('Y-m-d'); 
  186. //$order['invoice_date'] = WC_Subscriptions_Order::get_next_payment_date( $arr, $product_id, $from_date = '' ); 
  187.  
  188. //$order['invoice_date'] = substr($arr->order_date, 0, 10); 
  189. //$order['due_date'] = date("Y-m-d", strtotime($arr->order_date ." +15 day") ); 
  190. if($order_options['due-days'] != '') { 
  191. $order['due_days'] = $order_options['due-days']; 
  192. $order['currency'] = 'SEK'; 
  193. if($arr->billing_company) { 
  194. $order['customer']['name'] = $arr->billing_company; 
  195. }else{ 
  196. $order['customer']['name'] = $arr->billing_first_name . " " . $arr->billing_last_name; 
  197. $order['customer']['customer_no'] = $customerNumber; 
  198. $order['customer']['phone'] = $arr->billing_phone; 
  199. $order['customer']['address']['street_address'] = $arr->billing_address_1; 
  200. $order['customer']['address']['city'] = $arr->billing_city; 
  201. /**$order['customer']['address']['country'] = 'SE';*/ 
  202. /**$order['customer']['address']['country'] = $this->countries[$arr->billing_country];*/ 
  203. $order['customer']['address']['zipcode'] = $arr->billing_postcode; 
  204. $order['customer']['email'] = $arr->billing_email; 
  205. //$order['currency'] = $arr->get_order_currency(); 
  206. if($order_options['admin-fee'] != '') { 
  207. $order['invoice_fee'] = $order_options['admin-fee']; 
  208.  
  209. $invoicerows = array(); 
  210. //loop all items 
  211. $index = 0; 
  212. //logthis('Order object: '); 
  213. //logthis($arr); 
  214. foreach($arr->get_items() as $item) { 
  215. /**$key = "items" . $index;*/ 
  216.  
  217. //logthis('Order item: '); 
  218. //logthis($item)  
  219.  
  220. //if variable product there might be a different SKU 
  221. if(empty($item['variation_id'])) { 
  222. $productId = $item['product_id']; 
  223. //$description = $item['name']; 
  224. else{ 
  225. $productId = $item['variation_id']; 
  226. $_product = apply_filters( 'woocommerce_order_item_product', $arr->get_product_from_item( $item ), $item ); 
  227. $item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product ); 
  228. //$description = $item['name'].' - '.$item_meta->display($flat = true, $return = true); 
  229.  
  230. //logthis('Item Product ID'); 
  231. //logthis($productId); 
  232.  
  233. if(WC_Subscriptions_Product::get_price( $productId ) != '' ) { 
  234. $invoicerow = array(); 
  235. $product = new WC_Product($productId); 
  236.  
  237. //logthis('Item Product'); 
  238. //logthis($product); 
  239.  
  240. $productDesc = strip_tags(get_post($productId)->post_content); 
  241. $description = (strlen($productDesc) > 200) ? substr($productDesc, 0, 196).'...' : $productDesc; 
  242. $invoicerow['title'] = (strlen($item['name']) > 40) ? substr($item['name'], 0, 36).'...' : $item['name']; 
  243. if($product->post->post_type == 'product_variation' && $product->post->post_parent != 0) { 
  244. $attr = wc_get_product_variation_attributes($product->id); 
  245. //logthis($attr); 
  246. $attribute = ''; 
  247. foreach($attr as $key => $value) { 
  248. $attribute .= $value.'-'; 
  249. $parentProductTitle = explode('#', $product->get_title()); 
  250. $title = $attribute.$parentProductTitle[1]; 
  251. $invoicerow['title'] = (strlen($title) > 40) ? substr($title, 0, 36).'...' : $title; 
  252.  
  253. $invoicerow['price'] = round($item['line_subtotal']/$item['qty'], 2); 
  254.  
  255. $discount = $item['line_subtotal']-$item['line_total']; 
  256. if($product->is_on_sale() || $discount > 0) { 
  257. $invoicerow['discount'] = round($item['qty']*($discount), 2); 
  258. }else{ 
  259. $invoicerow['discount'] = 0; 
  260. $invoicerow['description'] = $description; 
  261.  
  262. $price_string = html_entity_decode(strip_tags(WC_Subscriptions_Product::get_price_string($productId)));  
  263.  
  264. $invoicerow['description'] .= substr_replace($price_string, $invoicerow['price'].html_entity_decode(get_woocommerce_currency_symbol()), 0, 2).' '.$invoicerow['description']; 
  265. $invoicerow['description'] = (strlen($invoicerow['description']) > 200) ? substr($invoicerow['description'], 0, 196).'...' : $invoicerow['description']; 
  266.  
  267. $tax = $item['line_tax']/$item['qty']; 
  268. if($tax > 0) { 
  269. $taxper = round($tax*100/$product->get_price_excluding_tax()); 
  270. $invoicerow['vat'] = $taxper; 
  271. }else{ 
  272. $invoicerow['vat'] = 0; 
  273. $invoicerow['count'] = $item['qty']; 
  274.  
  275.  
  276. /**$index += 1;*/ 
  277. $invoicerows[] = $invoicerow; 
  278. if ($arr->get_total_shipping() > 0 ) { 
  279. $invoicerowShipping = array(); 
  280. $invoicerowShipping['title'] = 'Shipping and Handling: '.$arr->get_shipping_method() ; 
  281. $invoicerowShipping['price'] = round($arr->get_total_shipping(), 2); 
  282. $tax = $arr->get_shipping_tax(); 
  283. $taxper = round($tax*100/$arr->get_total_shipping()); 
  284. //echo $taxper; die(); 
  285. $invoicerowShipping['vat'] = $taxper; 
  286. $invoicerowShipping['count'] = 1; 
  287. //$invoicerowShipping['unit'] = 'unit'; 
  288. $invoicerows[] = $invoicerowShipping; 
  289. if (count( WC()->cart->applied_coupons ) > 0 ) { 
  290. $invoicerowDiscount = array(); 
  291. foreach (WC()->cart->applied_coupons as $code ) { 
  292. $invoicerowDiscount['title'] = 'Coupon: '.$code; 
  293. $coupounAmount = WC()->cart->coupon_discount_amounts[ $code ]; 
  294. $coupounTaxAmount = WC()->cart->coupon_discount_tax_amounts[ $code ]; 
  295. //$invoicerowDiscount['price'] = -$coupounAmount; 
  296. $invoicerowDiscount['price'] = 0; 
  297. $tax = $coupounTaxAmount; 
  298. $taxper = round($tax*100/$coupounAmount); 
  299. //$invoicerowDiscount['vat'] = $taxper; 
  300. $invoicerowDiscount['vat'] = 0; 
  301. $invoicerowDiscount['count'] = 1; 
  302. //$invoicerow['unit'] = 'unit'; 
  303. $invoicerows[] = $invoicerowDiscount; 
  304.  
  305. $order['items'] = $invoicerows; 
  306. $order['callbacks']['sign_key'] = $signKey; 
  307. $order['callbacks']['url'] = $siteurl; 
  308. $order['info']['order_no'] = $arr->id; 
  309. $order['info']['order_date'] = substr($arr->order_date, 0, 10); 
  310. $order['info']['delivery_date'] = NULL; 
  311.  
  312. //logthis($order); 
  313.  
  314. return $order;