WC_Darwin_Pricing

Darwin Pricing Integration.

Defined (1)

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

/includes/class-wc-darwin-pricing.php  
  1. class WC_Darwin_Pricing extends WC_Integration { 
  2.  
  3. /** 
  4. * Init and hook in the integration. 
  5. * @return void 
  6. */ 
  7. public function __construct() { 
  8. $this->id = 'darwin_pricing'; 
  9. $this->method_title = __('Darwin Pricing', 'woocommerce-darwin-pricing-integration'); 
  10. $this->method_description = __('Darwin Pricing is a dynamic pricing software that provides real-time market monitoring, pricing optimization and a geo-targeted coupon box to eCommerce websites.', 'woocommerce-darwin-pricing-integration'); 
  11.  
  12. // Load the settings. 
  13. $this->init_form_fields(); 
  14. $this->init_settings(); 
  15.  
  16. // Define user set variables 
  17. $this->dp_id = $this->get_option('dp_id'); 
  18. $this->dp_host = $this->get_option('dp_host'); 
  19.  
  20. // Actions 
  21. add_action('woocommerce_update_options_integration_darwin_pricing', array($this, 'process_admin_options')); 
  22.  
  23. // Tracking code 
  24. add_action('wp_head', array($this, 'darwin_pricing_code_display'), 999999); 
  25.  
  26. /** 
  27. * Initialise Settings Form Fields 
  28. * @return void 
  29. */ 
  30. public function init_form_fields() { 
  31.  
  32. $this->form_fields = array( 
  33. 'dp_host' => array( 
  34. 'title' => __('API Server', 'woocommerce-darwin-pricing-integration'),  
  35. 'description' => __('Log into your Darwin Pricing account to find the URL of your API server, e.g. <code>https://api.darwinpricing.com</code>', 'woocommerce-darwin-pricing-integration'),  
  36. 'type' => 'text',  
  37. 'default' => 'https://api.darwinpricing.com' 
  38. ),  
  39. 'dp_id' => array( 
  40. 'title' => __('Site ID', 'woocommerce-darwin-pricing-integration'),  
  41. 'description' => __('Log into your Darwin Pricing account to find the ID of your website, e.g. <code>123</code>', 'woocommerce-darwin-pricing-integration'),  
  42. 'type' => 'text',  
  43. 'default' => '' 
  44. ); 
  45.  
  46. /** 
  47. * Display the tracking code 
  48. * @return string 
  49. */ 
  50. public function darwin_pricing_code_display() { 
  51. global $wp; 
  52. $tracking = false; 
  53.  
  54. if ('' == $this->dp_id || '' == $this->dp_host) { 
  55. return; 
  56.  
  57. // Track orders 
  58. if (is_order_received_page()) { 
  59. $order_id = isset($wp->query_vars['order-received']) ? $wp->query_vars['order-received'] : 0; 
  60.  
  61. if (0 < $order_id && 1 != get_post_meta($order_id, '_dp_tracked', true)) { 
  62. $tracking = true; 
  63. echo $this->get_tracking_code($order_id); 
  64.  
  65. if (!$tracking) { 
  66. echo $this->get_widget_code(); 
  67.  
  68. /** 
  69. * Darwin Pricing widget 
  70. * @return string 
  71. */ 
  72. protected function get_widget_code() { 
  73. $url = $this->dp_host . '/widget?site-id=' . $this->dp_id; 
  74. return <<<EOD 
  75. <script> 
  76. (function(s, n) { 
  77. s = document.createElement('script'); 
  78. s.async = 1; 
  79. s.src = '{$url}'; 
  80. n = document.getElementsByTagName('script')[0]; 
  81. n.parentNode.insertBefore(s, n); 
  82. })(); 
  83. </script> 
  84. EOD; 
  85.  
  86. /** 
  87. * Darwin Pricing tracking 
  88. * @param int $order_id 
  89. * @return string 
  90. */ 
  91. protected function get_tracking_code($order_id) { 
  92. $order = new WC_Order($order_id); 
  93. $currency = $order->get_order_currency(); 
  94. $value = $order->get_total() - $order->get_total_tax(); 
  95.  
  96. // Mark the order as tracked 
  97. update_post_meta($order_id, '_dp_tracked', 1); 
  98.  
  99. $url = $this->dp_host . '/add-payment?site-id=' . $this->dp_id . '&profit=' . $currency . $value; 
  100. return '<script src="' . esc_url($url) . '" type="text/javascript"></script>'; 
  101.