Skip to content

Commit 12dc80a

Browse files
Add HPOS support with backward compatibility (#54)
* Add HPOS support with backward compatibility * removed: checking the hpos support and error log * update: boolean data checking and coupons code * added: HPOS reference for compatibility * remove: helper method to Check if HPOS is enabled * update: cast string to bool with wc helper function
1 parent 81023b9 commit 12dc80a

5 files changed

Lines changed: 80 additions & 37 deletions

File tree

conversion-tracking.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public function __construct() {
7878
$this->init_classes();
7979

8080
register_activation_hook( __FILE__, array( $this, 'activate' ) );
81-
// hpos support
82-
add_action( 'before_woocommerce_init', [ $this, 'add_hpos_support' ] );
81+
82+
// Add High Performance Order Storage Support
83+
add_action( 'before_woocommerce_init', [ $this, 'declare_woocommerce_feature_compatibility' ] );
8384

8485
do_action( 'wcct_loaded' );
8586
}
@@ -258,12 +259,14 @@ public function init_tracker() {
258259

259260
/**
260261
* Add High Performance Order Storage Support
262+
*
263+
* @see https://developer.woocommerce.com/docs/hpos-extension-recipe-book/
261264
*
262-
* @since 2.0.11
265+
* @since 2.1.0
263266
*
264267
* @return void
265268
*/
266-
public function add_hpos_support() {
269+
public function declare_woocommerce_feature_compatibility() {
267270
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
268271
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
269272
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
@@ -345,7 +348,7 @@ public function happy_addons_ads_banner() {
345348
</div>
346349
<div class="wcct-message-action">
347350
<a href="" id="wcct-install-happ-addons" class="button button-primary"> <i class="dashicons dashicons-update wcct-update-icon"></i> Install Now For FREE</a>
348-
<p></strong><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
351+
<p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
349352
</p>
350353
</div>
351354
</div>
@@ -364,8 +367,8 @@ function wcct_init() {
364367
/**
365368
* Manage Capability
366369
*
367-
* @return void
370+
* @return string
368371
*/
369372
function wcct_manage_cap() {
370373
return apply_filters( 'wcct_capability', 'manage_options' );
371-
}
374+
}

includes/class-abstract-integration.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function get_name() {
8080
public function is_enabled() {
8181
$settings = $this->get_integration_settings();
8282

83-
if ( $settings && $settings[ 'enabled' ] == true ) {
83+
if ( $settings && wc_string_to_bool($settings[ 'enabled' ]) ) {
8484
return true;
8585
}
8686

@@ -107,8 +107,6 @@ public function event_enabled( $event ) {
107107
/**
108108
* Integration settings get options
109109
*
110-
* @param string $integration_id
111-
*
112110
* @return array|false
113111
*/
114112
public function get_integration_settings() {

includes/class-integration-manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function includes_integration() {
3838
/**
3939
* Get all active integrations
4040
*
41-
* @return void
41+
* @return array
4242
*/
4343
public function get_active_integrations() {
4444
$integrations = $this->integrations;
@@ -60,7 +60,7 @@ public function get_active_integrations() {
6060
*/
6161
public function get_integrations() {
6262
if ( empty( $this->integrations ) ) {
63-
return;
63+
return array();
6464
}
6565

6666
return $this->integrations;

includes/integration.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

includes/integrations/class-integration-custom.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,30 @@ function process_order_markdown( $code, $order_id ) {
107107
return $code;
108108
}
109109

110-
if ( version_compare( WC()->version, '3.0', '<' ) ) {
111-
// older version
110+
if ( version_compare( WC()->version, '3.0', '<=' ) ) {
112111
$order_currency = $order->get_order_currency();
113112
$payment_method = $order->payment_method;
113+
$order_shipping = $order->get_total_shipping();
114114

115115
} else {
116116
$order_currency = $order->get_currency();
117117
$payment_method = $order->get_payment_method();
118+
$order_shipping = $order->get_shipping_total();
119+
}
120+
121+
if ( version_compare( WC()->version, '3.7', '<=' ) ) {
122+
$used_coupons = $order->get_used_coupons();
123+
124+
} else {
125+
$used_coupons = $order->get_coupon_codes();
118126
}
119127

120128
$customer = $order->get_user();
121-
$used_coupons = $order->get_used_coupons() ? implode( ',', $order->get_used_coupons() ) : '';
122-
$order_currency = $order_currency;
123-
$order_total = $order->get_total() ? $order->get_total() : 0;
129+
$used_coupons = implode( ',', $used_coupons );
130+
$order_total = $order->get_total();
124131
$order_number = $order->get_order_number();
125132
$order_subtotal = $order->get_subtotal();
126133
$order_discount = $order->get_total_discount();
127-
$order_shipping = $order->get_total_shipping();
128134

129135

130136
// customer details

0 commit comments

Comments
 (0)