WCShareASaleMerchantTracking

WooCommerce ShareASale Merchant Tracking Class.

Defined (1)

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

/woocommerce-shareasale-merchant-tracking.php  
  1. class WCShareASaleMerchantTracking { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. function __construct() { 
  7.  
  8. // Plugin Details 
  9. $this->plugin = new stdClass; 
  10. $this->plugin->name = 'woocommerce-shareasale-merchant-tracking'; 
  11. $this->plugin->displayName = 'WC ShareASale - Merchant'; 
  12. $this->plugin->version = '1.0.4'; 
  13. $this->plugin->folder = plugin_dir_path( __FILE__ ); 
  14. $this->plugin->url = plugin_dir_url( __FILE__ ); 
  15.  
  16. // Dashboard Submodule 
  17. if ( ! class_exists( 'WPCubeDashboardWidget' ) ) { 
  18. require_once( $this->plugin->folder . '/_modules/dashboard/dashboard.php' ); 
  19. $dashboard = new WPCubeDashboardWidget( $this->plugin );  
  20.  
  21. // Hooks 
  22. add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); 
  23. add_action( 'admin_notices', array( &$this, 'admin_notices') ); 
  24. add_action( 'woocommerce_thankyou', array( &$this, 'tracking_code' ) ); 
  25. add_action( 'plugins_loaded', array( &$this, 'load_language_files' ) ); 
  26.  
  27.  
  28. /** 
  29. * Register the plugin settings panel 
  30. * @since 1.0.0 
  31. */ 
  32. function admin_menu() { 
  33.  
  34. add_menu_page( $this->plugin->displayName, $this->plugin->displayName, 'manage_options', $this->plugin->name, array( &$this, 'admin_screen' ), 'dashicons-cart' ); 
  35.  
  36.  
  37. /** 
  38. * Outputs Administration Notices if no merchant ID specified 
  39. * @since 1.0.0 
  40. */ 
  41. function admin_notices() { 
  42.  
  43. // Don't display on settings screen 
  44. if ( isset( $_GET['page'] ) && $_GET['page'] == $this->plugin->name ) { 
  45. return; 
  46.  
  47. // Check settings for merchant ID 
  48. $settings = get_option( $this->plugin->name ); 
  49. if ( ! isset( $settings['merchantID'] ) || empty( $settings['merchantID'] ) ) { 
  50. echo ( '<div class="error"><p>' . __( 'WooCommerce ShareASale Merchant Tracking requires your merchant ID before it can start tracking sales. <a href="admin.php?page=' . $this->plugin->name . '" class="button">Do this now</a>', $this->plugin->name ) . '</p></div>' ); 
  51.  
  52.  
  53. /** 
  54. * Output the Administration Screens 
  55. * Save POSTed data from the Administration Panel into a WordPress option 
  56. * @since 1.0.0 
  57. */ 
  58. function admin_screen() { 
  59.  
  60. // Save Settings 
  61. if ( isset( $_POST['submit'] ) ) { 
  62. // Check nonce 
  63. if ( ! isset( $_POST[ $this->plugin->name . '_nonce' ] ) ) { 
  64. // Missing nonce  
  65. $this->errorMessage = __( 'nonce field is missing. Settings NOT saved.', $this->plugin->name ); 
  66. } elseif ( ! wp_verify_nonce( $_POST[ $this->plugin->name . '_nonce' ], $this->plugin->name ) ) { 
  67. // Invalid nonce 
  68. $this->errorMessage = __( 'Invalid nonce specified. Settings NOT saved.', $this->plugin->name ); 
  69. } else {  
  70. if ( isset( $_POST[ $this->plugin->name ] ) ) { 
  71. update_option( $this->plugin->name, $_POST[ $this->plugin->name ] ); 
  72. $this->message = __( 'Settings Updated.', $this->plugin->name ); 
  73.  
  74. // Get latest settings 
  75. $this->settings = get_option( $this->plugin->name ); 
  76.  
  77. // Load Settings Form 
  78. include_once( $this->plugin->folder . 'views/settings.php' );  
  79.  
  80. /** 
  81. * Loads plugin textdomain 
  82. */ 
  83. function load_language_files() { 
  84.  
  85. load_plugin_textdomain( $this->plugin->name, false, $this->plugin->name . '/languages/' ); 
  86.  
  87.  
  88. /** 
  89. * Adds ShareASale tracking code to the WooCommerce thank you page 
  90. * @since 1.0.0 
  91. * @param int $orderID Order ID 
  92. */ 
  93. function tracking_code( $orderID ) { 
  94.  
  95. $order = new WC_Order($orderID); 
  96.  
  97. // Check if a merchant ID has been set 
  98. $settings = get_option( $this->plugin->name ); 
  99. if ( ! isset( $order ) || is_wp_error( $order ) ) { 
  100. return; 
  101. if ( ! isset( $settings['merchantID'] ) ) { 
  102. return; 
  103.  
  104. // Calculate total, excluding taxes 
  105. $total = 0; 
  106. $items = $order->get_items(); 
  107. foreach ( $items as $item ) { 
  108. $total += $item['line_total']; 
  109.  
  110. // Deduct discounts 
  111. $discounts = $order->get_order_discount(); 
  112.  
  113. // Final Total (ex. tax + after discounts) 
  114. $total = $total - $discounts; 
  115.  
  116. $tracked = get_post_meta( $orderID, $this->plugin->name, true ); 
  117. if ( empty( $tracked ) ) { 
  118. echo ( '<img src="https://shareasale.com/sale.cfm?amount=' . $total . '&tracking=' . $orderID . '&transtype=sale&merchantID=' . $settings['merchantID'] . '" width="1" height="1">' ); 
  119. update_post_meta( $orderID, $this->plugin->name, 1 ); 
  120.  
  121.