Advertisement
Guest User

Global Variable Question

a guest
May 30th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.06 KB | None | 0 0
  1.  
  2. <?php
  3. /*
  4. Plugin Name: WC Simple Product Badge
  5. Version: 1.1
  6. Description: Display a simple product badge overlay for a WooCommerce store product.
  7. Author: Designs 4 The Web
  8. Author URI: http://designs4theweb.com
  9. Text Domain: wc-simple-product-badge
  10. License: GNU General Public License v3.0
  11. License URI: http://www.gnu.org/licenses/gpl-3.0.html
  12. */
  13.  
  14. // Define the plugin path as a constant
  15. define('WC_PRODUCT_BADGE_PLUGIN_PATH', WP_PLUGIN_URL . '/' . plugin_basename( dirname(__FILE__) ) . '/' );
  16.  
  17. // Hook the stylesheet to an action
  18.  
  19. add_action( 'wp_enqueue_scripts', 'wc_simple_product_badge_stylesheet' );
  20.  
  21. //  Enqueue the stylesheet
  22.  
  23. function wc_simple_product_badge_stylesheet() {
  24.     wp_register_style( 'wc-simple-product-badge-style', WC_PRODUCT_BADGE_PLUGIN_PATH.'css/style.css' );
  25.     wp_enqueue_style( 'wc-simple-product-badge-style' );
  26. }
  27.  
  28. // Check to see that WooCommerce is installed and active
  29.  
  30. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  31.    
  32. // Create 'badge', 'class', and 'duration' fields in the General panel of WooCommerce product options
  33.  
  34. add_action( 'woocommerce_product_options_general_product_data', 'wc_simple_product_badge_fields' );
  35.  
  36.     function wc_simple_product_badge_fields() {
  37.        
  38.         global $woocommerce, $post;
  39.  
  40.         echo '<div class="options_group">';
  41.  
  42.         // Badge Title Field
  43.         woocommerce_wp_text_input(array(
  44.             'id'          => '_wc_simple_product_badge_title',
  45.             'label'       => __( 'Badge Title', 'wc-simple-product-badge' ),
  46.             'description' => __( 'e.g. New Product', 'wc-simple-product-badge' ),
  47.         ) );
  48.  
  49.         // Badge Class Field
  50.         woocommerce_wp_text_input(array(
  51.             'id'          => '_wc_simple_product_badge_class',
  52.             'label'       => __( 'CSS Class', 'wc-simple-product-badge' ),
  53.             'description' => __( 'e.g. orange, yellow, green, blue, purple, black', 'wc-simple-product-badge' ),
  54.         ) );
  55.        
  56.         // Badge Duration Field
  57.         woocommerce_wp_text_input(array(
  58.             'id'                => '_wc_simple_product_badge_duration',
  59.             'label'             => __( 'Duration', 'wc-simple-product-badge' ),
  60.             'description'       => __( 'e.g. 14', 'wc-simple-product-badge' ),
  61.             'type'              => 'number',
  62.         ) );
  63.        
  64.         // Badge on Single Page Checkbox Field
  65.         woocommerce_wp_checkbox(array(
  66.             'id'                => '_wc_simple_product_badge_single_page_option',
  67.             'label'             => __( 'Show on Single Product Page?', 'wc-simple-product-badge' ),
  68.         ) );       
  69.  
  70.         echo '</div>';
  71.     }
  72.  
  73. // Save custom field values
  74.  
  75. add_action( 'woocommerce_process_product_meta', 'wc_simple_product_badge_fields_save' );
  76.     function wc_simple_product_badge_fields_save( $post_id ) {
  77.         $title = $_POST['_wc_simple_product_badge_title'];
  78.         $class = $_POST['_wc_simple_product_badge_class'];
  79.         $duration = $_POST['_wc_simple_product_badge_duration'];
  80.         $single_opt = isset( $_POST['_wc_simple_product_badge_single_page_option'] ) ? 'yes' : 'no';
  81.  
  82.         update_post_meta( $post_id, '_wc_simple_product_badge_title', esc_attr( $title ) );
  83.         update_post_meta( $post_id, '_wc_simple_product_badge_class', esc_attr( $class ) );
  84.         update_post_meta( $post_id, '_wc_simple_product_badge_duration', esc_attr( $duration ) );
  85.         update_post_meta( $post_id, '_wc_simple_product_badge_single_page_option', esc_attr( $single_opt ) );
  86.     }  
  87.    
  88. // Display the product badge on the shop page
  89.  
  90. add_action( 'woocommerce_after_shop_loop_item_title', 'wc_simple_product_badge_display_shop', 30 );
  91.     function wc_simple_product_badge_display_shop() {
  92.         $title = get_post_meta( get_the_ID(), '_wc_simple_product_badge_title', true ); // badge title
  93.         $class = get_post_meta( get_the_ID(), '_wc_simple_product_badge_class', true ); // badge class
  94.         $duration = get_post_meta( get_the_ID(), '_wc_simple_product_badge_duration', true ); // badge duration
  95.         $postdate = get_the_time( 'Y-m-d' ); // post date
  96.         $postdatestamp = strtotime( $postdate ); // post date in unix timestamp
  97.         $difference = round ((time() - $postdatestamp) / (24*60*60));  // difference in days between now and product's post date
  98.  
  99.         if ( !empty( $title ) && empty( $duration ) || !empty( $title ) && $difference <= $duration ){ // Check to see if there is a title and the product is still within the duration timeframe if specified
  100.             $class = !empty( $class ) ? $class : '';
  101.             echo '<span class="wc_simple_product_badge ' . $class . '">' . $title . '</span>';
  102.         }
  103.     }
  104.    
  105. // Display the product badge on the single page
  106.    
  107. add_filter( 'woocommerce_single_product_image_html', 'wc_simple_product_badge_display_single' );   
  108.     function wc_simple_product_badge_display_single( $img_html ) {
  109.         $title = get_post_meta( get_the_ID(), '_wc_simple_product_badge_title', true ); // badge title
  110.         $class = get_post_meta( get_the_ID(), '_wc_simple_product_badge_class', true ); // badge class
  111.         $duration = get_post_meta( get_the_ID(), '_wc_simple_product_badge_duration', true ); // badge duration
  112.         $single_opt = get_post_meta( get_the_ID(), '_wc_simple_product_badge_single_page_option', true ); // badge on single page
  113.         $postdate = get_the_time( 'Y-m-d' ); // post date
  114.         $postdatestamp = strtotime( $postdate ); // post date in unix timestamp
  115.         $difference = round ((time() - $postdatestamp) / (24*60*60));  // difference in days between now and product's post date
  116.        
  117.         if ( !empty( $title ) && empty( $duration ) && $single_opt === 'yes' || !empty( $title ) && $difference <= $duration && $single_opt === 'yes' ){ // Check to see if there is a title and the product is still within the duration timeframe ()if specified) and the checkbox is checked to show on single page view
  118.             $class = !empty( $class ) ? $class : '';
  119.             echo '<span class="wc_simple_product_badge ' . $class . '">' . $title . '</span>';
  120.             return $img_html;
  121. }
  122.  
  123.         elseif ( $single_opt === 'no' ) { // Check to see if the checkbox is unchecked to show on single page view
  124.             return $img_html;
  125.         }
  126.  
  127.     }
  128.  
  129. }
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement