-
Notifications
You must be signed in to change notification settings - Fork 1
/
woocommerce-simple-product-badge.php
70 lines (59 loc) · 2.71 KB
/
woocommerce-simple-product-badge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
Plugin Name: WooCommerce Simple Product Badge
Version: 0.1.0
Description: Display custom badge on WooCommerce products
Author: Pragmatic Mates s.r.o.
Author URI: http://pragmaticmates.com
Text Domain: woocommerce-simple-product-badge
Domain Path: /languages/
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/**
* New badge fields in admin
*/
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_simple_product_badge_fields' );
function woocommerce_simple_product_badge_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(array(
'id' => '_woocommerce_simple_product_badge_title',
'label' => __( 'Badge Title', 'woocommerce-simple-product-badge' ),
'description' => __( 'e.g. Recommended', 'woocommerce-simple-product-badge' ),
) );
woocommerce_wp_text_input(array(
'id' => '_woocommerce_simple_product_badge_class',
'label' => __( 'Badge Class', 'woocommerce-simple-product-badge' ),
'description' => __( 'e.g. background-green', 'woocommerce-simple-product-badge' ),
) );
echo '</div>';
}
/**
* Save custom fields values
*/
add_action( 'woocommerce_process_product_meta', 'woocommerce_simple_product_badge_fields_save' );
function woocommerce_simple_product_badge_fields_save( $post_id ) {
$title = $_POST['_woocommerce_simple_product_badge_title'];
$class = $_POST['_woocommerce_simple_product_badge_class'];
if ( !empty( $title ) ) {
update_post_meta( $post_id, '_woocommerce_simple_product_badge_title', esc_attr( $title ) );
}
if ( !empty( $class ) ) {
update_post_meta( $post_id, '_woocommerce_simple_product_badge_class', esc_attr( $class ) );
}
}
/**
* Display product badge
*/
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_simple_product_badge_display', 30 );
function woocommerce_simple_product_badge_display() {
$title = get_post_meta( get_the_ID(), '_woocommerce_simple_product_badge_title', true );
$class = get_post_meta( get_the_ID(), '_woocommerce_simple_product_badge_class', true );
if ( !empty( $title ) ) {
$class = !empty( $class ) ? $class : '';
echo '<span class="wc-simple-product-badge ' . $class . '">' . $title . '</span>';
}
}
}