WCPBC_Frontend

WCPBC_Frontend.

Defined (1)

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

/includes/class-wcpbc-frontend.php  
  1. class WCPBC_Frontend { 
  2.  
  3. /** 
  4. * Hook actions and filters 
  5. */ 
  6. public static function init() {  
  7.  
  8. add_action( 'woocommerce_init', array( __CLASS__ , 'check_test_mode'), 10 ); 
  9.  
  10. add_action( 'woocommerce_init', array( __CLASS__ , 'check_manual_country_widget'), 20 );  
  11.  
  12. add_action( 'woocommerce_init', array( __CLASS__ , 'checkout_country_update'), 20 );  
  13.  
  14. add_action( 'wp_enqueue_scripts', array( __CLASS__ , 'load_scripts' ) );  
  15.  
  16. add_filter( 'woocommerce_shipping_methods', array( __CLASS__ , 'overwrite_free_shipping_class' ) ); 
  17. }  
  18.  
  19. /** 
  20. * Check test mode 
  21. */  
  22. public static function check_test_mode() { 
  23.  
  24. if ( get_option('wc_price_based_country_test_mode', 'no') === 'yes' && $test_country = get_option('wc_price_based_country_test_country') ) { 
  25.  
  26. wcpbc_set_woocommerce_country( $test_country ); 
  27.  
  28. /** add test store message */ 
  29. add_action( 'wp_footer', array( __CLASS__, 'test_store_message' ) ); 
  30. }  
  31.  
  32. /** 
  33. * Return test store message  
  34. */ 
  35. public static function test_store_message() { 
  36. echo '<p class="demo_store">' . __( 'This is a demo store for testing purposes', 'wc-price-based-country') . '</p>'; 
  37.  
  38. /** 
  39. * Check manual country widget 
  40. */  
  41. public static function check_manual_country_widget() { 
  42.  
  43. if ( isset( $_POST['wcpbc-manual-country'] ) && $_POST['wcpbc-manual-country'] ) {  
  44.  
  45. wcpbc_set_woocommerce_country( wc_clean( $_POST['wcpbc-manual-country'] ) );  
  46.  
  47. add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_frontend_script' ), 5 ); 
  48. }  
  49.  
  50. /** 
  51. * Add scripts 
  52. */ 
  53. public static function load_scripts( ) { 
  54.  
  55. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  56.  
  57. wp_register_script( 'wc-price-based-country-frontend', WCPBC()->plugin_url() . 'assets/js/wcpbc-frontend' . $suffix . '.js', array( 'wc-cart-fragments' ), WCPBC()->version, true );  
  58. wp_enqueue_script( 'wc-price-based-country-frontend' ); 
  59.  
  60. if ( is_checkout() ) {  
  61.  
  62. if ( version_compare( WC()->version, '2.4', '<' ) ) { 
  63. $version = '-2.3'; 
  64. } else { 
  65. $version = ''; 
  66.  
  67. wp_enqueue_script( 'wc-price-based-country-checkout', WCPBC()->plugin_url() . 'assets/js/wcpbc-checkout' . $version . $suffix . '.js', array( 'wc-checkout', 'wc-price-based-country-frontend' ), WCPBC()->version, true ); 
  68.  
  69. /** 
  70. * Localize frontend script. 
  71. */ 
  72. public static function localize_frontend_script() { 
  73.  
  74. wp_localize_script( 'wc-price-based-country-frontend', 'wcpbc_frontend_params', array('refresh_cart' => 'true' ) ); 
  75.  
  76. /** 
  77. * Update WCPBC Customer country when order review is update 
  78. */ 
  79. public static function checkout_country_update( $post_data = array() ) {  
  80.  
  81. if ( defined( 'WC_DOING_AJAX' ) && WC_DOING_AJAX && isset( $_GET['wc-ajax'] ) && 'update_order_review' == $_GET['wc-ajax'] ) { 
  82.  
  83. if ( isset( $_POST['country'] ) ) { 
  84. WC()->customer->set_country( $_POST['country'] ); 
  85.  
  86. if ( wc_ship_to_billing_address_only() ) { 
  87. if ( isset( $_POST['country'] ) ) { 
  88. WC()->customer->set_shipping_country( $_POST['country'] ); 
  89. } else { 
  90. if ( isset( $_POST['s_country'] ) ) { 
  91. WC()->customer->set_shipping_country( $_POST['s_country'] ); 
  92. }  
  93. }  
  94.  
  95. /** 
  96. * Replace WooCommerce Free Shipping Method 
  97. */ 
  98. public static function overwrite_free_shipping_class( $shipping_methods ) { 
  99.  
  100. include_once( 'class-wcpbc-shipping-free-shipping.php' ); 
  101.  
  102. $methods = array(); 
  103.  
  104. foreach ( $shipping_methods as $method ) { 
  105. if ( $method === 'WC_Shipping_Free_Shipping' ) { 
  106.  
  107. $methods[] = 'WCPBC_Shipping_Free_Shipping'; 
  108. } else { 
  109. $methods[] = $method; 
  110.  
  111. return $methods;