1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce Discontinued Products Template Functions |
4
|
|
|
* |
5
|
|
|
* @package woocommerce |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
if ( ! function_exists( 'dp_is_discontinued' ) ) { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Is discontiued. |
13
|
|
|
* Check if product is discontinued. |
14
|
|
|
* |
15
|
|
|
* @since 1.0.0 |
16
|
|
|
* @param int|null $product_id Optional. ID of the product to check. |
17
|
|
|
*/ |
18
|
|
|
function dp_is_discontinued( $product_id = null ) { |
|
|
|
|
19
|
|
|
|
20
|
|
|
global $post; |
21
|
|
|
if ( $post || null !== $product_id ) { |
22
|
|
|
$product_id = null !== $product_id ? $product_id : $post->ID; |
23
|
|
|
return has_term( (int) get_option( 'dp_discontinued_term' ), 'product_discontinued', $product_id ); |
24
|
|
|
} |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ( ! function_exists( 'dp_alt_products' ) ) { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Alternative Products. |
33
|
|
|
* Output buttons to alternative products. |
34
|
|
|
* |
35
|
|
|
* @since 1.0.0 |
36
|
|
|
*/ |
37
|
|
|
function dp_alt_products() { |
38
|
|
|
|
39
|
|
|
global $post; |
40
|
|
|
$alt_products = get_post_meta( $post->ID, '_alt_products', true ); |
41
|
|
|
$alt_products = is_array( $alt_products ) ? $alt_products : array(); |
42
|
|
|
$notice = dp_alt_products_notice( $post->ID, empty( $alt_products ) ); |
43
|
|
|
?> |
44
|
|
|
<?php echo wp_kses( $notice, 'post' ); ?></h4> |
45
|
|
|
<?php |
46
|
|
|
foreach ( $alt_products as $alt_product ) { |
47
|
|
|
?> |
48
|
|
|
<a href="<?php echo esc_url( get_permalink( $alt_product ) ); ?>" class="button"><?php echo esc_html( get_the_title( $alt_product ) ); ?></a> |
49
|
|
|
<?php |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( ! function_exists( 'dp_alt_products_notice' ) ) { |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Alternative Products Notice. |
58
|
|
|
* Determin notice output for discontinued products based on settings. |
59
|
|
|
* |
60
|
|
|
* @since 1.1.0 |
61
|
|
|
* @param int $product_id ID of the product to check. |
62
|
|
|
* @param boolean $no_alt true or false if there are no alternative products. |
63
|
|
|
*/ |
64
|
|
|
function dp_alt_products_notice( $product_id, $no_alt ) { |
65
|
|
|
|
66
|
|
|
$prod_text_option = get_post_meta( $product_id, '_discontinued_product_text', true ); |
67
|
|
|
$prod_alt_option = get_post_meta( $product_id, '_alt_product_text', true ); |
68
|
|
|
$text_option = get_option( 'dp_discontinued_text' ); |
69
|
|
|
$alt_option = get_option( 'dp_alt_text' ); |
70
|
|
|
$text = dp_alt_products_text( $prod_text_option, $text_option, __( 'This product has been discontinued.', 'discontinued-products' ) ); |
71
|
|
|
$alt = dp_alt_products_text( $prod_alt_option, $alt_option, __( 'You may be interested in:', 'discontinued-products' ) ); |
72
|
|
|
$notice = $no_alt ? '<h4 class="discontinued-notice">' . esc_html( $text ) . '</H4>' : '<h4 class="discontinued-notice">' . esc_html( $text ) . '</H4><h4 class="discontinued-notice-alt">' . esc_html( $alt ) . '</H4>'; |
73
|
|
|
return $notice; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ( ! function_exists( 'dp_alt_products_text' ) ) { |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Alternative Products Text. |
81
|
|
|
* Determin text for discontinued products based on settings. |
82
|
|
|
* |
83
|
|
|
* @since 1.1.0 |
84
|
|
|
* @param string $product_text product meta text. |
85
|
|
|
* @param string $option_text options settings text. |
86
|
|
|
* @param string $default_text default text. |
87
|
|
|
*/ |
88
|
|
|
function dp_alt_products_text( $product_text, $option_text, $default_text ) { |
89
|
|
|
|
90
|
|
|
$text = $product_text ? $product_text : ( $option_text ? $option_text : $default_text ); |
91
|
|
|
return $text; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ( ! function_exists( 'is_dp_shop' ) ) { |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Is_shop - Returns true when viewing the product type archive (shop). |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
function is_dp_shop() { |
103
|
|
|
return ( is_page( (int) get_option( 'dp_shop_page_id' ) ) ); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
add_filter( 'woocommerce_variable_sale_price_html', 'discontinued_template_loop_price', 10, 2 ); |
108
|
|
|
add_filter( 'woocommerce_variable_price_html', 'discontinued_template_loop_price', 10, 2 ); |
109
|
|
|
add_filter( 'woocommerce_get_price_html', 'discontinued_template_loop_price', 10, 2 ); |
110
|
|
|
|
111
|
|
|
if ( ! function_exists( 'discontinued_template_loop_price' ) ) { |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Discontinued_template_loop_price - replaces price with discontinued text. |
115
|
|
|
* Replaces price in admin with "discontinued". |
116
|
|
|
* |
117
|
|
|
* @param int $price Product price. |
118
|
|
|
* @param object $product Product object. |
119
|
|
|
* @return null |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
function discontinued_template_loop_price( $price, $product ) { |
122
|
|
|
$product_id = $product->get_id(); |
123
|
|
|
if ( ! is_single() && dp_is_discontinued( $product_id ) ) { |
124
|
|
|
if ( is_admin() ) { |
125
|
|
|
return 'Discontinued'; |
126
|
|
|
} |
127
|
|
|
$prod_text_option = get_post_meta( $product_id, '_discontinued_product_text', true ); |
128
|
|
|
$text_option = get_option( 'dp_discontinued_text' ); |
129
|
|
|
$text = dp_alt_products_text( $prod_text_option, $text_option, __( 'This product has been discontinued.', 'discontinued-products' ) ); |
130
|
|
|
$price = $price . '<br>' . $text; |
131
|
|
|
} |
132
|
|
|
return $price; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
add_filter( 'woocommerce_is_purchasable', 'discontinued_is_purchasable', 100, 2 ); |
136
|
|
|
|
137
|
|
|
if ( ! function_exists( 'discontinued_is_purchasable' ) ) { |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set is_purchasable to false if product is discontinued. |
141
|
|
|
* |
142
|
|
|
* @param int $is_purchasable Product is_purchasable. |
143
|
|
|
* @param object $product Product object. |
144
|
|
|
* @return null |
|
|
|
|
145
|
|
|
*/ |
146
|
|
|
function discontinued_is_purchasable( $is_purchasable, $product ) { |
147
|
|
|
if ( dp_is_discontinued( $product->get_id() ) ) { |
148
|
|
|
$is_purchasable = false; |
149
|
|
|
} |
150
|
|
|
return $is_purchasable; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
add_filter( 'woocommerce_product_price_class', 'discontinued_template_price_class', 10, 1 ); |
155
|
|
|
|
156
|
|
|
if ( ! function_exists( 'discontinued_template_price_class' ) ) { |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Discontinued_template_price_class - Add "discontinued" to the price class. |
160
|
|
|
* |
161
|
|
|
* @param int $class Product price css class. |
162
|
|
|
* @return null |
|
|
|
|
163
|
|
|
*/ |
164
|
|
|
function discontinued_template_price_class( $class ) { |
165
|
|
|
$class = $class . ' discontinued'; |
166
|
|
|
|
167
|
|
|
return $class; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.