WooCommerce_Product_Badge

The WooCommerce Product Badge WooCommerce Product Badge class.

Defined (1)

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

/wc-product-badge.php  
  1. class WooCommerce_Product_Badge{ 
  2.  
  3. var $textdomain; 
  4. var $options_product_badge; 
  5. var $saved_options_product_badge; 
  6.  
  7. /** 
  8. * Gets things started by adding an action to initialize this plugin once 
  9. * WooCommerce is known to be active and initialized 
  10. */ 
  11. public function __construct() { 
  12. load_plugin_textdomain('wc-product-badge', false, dirname(plugin_basename(__FILE__)) . '/languages/'); 
  13.  
  14. $this->textdomain = 'wc-product-badge'; 
  15.  
  16. $this->options_product_badge = array( 
  17. 'product_badge_enabled' => '',  
  18. 'product_badge_display_position' => '',  
  19. 'product_badge_enabled_new' => '',  
  20. 'product_badge_new_days' => '30',  
  21. 'product_badge_enabled_sale' => '',  
  22. 'product_badge_enabled_featured' => '' 
  23. ); 
  24.  
  25. $this->display_positions = array( 'default' => __( 'Default', $this->textdomain ), 'after_title' => __( 'After Product Title', $this->textdomain ), 'after_price' => __( 'After Product Price', $this->textdomain ), 'after_short_desc' => __( 'After Short Desc', $this->textdomain ), 'after_meta' => __( 'After SKU, Categories & Tags', $this->textdomain )); 
  26.  
  27. $this->saved_options_product_badge = array(); 
  28.  
  29. add_action('woocommerce_init', array(&$this, 'init')); 
  30.  
  31. // load admin css 
  32. add_action( 'admin_enqueue_scripts', array(&$this, 'wpb_load_admin_css' ) ); 
  33.  
  34. // load admin css 
  35. add_action( 'wp_enqueue_scripts', array(&$this, 'wpb_load_frontend_css') ); 
  36.  
  37. /** 
  38. * Initialize extension when WooCommerce is active 
  39. */ 
  40. public function init() { 
  41. global $product; 
  42.  
  43. //add menu link for the plugin (backend) 
  44. add_action( 'admin_menu', array( &$this, 'add_menu_product_badge' ) ); 
  45.  
  46. if(get_option('product_badge_enabled')) 
  47. //add_action( 'woocommerce_before_shop_loop_item', array( &$this, 'woocommerce_show_product_loop_badge_new' ), 30 ); // The new badge function 
  48. //add_action( 'woocommerce_before_shop_loop_item_title', array( &$this, 'woocommerce_show_product_loop_badge_new' ), 30 ); // The new badge function 
  49. //add_action( 'woocommerce_after_shop_loop_item', array( &$this, 'woocommerce_show_product_loop_badge_new' ), 30 ); // The new badge function 
  50.  
  51. //add sharing media at product summary page  
  52. $default_position = get_option('product_badge_display_position'); 
  53.  
  54. $position_index = 101; 
  55. switch($default_position) 
  56. case 'default': 
  57. $position_index = 101; 
  58. break; 
  59. case 'after_title': 
  60. $position_index = 8; 
  61. break; 
  62. case 'after_price': 
  63. $position_index = 15; 
  64. break;  
  65. case 'after_short_desc': 
  66. $position_index = 30; 
  67. break; 
  68. case 'after_meta': 
  69. $position_index = 45; 
  70. break; 
  71.  
  72. if( get_option('product_badge_enabled_new') ) { 
  73. add_action( 'woocommerce_before_shop_loop_item', array( &$this, 'woocommerce_show_product_loop_badge_new' ), 30 ); // The new badge function 
  74. add_action( 'woocommerce_single_product_summary', array(&$this, 'woocommerce_show_product_loop_badge_new' ), $position_index ); 
  75.  
  76. if( get_option('product_badge_enabled_featured') ) { 
  77. add_action( 'woocommerce_before_shop_loop_item', array( &$this, 'woocommerce_show_product_loop_badge_featured' ), 35 ); // The featured badge function 
  78. add_action( 'woocommerce_single_product_summary', array(&$this, 'woocommerce_show_product_loop_badge_featured' ), $position_index ); 
  79.  
  80. if( get_option('product_badge_enabled_sale') ) { 
  81. add_action( 'woocommerce_before_shop_loop_item', array( &$this, 'woocommerce_show_product_loop_badge_sale' ), 20 );  
  82. add_action( 'woocommerce_single_product_summary', array(&$this, 'woocommerce_show_product_loop_badge_sale' ), $position_index ); // The sale badge function 
  83.  
  84.  
  85.  
  86. /** 
  87. * Load admin stylesheets 
  88. */ 
  89. function wpb_load_admin_css() { 
  90. wp_register_style( 'wc-product-badge-admin-stylesheet', plugins_url('/assets/css/admin-styles.css', __FILE__) ); 
  91. wp_enqueue_style( 'wc-product-badge-admin-stylesheet' ); 
  92.  
  93. /** 
  94. * Load frontend stylesheets 
  95. */ 
  96. function wpb_load_frontend_css() { 
  97. wp_register_style( 'wc-product-badge-stylesheet', plugins_url('/assets/css/style.css', __FILE__) ); 
  98. wp_enqueue_style( 'wc-product-badge-stylesheet' ); 
  99.  
  100. // Display the New badge 
  101. function woocommerce_show_product_loop_badge_new() { 
  102. $dateposted = get_the_time( 'Y-m-d' ); // Post date 
  103. $timestampposted = strtotime( $dateposted ); 
  104. $new_days = get_option( 'product_badge_new_days' );  
  105. //echo 'new days'.$new_days; 
  106. if( (time() - ( 60 * 60 * 24 * $new_days ) ) < $timestampposted ) { 
  107. echo '<h3 class="product-badge product-badge-new">'.__( 'New', $this->textdomain ).'</h3>'; 
  108. //echo '<div class="ribbon-wrapper-green"><div class="ribbon-green">NEW</div></div>'; 
  109. }  
  110.  
  111. // Display the Featured badge 
  112. function woocommerce_show_product_loop_badge_featured() { 
  113. global $product; 
  114. if($product->is_featured()) 
  115. echo '<h3 class="product-badge product-badge-featured">'.__( 'Featured', $this->textdomain ).'</h3>'; 
  116.  
  117. // Display the Sale badge 
  118. function woocommerce_show_product_loop_badge_sale() { 
  119. global $product; 
  120. if($product->sale_price != '') { 
  121. $sale_price = $product->sale_price; 
  122. $regular_price = $product->regular_price; 
  123.  
  124. $save_percentage = round((($regular_price - $sale_price) / $regular_price) * 100); 
  125. if($regular_price != '' && $save_percentage != '') 
  126. echo '<h3 class="product-badge product-badge-sale">'.__( 'Sale', $this->textdomain ).' ('.__( 'Save', $this->textdomain ).' '.$save_percentage.'%)</h3>'; 
  127.  
  128. /** 
  129. * Add a menu link to the woocommerce section menu 
  130. */ 
  131. function add_menu_product_badge() { 
  132. $wc_page = 'woocommerce'; 
  133. $comparable_settings_page = add_submenu_page( $wc_page , __( 'Product Badge', $this->textdomain ), __( 'Product Badge', $this->textdomain ), 'manage_options', 'wc-product-badge', array( 
  134. &$this,  
  135. 'settings_page_product_badge' 
  136. )); 
  137.  
  138. /** 
  139. * Create the settings page content 
  140. */ 
  141. public function settings_page_product_badge() { 
  142.  
  143. // If form was submitted 
  144. if ( isset( $_POST['submitted'] ) ) 
  145. check_admin_referer( $this->textdomain ); 
  146.  
  147. $this->saved_options_product_badge['product_badge_enabled'] = ! isset( $_POST['product_badge_enabled'] ) ? '1' : $_POST['product_badge_enabled']; 
  148. $this->saved_options_product_badge['product_badge_display_position'] = ! isset( $_POST['product_badge_display_position'] ) ? '' : $_POST['product_badge_display_position']; 
  149. $this->saved_options_product_badge['product_badge_enabled_new'] = ! isset( $_POST['product_badge_enabled_new'] ) ? '1' : $_POST['product_badge_enabled_new']; 
  150. $this->saved_options_product_badge['product_badge_new_days'] = ! isset( $_POST['product_badge_new_days'] ) ? '30' : $_POST['product_badge_new_days']; 
  151. $this->saved_options_product_badge['product_badge_enabled_sale'] = ! isset( $_POST['product_badge_enabled_sale'] ) ? '1' : $_POST['product_badge_enabled_sale']; 
  152. $this->saved_options_product_badge['product_badge_enabled_featured'] = ! isset( $_POST['product_badge_enabled_featured'] ) ? '1' : $_POST['product_badge_enabled_featured']; 
  153.  
  154. foreach($this->options_product_badge as $field => $value) 
  155. $option_product_badge = get_option( $field ); 
  156.  
  157. if($option_product_badge != $this->saved_options_product_badge[$field]) 
  158. update_option( $field, $this->saved_options_product_badge[$field] ); 
  159.  
  160. // Show message 
  161. echo '<div id="message" class="updated fade"><p>' . __( 'You have saved WooCommerce Product Badge options.', $this->textdomain ) . '</p></div>'; 
  162.  
  163. $product_badge_enabled = get_option( 'product_badge_enabled' ); 
  164. $product_badge_display_position = get_option( 'product_badge_display_position' ); 
  165. $product_badge_enabled_new = get_option( 'product_badge_enabled_new' ); 
  166. $product_badge_new_days = get_option( 'product_badge_new_days' ) ? get_option( 'product_badge_new_days' ) : '30'; 
  167. $product_badge_enabled_sale = get_option( 'product_badge_enabled_sale' ); 
  168. $product_badge_enabled_featured = get_option( 'product_badge_enabled_featured' ); 
  169.  
  170. $checked_enabled = ''; 
  171. $checked_enabled_new = ''; 
  172. $checked_enabled_sale = ''; 
  173. $checked_enabled_featured = ''; 
  174.  
  175. if($product_badge_enabled) 
  176. $checked_enabled = 'checked="checked"'; 
  177.  
  178. if($product_badge_enabled_new) 
  179. $checked_enabled_new = 'checked="checked"'; 
  180.  
  181. if($product_badge_enabled_sale) 
  182. $checked_enabled_sale = 'checked="checked"'; 
  183.  
  184. if($product_badge_enabled_featured) 
  185. $checked_enabled_featured = 'checked="checked"'; 
  186.  
  187. $actionurl = $_SERVER['REQUEST_URI']; 
  188. $nonce = wp_create_nonce( $this->textdomain ); 
  189.  
  190. $this->options = $this->get_options(); 
  191.  
  192.  
  193. // Configuration Page 
  194.  
  195. ?> 
  196. <div class="wrap"> 
  197. <div class="plugin-container"> 
  198. <div class="plugin-column plugin-primary"> 
  199.  
  200. <h2>WooCommerce Product Badge</h2> 
  201. <h4></h4> 
  202.  
  203.  
  204. <form id="plugin_settings" method="post" action="<?php echo $actionurl; ?>"> 
  205. <?php settings_fields( 'plugin_product_badge' ); ?> 
  206.  
  207. <h3><?php _e('Settings'); ?></h3> 
  208.  
  209. <table class="form-table"> 
  210.  
  211. <tr valign="top"> 
  212. <th scope="row" width="35%"><?php _e( 'Enable', $this->textdomain ); ?></td> 
  213. <td> 
  214. <input class="checkbox" name="product_badge_enabled" id="product_badge_enabled" value="0" type="hidden"> 
  215. <input class="checkbox" name="product_badge_enabled" id="product_badge_enabled" value="1" type="checkbox" <?php echo $checked_enabled; ?>> 
  216. </td> 
  217. </tr> 
  218. <tr valign="top"> 
  219. <th scope="row" width="35%"><?php _e( 'Display Position (Product Page)', $this->textdomain ); ?></td> 
  220. <td> 
  221. <select name="product_badge_display_position"> 
  222. <?php foreach($this->display_positions as $option => $option_name): ?> 
  223. <?php if($option == $product_badge_display_position): ?> 
  224. <option selected="selected" value="<?php echo $option; ?>"><?php echo $option_name; ?></option> 
  225. <?php else: ?> 
  226. <option value="<?php echo $option; ?>"><?php echo $option_name; ?></option> 
  227. <?php endif; ?> 
  228. <?php endforeach; ?> 
  229. </select> 
  230. </td> 
  231. </tr> 
  232. <tr valign="top"> 
  233. <th scope="row" width="35%"><?php _e( 'Show "New" Badge', $this->textdomain ); ?></td> 
  234. <td> 
  235. <input class="checkbox" name="product_badge_enabled_new" id="product_badge_enabled_new" value="0" type="hidden"> 
  236. <input class="checkbox" name="product_badge_enabled_new" id="product_badge_enabled_new" value="1" type="checkbox" <?php echo $checked_enabled_new; ?>> 
  237. </td> 
  238. </tr> 
  239. <tr valign="top"> 
  240. <th scope="row" width="35%"><?php _e( 'New Product with How Many Days?', $this->textdomain ); ?></td> 
  241. <td> 
  242. <input name="product_badge_new_days" type="text" id="product_badge_new_days" value="<?php echo $product_badge_new_days; ?>" /> 
  243. </td> 
  244. </tr> 
  245. <tr valign="top"> 
  246. <th scope="row" width="35%"><?php _e( 'Show "Sale" Badge', $this->textdomain ); ?></td> 
  247. <td> 
  248. <input class="checkbox" name="product_badge_enabled_sale" id="product_badge_enabled_sale" value="0" type="hidden"> 
  249. <input class="checkbox" name="product_badge_enabled_sale" id="product_badge_enabled_sale" value="1" type="checkbox" <?php echo $checked_enabled_sale; ?>> 
  250. </td> 
  251. </tr> 
  252. <tr valign="top"> 
  253. <th scope="row" width="35%"><?php _e( 'Show "Featured" Badge', $this->textdomain ); ?></td> 
  254. <td> 
  255. <input class="checkbox" name="product_badge_enabled_featured" id="product_badge_enabled_featured" value="0" type="hidden"> 
  256. <input class="checkbox" name="product_badge_enabled_featured" id="product_badge_enabled_featured" value="1" type="checkbox" <?php echo $checked_enabled_featured; ?>> 
  257. </td> 
  258. </tr> 
  259.  
  260.  
  261.  
  262. </table> 
  263.  
  264. <input class="button-primary" type="submit" name="Save" value="<?php _e('Save Options', $this->textdomain); ?>" id="submitbutton" /> 
  265. <input type="hidden" name="submitted" value="1" />  
  266. <input type="hidden" id="_wpnonce" name="_wpnonce" value="<?php echo $nonce; ?>" /> 
  267.  
  268. </form> 
  269.  
  270. </div> 
  271.  
  272. <!-- Start plugin Sidebar --> 
  273. <div class="plugin-column plugin-sidebar"> 
  274.  
  275. <div class="plugin-box"> 
  276. <h3 class="plugin-title"><?php _e( 'Donate $10, $20 or $30', $this->textdomain ); ?></h3> 
  277. <p><?php _e( 'If you like this plugin and find it is helpful, consider supporting it by donating a token of your appreciation.', $this->textdomain ); ?></p> 
  278.  
  279. <div class="plugin-donate"> 
  280. <form class="donate" action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
  281. <input type="hidden" name="cmd" value="_donations"> 
  282. <input type="hidden" name="business" value="terry@terrytsang.com"> 
  283. <input type="hidden" name="lc" value="US"> 
  284. <input type="hidden" name="item_name" value="WordPress Plugin Development by Terry Tsang"> 
  285. <input type="hidden" name="item_number" value="woocommerce-product-badge"> 
  286. <input type="hidden" name="currency_code" value="USD"> 
  287. <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted"> 
  288. <button name="submit" class="button-primary"><?php _e( 'Donate with PayPal', $this->textdomain ); ?></button> 
  289. <img alt="" border="0" src="https://www.paypalobjects.com/nl_NL/i/scr/pixel.gif" width="1" height="1"> 
  290. </form> 
  291. </div> 
  292.  
  293. <p><?php _e( 'Some other ways to support this plugin', $this->textdomain ); ?></p> 
  294. <ul class="ul-square"> 
  295. <li><a href="http://wordpress.org/support/view/plugin-reviews/woocommerce-product-badge?rate=5#postform" target="_blank"><?php printf( __( 'Leave a %s review on WordPress.org', $this->textdomain ), '★★★★★' ); ?></a></li> 
  296. <li><a href="http://shop.terrytsang.com" target="_blank"><?php _e( 'Link to the plugin page from your blog', $this->textdomain ); ?></a></li> 
  297. <li><a href="http://twitter.com/intent/tweet/?text=<?php echo urlencode('I am using WooCommerce Product Badge free plugin to show "New", "Sale" or "Featured" badge on my product page. It\'s great!'); ?>&via=terrytsang811&url=<?php echo urlencode('http://wordpress.org/plugins/woocommerce-product-badge/'); ?>" target="_blank">Tweet about WooCommerce Product Badge </a></li> 
  298. <li><a href="http://wordpress.org/plugins/woocommerce-product-badge/#compatibility"><?php _e( 'Vote "works" on the WordPress.org plugin page', $this->textdomain ); ?></a></li> 
  299. </ul> 
  300. </div> 
  301.  
  302. <div class="plugin-box"> 
  303. <h3 class="plugin-title"><?php _e( 'Looking for support?', $this->textdomain ); ?></h3> 
  304. <p><?php printf( __( 'Please use the %splugin support forums%s on WordPress.org.', $this->textdomain ), '<a href="http://wordpress.org/support/plugin/woocommerce-product-badge">', '</a>' ); ?></p> 
  305. </div> 
  306.  
  307. <div class="plugin-box"> 
  308.  
  309. <div class="plugin-box"> 
  310. <h3 class="plugin-title">About the developer</h3> 
  311. <p>My name is <a href="http://terrytsang.com/">Terry Tsang</a>. I love developing WordPress plugins which help your business to grow.</p> 
  312. <p>Take a look at my other <a href="http://shop.terrytsang.com/">plugins for WordPress & WooCommerce</a> or <em>like</em> my Facebook page to stay updated.</p> 
  313. <p><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2FecommercePlugins&width&layout=standard&action=like&show_faces=true&share=false&appId=225994527565061" scrolling="no" frameborder="0" style="border:none; width: 100%; overflow:hidden; height: 80px;" allowTransparency="true"></iframe></p> 
  314. <p>You can also follow me on twitter <a href="http://twitter.com/terrytsang811">here</a>.</p> 
  315. </div> 
  316. </div> 
  317. <!-- End plugin Sidebar --> 
  318.  
  319. <br style="clear:both; " /> 
  320. </div> 
  321. </div> 
  322.  
  323. <?php 
  324.  
  325. /** 
  326. * Get the setting options 
  327. */ 
  328. function get_options() { 
  329.  
  330. foreach($this->options_product_badge as $field => $value) 
  331. $array_options[$field] = get_option( $field ); 
  332.  
  333. return $array_options; 
  334.  
  335.  
  336. }//end class