-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwoocommerce-template.php
169 lines (146 loc) · 5.28 KB
/
woocommerce-template.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* WooCommerce Discontinued Products Template Functions
*
* @package woocommerce
* @since 1.0.0
*/
if ( ! function_exists( 'dp_is_discontinued' ) ) {
/**
* Is discontiued.
* Check if product is discontinued.
*
* @since 1.0.0
* @param int|null $product_id Optional. ID of the product to check.
*/
function dp_is_discontinued( $product_id = null ) {
global $post;
if ( $post || null !== $product_id ) {
$product_id = null !== $product_id ? $product_id : $post->ID;
return has_term( 'dp-discontinued', 'product_discontinued', $product_id );
}
return false;
}
}
if ( ! function_exists( 'dp_alt_products' ) ) {
/**
* Alternative Products.
* Output buttons to alternative products.
*
* @since 1.0.0
*/
function dp_alt_products() {
global $post;
$alt_products = get_post_meta( $post->ID, '_alt_products', true );
$alt_products = is_array( $alt_products ) ? $alt_products : array();
$notice = dp_alt_products_notice( $post->ID, empty( $alt_products ) );
?>
<?php echo wp_kses( $notice, 'post' ); ?></h4>
<?php
foreach ( $alt_products as $alt_product ) {
?>
<a href="<?php echo esc_url( get_permalink( $alt_product ) ); ?>" class="button"><?php echo esc_html( get_the_title( $alt_product ) ); ?></a>
<?php
}
}
}
if ( ! function_exists( 'dp_alt_products_notice' ) ) {
/**
* Alternative Products Notice.
* Determin notice output for discontinued products based on settings.
*
* @since 1.1.0
* @param int $product_id ID of the product to check.
* @param boolean $no_alt true or false if there are no alternative products.
*/
function dp_alt_products_notice( $product_id, $no_alt ) {
$prod_text_option = get_post_meta( $product_id, '_discontinued_product_text', true );
$prod_alt_option = get_post_meta( $product_id, '_alt_product_text', true );
$text_option = get_option( 'dp_discontinued_text' );
$alt_option = get_option( 'dp_alt_text' );
$text = dp_alt_products_text( $prod_text_option, $text_option, __( 'This product has been discontinued.', 'discontinued-products' ) );
$alt = dp_alt_products_text( $prod_alt_option, $alt_option, __( 'You may be interested in:', 'discontinued-products' ) );
$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>';
return $notice;
}
}
if ( ! function_exists( 'dp_alt_products_text' ) ) {
/**
* Alternative Products Text.
* Determin text for discontinued products based on settings.
*
* @since 1.1.0
* @param string $product_text product meta text.
* @param string $option_text options settings text.
* @param string $default_text default text.
*/
function dp_alt_products_text( $product_text, $option_text, $default_text ) {
$text = $product_text ? $product_text : ( $option_text ? $option_text : $default_text );
return $text;
}
}
if ( ! function_exists( 'is_dp_shop' ) ) {
/**
* Is_shop - Returns true when viewing the product type archive (shop).
*
* @return bool
*/
function is_dp_shop() {
return ( is_page( (int) get_option( 'dp_shop_page_id' ) ) );
}
}
add_filter( 'woocommerce_variable_sale_price_html', 'discontinued_template_loop_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'discontinued_template_loop_price', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'discontinued_template_loop_price', 10, 2 );
if ( ! function_exists( 'discontinued_template_loop_price' ) ) {
/**
* Discontinued_template_loop_price - replaces price with discontinued text.
* Replaces price in admin with "discontinued".
*
* @param int $price Product price.
* @param object $product Product object.
* @return null
*/
function discontinued_template_loop_price( $price, $product ) {
$product_id = $product->get_id();
if ( ! is_single() && dp_is_discontinued( $product_id ) ) {
if ( is_admin() ) {
return 'Discontinued';
}
$prod_text_option = get_post_meta( $product_id, '_discontinued_product_text', true );
$text_option = get_option( 'dp_discontinued_text' );
$text = dp_alt_products_text( $prod_text_option, $text_option, __( 'This product has been discontinued.', 'discontinued-products' ) );
$price = $price . '<br>' . $text;
}
return $price;
}
}
add_filter( 'woocommerce_is_purchasable', 'discontinued_is_purchasable', 100, 2 );
if ( ! function_exists( 'discontinued_is_purchasable' ) ) {
/**
* Set is_purchasable to false if product is discontinued.
*
* @param int $is_purchasable Product is_purchasable.
* @param object $product Product object.
* @return null
*/
function discontinued_is_purchasable( $is_purchasable, $product ) {
if ( dp_is_discontinued( $product->get_id() ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}
}
add_filter( 'woocommerce_product_price_class', 'discontinued_template_price_class', 10, 1 );
if ( ! function_exists( 'discontinued_template_price_class' ) ) {
/**
* Discontinued_template_price_class - Add "discontinued" to the price class.
*
* @param int $class Product price css class.
* @return null
*/
function discontinued_template_price_class( $class ) {
$class = $class . ' discontinued';
return $class;
}
}