/accepted-payment-methods.php

  1. <?php 
  2. /** 
  3. Plugin Name: WooCommerce Accepted Payment Methods 
  4. Plugin URI: http://jameskoster.co.uk/tag/accepted-payment-methods/ 
  5. Version: 0.7.0 
  6. Description: Allows you display which payment methods your online store accepts. 
  7. Author: jameskoster 
  8. Tested up to: 4.0 
  9. Author URI: http://jameskoster.co.uk 
  10. Text Domain: woocommerce-accepted-payment-methods 
  11. Domain Path: /languages/ 
  12.   
  13. License: GNU General Public License v3.0 
  14. License URI: http://www.gnu.org/licenses/gpl-3.0.html 
  15. */ 
  16.  
  17. /** 
  18. * Localisation 
  19. */ 
  20. load_plugin_textdomain( 'woocommerce-accepted-payment-methods', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 
  21.  
  22. /** 
  23. * Check if WooCommerce is active 
  24. **/ 
  25. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
  26.  
  27. /** 
  28. * Accepted Payment Methods class 
  29. **/ 
  30. if ( ! class_exists( 'WC_apm' ) ) { 
  31.  
  32. class WC_apm { 
  33.  
  34. public function __construct() { 
  35.  
  36. // Init settings 
  37. $this->settings = array( 
  38. array( 
  39. 'name' => __( 'Accepted Payment Methods', 'woocommerce-accepted-payment-methods' ),  
  40. 'type' => 'title',  
  41. 'desc' => sprintf( __( 'To display the selected payment methods you can use the built in widget, the %s shortcode or the %s template tag.', 'woocommerce-accepted-payment-methods' ), '<code>[woocommerce_accepted_payment_methods]</code>', '<code><?php wc_accepted_payment_methods(); ?></code>' ),  
  42. 'id' => 'wc_apm_options' 
  43. ),  
  44. array( 
  45. 'name' => __( 'American Express', 'woocommerce-accepted-payment-methods' ),  
  46. 'desc' => __( 'Display the American Express logo', 'woocommerce-accepted-payment-methods' ),  
  47. 'id' => 'wc_apm_american_express',  
  48. 'type' => 'checkbox' 
  49. ),  
  50. array( 
  51. 'name' => __( 'Bitcoin', 'woocommerce-accepted-payment-methods' ),  
  52. 'desc' => __( 'Display the Bitcoin logo', 'woocommerce-accepted-payment-methods' ),  
  53. 'id' => 'wc_apm_bitcoin',  
  54. 'type' => 'checkbox' 
  55. ),  
  56. array( 
  57. 'name' => __( 'Cash on Delivery', 'woocommerce-accepted-payment-methods' ),  
  58. 'desc' => __( 'Display Cash on Delivery symbol', 'woocommerce-accepted-payment-methods' ),  
  59. 'id' => 'wc_apm_cash_on_delivery',  
  60. 'type' => 'checkbox' 
  61. ),  
  62. array( 
  63. 'name' => __( 'Dankort', 'woocommerce-accepted-payment-methods' ),  
  64. 'desc' => __( 'Display the Dankort logo', 'woocommerce-accepted-payment-methods' ),  
  65. 'id' => 'wc_apm_dankort',  
  66. 'type' => 'checkbox' 
  67. ),  
  68. array( 
  69. 'name' => __( 'Discover', 'woocommerce-accepted-payment-methods' ),  
  70. 'desc' => __( 'Display the Discover logo', 'woocommerce-accepted-payment-methods' ),  
  71. 'id' => 'wc_apm_discover',  
  72. 'type' => 'checkbox' 
  73. ),  
  74. array( 
  75. 'name' => __( 'Google', 'woocommerce-accepted-payment-methods' ),  
  76. 'desc' => __( 'Display the Google logo', 'woocommerce-accepted-payment-methods' ),  
  77. 'id' => 'wc_apm_google',  
  78. 'type' => 'checkbox' 
  79. ),  
  80. array( 
  81. 'name' => __( 'Maestro', 'woocommerce-accepted-payment-methods' ),  
  82. 'desc' => __( 'Display the Maestro logo', 'woocommerce-accepted-payment-methods' ),  
  83. 'id' => 'wc_apm_maestro',  
  84. 'type' => 'checkbox' 
  85. ),  
  86. array( 
  87. 'name' => __( 'MasterCard', 'woocommerce-accepted-payment-methods' ),  
  88. 'desc' => __( 'Display the MasterCard logo', 'woocommerce-accepted-payment-methods' ),  
  89. 'id' => 'wc_apm_mastercard',  
  90. 'type' => 'checkbox' 
  91. ),  
  92. array( 
  93. 'name' => __( 'PayPal', 'woocommerce-accepted-payment-methods' ),  
  94. 'desc' => __( 'Display the PayPal logo', 'woocommerce-accepted-payment-methods' ),  
  95. 'id' => 'wc_apm_paypal',  
  96. 'type' => 'checkbox' 
  97. ),  
  98. array( 
  99. 'name' => __( 'Visa', 'woocommerce-accepted-payment-methods' ),  
  100. 'desc' => __( 'Display the Visa logo', 'woocommerce-accepted-payment-methods' ),  
  101. 'id' => 'wc_apm_visa',  
  102. 'type' => 'checkbox' 
  103. ),  
  104. array( 'type' => 'sectionend', 'id' => 'wc_apm_options' ),  
  105. ); 
  106.  
  107. // Default options 
  108. add_option( 'wc_apm_label', '' ); 
  109. add_option( 'wc_apm_american_express', 'no' ); 
  110. add_option( 'wc_apm_google', 'no' ); 
  111. add_option( 'wc_apm_mastercard', 'no' ); 
  112. add_option( 'wc_apm_paypal', 'no' ); 
  113. add_option( 'wc_apm_visa', 'no' ); 
  114. add_option( 'wc_apm_discover', 'no' ); 
  115. add_option( 'wc_apm_bitcoin', 'no' ); 
  116. add_option( 'wc_apm_maestro', 'no' ); 
  117. add_option( 'wc_apm_cash_on_delivery', 'no' ); 
  118. add_option( 'wc_apm_dankort', 'no' ); 
  119.  
  120. // Admin 
  121. add_action( 'woocommerce_settings_checkout', array( $this, 'admin_settings' ), 20 ); 
  122. add_action( 'woocommerce_update_options_checkout', array( $this, 'save_admin_settings' ) ); 
  123. add_action( 'wp_enqueue_scripts', array( $this, 'setup_styles' ) ); 
  124.  
  125.  
  126.  
  127. /**-----------------------------------------------------------------------------------*/ 
  128. /** Class Functions */ 
  129. /**-----------------------------------------------------------------------------------*/ 
  130.  
  131. function admin_settings() { 
  132. woocommerce_admin_fields( $this->settings ); 
  133.  
  134. function save_admin_settings() { 
  135. woocommerce_update_options( $this->settings ); 
  136.  
  137. // Setup styles 
  138. function setup_styles() { 
  139. wp_enqueue_style( 'apm-styles', plugins_url( '/assets/css/style.css', __FILE__ ) ); 
  140.  
  141. $WC_apm = new WC_apm(); 
  142.  
  143. /** 
  144. * Frontend functions 
  145. */ 
  146. // Template tag 
  147. if ( ! function_exists( 'wc_accepted_payment_methods' ) ) { 
  148. function wc_accepted_payment_methods() { 
  149. $amex = get_option( 'wc_apm_american_express' ); 
  150. $google = get_option( 'wc_apm_google' ); 
  151. $mastercard = get_option( 'wc_apm_mastercard' ); 
  152. $paypal = get_option( 'wc_apm_paypal' ); 
  153. $visa = get_option( 'wc_apm_visa' ); 
  154. $discover = get_option( 'wc_apm_discover' ); 
  155. $bitcoin = get_option( 'wc_apm_bitcoin' ); 
  156. $maestro = get_option( 'wc_apm_maestro' ); 
  157. $cod = get_option( 'wc_apm_cash_on_delivery'); 
  158. $dankort = get_option( 'wc_apm_dankort'); 
  159.  
  160. // Display 
  161. echo '<ul class="accepted-payment-methods">'; 
  162. if ( $dankort == "yes" ) { echo '<li class="dankort"><span>Dankort</span></li>'; } 
  163. if ( $amex == "yes" ) { echo '<li class="american-express"><span>American Express</span></li>'; } 
  164. if ( $bitcoin == "yes" ) { echo '<li class="bitcoin"><span>Bitcoin</span></li>'; } 
  165. if ( $cod == "yes" ) { echo '<li class="cash-on-delivery"><span>Cash on Delivery</span></li>'; } 
  166. if ( $discover == "yes" ) { echo '<li class="discover"><span>Discover</span></li>'; } 
  167. if ( $google == "yes" ) { echo '<li class="google"><span>Google</span></li>'; } 
  168. if ( $maestro == "yes" ) { echo '<li class="maestro"><span>Maestro</span></li>'; } 
  169. if ( $mastercard == "yes" ) { echo '<li class="mastercard"><span>MasterCard</span></li>'; } 
  170. if ( $paypal == "yes" ) { echo '<li class="paypal"><span>PayPal</span></li>'; } 
  171. if ( $visa == "yes" ) { echo '<li class="visa"><span>Visa</span></li>'; } 
  172. echo '</ul>'; 
  173.  
  174. // The shortcode 
  175. add_shortcode( 'woocommerce_accepted_payment_methods', 'wc_accepted_payment_methods' ); 
  176.  
  177. // The widget 
  178. class Accepted_Payment_Methods extends WP_Widget { 
  179.  
  180. function Accepted_Payment_Methods() { 
  181. // Instantiate the parent object 
  182. parent::__construct( false, 'WooCommerce Accepted Payment Methods' ); 
  183.  
  184. function widget( $args, $instance ) { 
  185. // Widget output 
  186. extract( $args ); 
  187.  
  188. $title = apply_filters( 'widget_title', $instance['title'] ); 
  189.  
  190. echo $before_widget; 
  191. if ( ! empty( $title ) ) 
  192. echo $before_title . $title . $after_title; 
  193. wc_accepted_payment_methods(); 
  194. echo $after_widget; 
  195. /** 
  196. * Sanitize widget form values as they are saved. 
  197. */ 
  198. public function update( $new_instance, $old_instance ) { 
  199. $instance = array(); 
  200. $instance['title'] = strip_tags( $new_instance['title'] ); 
  201.  
  202. return $instance; 
  203.  
  204. /** 
  205. * Back-end widget form. 
  206. */ 
  207. public function form( $instance ) { 
  208. if ( isset( $instance[ 'title' ] ) ) { 
  209. $title = $instance[ 'title' ]; 
  210. else { 
  211. $title = __( 'Accepted Payment Methods', 'woocommerce-accepted-payment-methods' ); 
  212. ?> 
  213. <p> 
  214. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'woocommerce-accepted-payment-methods' ); ?></label> 
  215. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> 
  216. </p> 
  217. <p> 
  218. <?php _e( 'Configure which payment methods your store accepts in the', 'woocommerce-accepted-payment-methods' ); ?> <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=checkout' ); ?>"><?php _e( 'WooCommerce settings', 'woocommerce-accepted-payment-methods' ); ?></a>. 
  219. </p> 
  220. <?php 
  221.  
  222.  
  223. function apm_register_widgets() { 
  224. register_widget( 'Accepted_Payment_Methods' ); 
  225.  
  226. add_action( 'widgets_init', 'apm_register_widgets' ); 
  227.  
.