@@ -109,10 +109,14 @@ function validate_textarea_field( $key, $value ) {
109109 * @param int $post_id
110110 */
111111 function product_options_save ( $ post_id ) {
112-
113112 if ( isset ( $ _POST ['_wc_conv_track ' ] ) ) {
114113 $ value = trim ( sanitize_text_field ( wp_unslash ( $ _POST ['_wc_conv_track ' ] ) ) );
115- update_post_meta ( $ post_id , '_wc_conv_track ' , $ value );
114+
115+ $ product = wc_get_product ( $ post_id );
116+ if ( $ product ) {
117+ $ product ->update_meta_data ( '_wc_conv_track ' , $ value );
118+ $ product ->save ();
119+ }
116120 }
117121 }
118122
@@ -225,25 +229,48 @@ function code_handler() {
225229 public function thankyou_page ( $ order_id ) {
226230 $ order = wc_get_order ( $ order_id );
227231
228- if ( $ items = $ order-> get_items () ) {
229- foreach ( $ items as $ item ) {
230- $ product = $ order -> get_product_from_item ( $ item );
232+ if ( ! $ order ) {
233+ return ;
234+ }
231235
232- if ( ! $ product ) {
233- continue ;
234- }
236+ /**
237+ * @var WC_Order_Item_Product $item
238+ */
239+ foreach ( $ order ->get_items () as $ item ) {
240+ $ product = $ item ->get_product ();
235241
236- $ code = get_post_meta ( $ product ->get_id (), '_wc_conv_track ' , true );
242+ if ( ! $ product ) {
243+ continue ;
244+ }
237245
238- if ( empty ( $ code ) ) {
239- continue ;
240- }
246+ $ code = $ this ->get_product_conversion_code ( $ product ->get_id () );
241247
242- echo $ this ->print_conversion_code ( $ this ->process_product_markdown ( $ code , $ product ) );
248+ if ( empty ( $ code ) ) {
249+ continue ;
243250 }
251+
252+ echo $ this ->print_conversion_code ( $ this ->process_product_markdown ( $ code , $ product ) );
253+ }
254+ }
255+
256+ /**
257+ * Get product conversion code
258+ *
259+ * @param int $product_id
260+ *
261+ * @return string
262+ */
263+ private function get_product_conversion_code ( $ product_id ) {
264+ $ product = wc_get_product ( $ product_id );
265+
266+ if ( ! $ product ) {
267+ return '' ;
244268 }
269+
270+ return $ product ->get_meta ( '_wc_conv_track ' );
245271 }
246272
273+
247274 /**
248275 * Registration code print handler
249276 *
@@ -258,16 +285,20 @@ function print_reg_code() {
258285 *
259286 * @param string $code
260287 *
261- * @return void
288+ * @return string
262289 */
263290 function print_conversion_code ( $ code ) {
264291 if ( $ code == '' ) {
265- return ;
292+ return '' ;
266293 }
267294
295+ ob_start ();
296+
268297 echo "<!-- Tracking pixel by WooCommerce Conversion Tracking plugin by Tareq Hasan --> \n" ;
269298 echo $ code ;
270299 echo "\n<!-- Tracking pixel by WooCommerce Conversion Tracking plugin --> \n" ;
300+
301+ return ob_get_clean ();
271302 }
272303
273304 /**
@@ -293,8 +324,7 @@ function process_order_markdown( $code ) {
293324 return $ code ;
294325 }
295326
296- if ( version_compare ( WC ()->version , '3.0 ' , '< ' ) ) {
297- // older version
327+ if ( version_compare ( WC ()->version , '3.0 ' , '<= ' ) ) {
298328 $ order_currency = $ order ->get_order_currency ();
299329 $ payment_method = $ order ->payment_method ;
300330
@@ -303,9 +333,15 @@ function process_order_markdown( $code ) {
303333 $ payment_method = $ order ->get_payment_method ();
304334 }
305335
336+ if ( version_compare ( WC ()->version , '3.7 ' , '<= ' ) ) {
337+ $ used_coupons = $ order ->get_used_coupons ();
338+
339+ } else {
340+ $ used_coupons = $ order ->get_coupon_codes ();
341+ }
342+
306343 $ customer = $ order ->get_user ();
307- $ used_coupons = $ order ->get_used_coupons () ? implode ( ', ' , $ order ->get_used_coupons () ) : '' ;
308- $ order_currency = $ order_currency ;
344+ $ used_coupons = implode ( ', ' , $ used_coupons );
309345 $ order_total = $ order ->get_total ();
310346 $ order_number = $ order ->get_order_number ();
311347 $ order_subtotal = $ order ->get_subtotal ();
0 commit comments