forked from datafeedr/datafeedr-woocommerce-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.php
284 lines (243 loc) · 8.76 KB
/
filters.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Exit if accessed directly
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* This filter takes the current WooCommerce Product, finds other Product Sets that
* it might be associated with and adds those Product Sets' category IDs to this
* product's array of $category_ids.
*
* @param array $category_ids
* @param Dfrpswc_Product_Update_Handler $updater
*
* @return array
*/
function dfrpswc_append_category_ids_from_other_product_sets( array $category_ids, Dfrpswc_Product_Update_Handler $updater ) {
$product_set_ids = get_post_meta( $updater->wc_product->get_id(), '_dfrps_product_set_id', false );
if ( ! $product_set_ids ) {
return $category_ids;
}
foreach ( $product_set_ids as $product_set_id ) {
$category_ids = array_merge( $category_ids, dfrps_get_cpt_terms( $product_set_id ) );
}
return $category_ids;
}
add_filter( 'dfrpswc_product_cat_category_ids', 'dfrpswc_append_category_ids_from_other_product_sets', 10, 2 );
/**
* This formats the price for WooCommerce products dependent on its currency code.
*
* The format used is from WooCommerce 4.9.1.
*
* The order of the {symbol} and the {price} is determined by the format
* set in the Dfrapi_Currency class therefore the formats in the 2 examples
* below might not be exactly what is output (symbol and price could be in
* a different order and there might be differences in spacing).
*
* ------------ NOT ON SALE FORMAT ------------
*
* <span class="woocommerce-Price-amount amount">
* <bdi>
* <span class="woocommerce-Price-currencySymbol">
* {symbol}
* </span>
* {price}
* </bdi>
* </span>
*
* ------------ ON SALE FORMAT ------------
*
* <del>
* <span class="woocommerce-Price-amount amount">
* <bdi>
* <span class="woocommerce-Price-currencySymbol">
* {symbol}
* </span>
* {price}
* </bdi>
* </span>
* </del>
* <ins>
* <span class="woocommerce-Price-amount amount">
* <bdi>
* <span class="woocommerce-Price-currencySymbol">
* {symbol}
* </span>
* {price}
* </bdi>
* </span>
* </ins>
*
* @param string $price
* @param WC_Product $product
*
* @return string
*/
function dfrpswc_woocommerce_get_price_html( string $price, WC_Product $product ) {
// If the "format_price" option is not set to "yes", return $price argument.
$options = dfrpswc_get_options();
if ( $options['format_price'] !== 'yes' ) {
return $price;
}
// If there is no price, return $price argument.
if ( '' === $product->get_price() ) {
return $price;
}
// If there is no currency code, return $price argument.
if ( ! $currency = dfrps_get_product_field( $product->get_id(), 'currency' ) ) {
return $price;
}
// Formats to use in sprintf() calls to generate HTML.
$sale_format = apply_filters( 'dfrpswc_woocommerce_get_price_html_sale_format', '<del>%s</del> <ins>%s</ins>' );
$wrapper_format = apply_filters( 'dfrpswc_woocommerce_get_price_html_wrapper_format', '<span class="woocommerce-Price-amount amount"><bdi>%s</bdi></span>' );
$symbol_format = apply_filters( 'dfrpswc_woocommerce_get_price_html_symbol_format', '<span class="woocommerce-Price-currencySymbol">%s</span>' );
$price_format = apply_filters( 'dfrpswc_woocommerce_get_price_html_price_format', '%s' );
// Get a Dfrapi_Price object for this $product's regular price.
$regular_price = dfrapi_price( $product->get_regular_price(), $currency, 'woocommerce_get_price_html' );
// Format the regular price in the necessary HTML.
$formatted_regular_price = sprintf( $wrapper_format, strtr( $regular_price->get_signed_format(), [
'{symbol}' => sprintf( $symbol_format, $regular_price->currency->get_currency_symbol() ),
'{price}' => sprintf( $price_format, $regular_price->get_formatted_number() ),
] ) );
if ( $product->is_on_sale() ) {
// Get a Dfrapi_Price object for this $product's sale price.
$sale_price = dfrapi_price( $product->get_sale_price(), $currency, 'woocommerce_get_price_html' );
// Format the sale price in the necessary HTML.
$formatted_sale_price = sprintf( $wrapper_format, strtr( $sale_price->get_signed_format(), [
'{symbol}' => sprintf( $symbol_format, $sale_price->currency->get_currency_symbol() ),
'{price}' => sprintf( $price_format, $sale_price->get_formatted_number() ),
] ) );
// Return the regular and sale prices HTML in the $sale_format.
return sprintf( $sale_format, $formatted_regular_price, $formatted_sale_price );
}
// Return the regular price HTML.
return $formatted_regular_price;
}
add_filter( 'woocommerce_get_price_html', 'dfrpswc_woocommerce_get_price_html', 11, 2 );
/**
* Add Datafeedr WooCommerce Impoter Plugin's settings and configuration to the WordPress
* Site Health Info section (WordPress Admin Area > Tools > Site Health).
*
* @return array
*/
add_filter( 'debug_information', function ( $info ) {
$options = dfrpswc_get_options();
$info['datafeedr-woocommerce-importer-plugin'] = [
'label' => __( 'Datafeedr WooCommerce Importer Plugin', 'dfrpswc_integration' ),
'description' => '',
'fields' => [
'button_text' => [
'label' => __( 'Button Text', 'dfrpswc_integration' ),
'value' => isset( $options['button_text'] ) && ! empty( $options['button_text'] ) ? $options['button_text'] : '—',
],
'format_price' => [
'label' => __( 'Format Prices', 'dfrpswc_integration' ),
'value' => isset( $options['format_price'] ) && ! empty( $options['format_price'] ) ? ucfirst( $options['format_price'] ) : '—',
'debug' => isset( $options['format_price'] ) && ! empty( $options['format_price'] ) ? $options['format_price'] : '—',
],
]
];
return $info;
} );
/**
* Customize the featured image data before importing image into Media Library.
*
* @param array $args Array of args to use when adding image to Media Library.
* @param string $url URL of image we are importing into the Media Library.
* @param WP_Post $post
*
* @return array
*/
function dfrpswc_image_import_args( array $args, string $url, WP_Post $post ) {
$product = wc_get_product( $post->ID );
if ( ! $product ) {
return $args;
}
$args = [
'title' => $product->get_name(),
'file_name' => $product->get_name(),
'post_id' => $product->get_id(),
'description' => $product->get_name(),
'caption' => $product->get_name(),
'alt_text' => $product->get_name(),
'user_id' => dfrpswc_get_post_author_of_product_set_for_product( $product->get_id() ),
'is_post_thumbnail' => true,
'timeout' => 5,
'_source_plugin' => 'dfrpswc',
];
return apply_filters( 'dfrpswc_image_import_args', $args, $url, $post );
}
add_filter( 'dfrps_import_post_thumbnail/args', 'dfrpswc_image_import_args', 10, 3 );
/**
* Set the rel attribute for the Buy Button on Single Product Pages.
*
* @since 1.2.58
*
* @param string $product_type
*
* @param string $rel
*
* @return string
*/
function dfrpswc_single_product_button_rel( string $rel, string $product_type ) {
return ( $product_type === 'external' )
? dfrpswc_get_option( 'rel_single', 'nofollow' )
: $rel;
}
add_filter( 'dfrpswc_single_product_add_to_cart_button_rel', 'dfrpswc_single_product_button_rel', 10, 2 );
/**
* Set the rel attribute for the Buy Button in the Loop.
*
* @since 1.2.58
*
* @param WC_Product $product
*
* @param array $args
*
* @return array
*/
function dfrpswc_loop_button_rel( array $args, WC_Product $product ) {
if ( $product->is_type( 'external' ) ) {
$args['attributes']['rel'] = dfrpswc_get_option( 'rel_loop', 'nofollow' );
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'dfrpswc_loop_button_rel', 10, 2 );
/**
* Set the target attribute for the Buy Button on Single Product Pages.
*
* @since 1.2.58
*
* @param string $product_type
*
* @param string $rel
*
* @return string
*/
function dfrpswc_single_product_button_target( string $target, string $product_type ) {
return ( $product_type === 'external' )
? dfrpswc_get_option( 'target_single', '_blank' )
: $target;
}
add_filter( 'dfrpswc_single_product_add_to_cart_button_target', 'dfrpswc_single_product_button_target', 10, 2 );
/**
* Set the target attribute for the Buy Button in the Loop.
*
* @since 1.2.58
*
* @param WC_Product $product
*
* @param array $args
*
* @return array
*
*/
function dfrpswc_loop_button_target( array $args, WC_Product $product ) {
if ( $product->is_type( 'external' ) ) {
$args['attributes']['target'] = dfrpswc_get_option( 'target_loop', '_blank' );
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'dfrpswc_loop_button_target', 10, 2 );