-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathqwc-front.php
240 lines (209 loc) · 7.89 KB
/
qwc-front.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
<?php
if(!defined('ABSPATH'))exit;
function qwc_add_filters_front() {
remove_filter('get_post_metadata', 'qtranxf_filter_postmeta', 5);
add_filter('get_post_metadata', 'qwc_filter_postmeta', 5, 4);
$use_filters = array(
/* do not exist any more */
//'option_woocommerce_email_from_name' => 10,
//'the_title_attribute' => 10,
//'woocommerce_order_product_title' => 10,
//'woocommerce_composite_component_title' => 10,
//'woocommerce_composite_component_description' => 10,
//'woocommerce_composited_product_excerpt' => 10,
//'woocommerce_bto_component_title' => 10,
//'woocommerce_bto_component_description' => 10,
//'woocommerce_bto_product_excerpt' => 10,
//'woocommerce_bundled_item_title' => 10,
//'woocommerce_bundled_item_description' => 10,
//'woocommerce_in_cart_product_title' => 10,
//'woocommerce_order_table_product_title' => 10,
/* one-argument filters */
'wp_mail_from_name' => 20,
'woocommerce_order_item_display_meta_value' => 20,
'woocommerce_order_items_table' => 20,
'woocommerce_page_title' => 20,
'woocommerce_short_description' => 20,
'woocommerce_variation_option_name' => 20,
//'woocommerce_shipping_methods' => 20
/* two-argument filters */
'woocommerce_attribute_label' => 20,
'woocommerce_cart_shipping_method_full_label' => 20,
'woocommerce_cart_tax_totals' => 20,
'woocommerce_email_footer_text' => 20,
'woocommerce_gateway_description' => 20,
'woocommerce_gateway_title' => 20,
'woocommerce_gateway_icon' => 20,
'woocommerce_order_item_name' => 20,
'woocommerce_order_shipping_to_display' => 20,
'woocommerce_order_tax_totals' => 20,
'woocommerce_product_title' => 20,
'woocommerce_rate_label' => 20,
/* three-argument filters */
'woocommerce_attribute' => 20,
'woocommerce_cart_item_name' => 20,
'woocommerce_cart_item_thumbnail' => 20,
'woocommerce_order_subtotal_to_display' => 20,
/* four-argument filters */
'woocommerce_format_content' => 20,//function wc_format_content in woocommerce/includes/wc-formatting-functions.php
//not in front
//'woocommerce_email_get_option' => 0
);
foreach ( $use_filters as $name => $priority ) {
add_filter( $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
}
add_filter( 'woocommerce_paypal_args', 'qwc_paypal_args' );
add_filter( 'woocommerce_get_product_attributes', 'qwc_get_product_attributes', 5 );
//no need add_filter( 'woocommerce_product_default_attributes', 'qwc_product_default_attributes', 5 );
//below do not seem to need
//foreach ( $url_filters as $name => $priority ) {
// add_filter( $name, 'qtranxf_convertURL', $priority );
//}
/* Fix the product categories and tags (displayed above the "additional informations" tab) *
add_filter( 'wp_get_object_terms', function ( $terms ) {
foreach ( $terms as $term ) {
if ( $term->taxonomy == 'product_cat' || $term->taxonomy == 'product_tag' ) {
$term->name = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $term->name );
}
}
return $terms;
} );
/* Fix the product attributes (displayed in the "additional informations" tab) *
add_filter( 'woocommerce_attribute', function ( $text ) {
$values = explode( ', ', $text );
foreach ( $values as $i => $val ) {
$values[ $i ] = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $val );
}
return implode( ', ', $values );
} );
*/
}
function qwc_filter_postmeta($original_value, $object_id, $meta_key = '', $single = false){
//qtranxf_dbg_log_if($object_id==58,'qwc_filter_postmeta: $object_id='.$object_id.' $meta_key:',$meta_key);
switch($meta_key){
case '_product_attributes':
return $original_value;
default: return qtranxf_filter_postmeta($original_value, $object_id, $meta_key, $single);
}
}
/**
* Store the current WordPress language along with the order, so we know later on which language the customer used while ordering
* Called with
* do_action( 'woocommerce_checkout_update_order_meta', $order_id, $this->posted );
* in /woocommerce/includes/class-wc-checkout.php
* @since 1.1
*/
add_action( 'woocommerce_checkout_update_order_meta', 'save_post_qwc_store_language', 100 );
function save_post_qwc_store_language ( $order_id ) {
global $q_config;
add_post_meta( $order_id, '_user_language', $q_config['language'], true );
}
function qwc_get_product_attributes($attributes){
//only 'value' needs to be translated at front end
foreach ( $attributes as $key => $attribute ) {
if(!isset($attribute['value'])) continue;
$attributes[$key]['value'] = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage($attribute['value']);
}
return $attributes;
}
function qwc_paypal_args($args)
{
//if(!isset($args['country']))
$args['lc'] = get_locale();
//unset($args['country']);
return $args;
}
/**
* Dealing with webhooks, which should always send information in Raw ML format
* @since 1.4
*/
if(defined('DOING_CRON')){
function qwc_deliver_webhook_async($webhook_id, $arg)
{
//qtranxf_dbg_log('qwc_deliver_webhook_async: $webhook_id=',$webhook_id);
$page_configs = qtranxf_get_front_page_config();
//qtranxf_dbg_log('$page_configs: ', $page_configs);
if(!empty($page_configs['']['filters'])){
qtranxf_remove_filters($page_configs['']['filters']);
}
remove_filter('get_post_metadata', 'qtranxf_filter_postmeta', 5);
remove_filter('the_posts', 'qtranxf_postsFilter', 5);
remove_action('pre_get_posts', 'qtranxf_pre_get_posts', 99);
remove_filter('get_term', 'qtranxf_useTermLib', 0);
remove_filter('get_terms', 'qtranxf_useTermLib', 0);
//add_filter('get_term', 'qtranxf_useAdminTermLibJoin', 20);
//add_filter('get_terms', 'qtranxf_useAdminTermLibJoin', 20);
}
add_action( 'woocommerce_deliver_webhook_async', 'qwc_deliver_webhook_async', 5, 2 );
}else{
qwc_add_filters_front();
}
/**
* Dealing with mini-cart cache in internal browser storage.
* @param array $cart wc variable holding contents of the cart without language information.
* @return string cart hash with language information
* @since 1.4
*/
function qwc_get_cart_hash($cart)
{
$lang = qtranxf_getLanguage();
$hash = md5(json_encode($cart).$lang);
return $hash;
}
/**
* Dealing with mini-cart cache in internal browser storage.
* Sets 'woocommerce_cart_hash' cookie.
* @param array $cart wc variable holding contents of the cart without language information.
* @since 1.4
*/
function qwc_set_cookies_cart_hash($cart)
{
$hash = qwc_get_cart_hash($cart);
wc_setcookie( 'woocommerce_cart_hash', $hash );
}
/**
* Dealing with mini-cart cache in internal browser storage.
* Response to action 'woocommerce_cart_loaded_from_session'.
* @param WC_Cart $wc_cart wc object without language information.
* @since 1.4
*/
function qwc_cart_loaded_from_session($wc_cart)
{
if(headers_sent())
return;
$cart = $wc_cart->get_cart_for_session();
qwc_set_cookies_cart_hash($cart);
}
add_action( 'woocommerce_cart_loaded_from_session', 'qwc_cart_loaded_from_session', 5 );
/**
* Dealing with mini-cart cache in internal browser storage.
* Response to action 'woocommerce_set_cart_cookies', which overwrites the default WC cart hash and cookies.
* @param bool $set is true if cookies need to be set, otherwse they are unset in calling function.
* @since 1.4
*/
function qwc_set_cart_cookies($set)
{
if($set){
$wc = WC();
$wc_cart = $wc->cart;
$cart = $wc_cart->get_cart_for_session();
qwc_set_cookies_cart_hash($cart);
}
}
add_action( 'woocommerce_set_cart_cookies', 'qwc_set_cart_cookies' );
/**
* Dealing with mini-cart cache in internal browser storage.
* Response to action 'woocommerce_add_to_cart_hash', which overwrites the default WC cart hash and cookies.
* @param string $hash default WC hash.
* @param array $cart wc variable holding contents of the cart without language information.
* @since 1.4
*/
function qwc_add_to_cart_hash($hash,$cart)
{
$hash = qwc_get_cart_hash($cart);
if(!headers_sent()){
wc_setcookie( 'woocommerce_cart_hash', $hash );
}
return $hash;
}
add_filter( 'woocommerce_add_to_cart_hash', 'qwc_add_to_cart_hash', 5, 2 );