Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HPOS support with backward compatibility #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions conversion-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ public function __construct() {
$this->init_classes();

register_activation_hook( __FILE__, array( $this, 'activate' ) );
// hpos support
add_action( 'before_woocommerce_init', [ $this, 'add_hpos_support' ] );

// Add High Performance Order Storage Support
add_action( 'before_woocommerce_init', [ $this, 'declare_woocommerce_feature_compatibility' ] );

do_action( 'wcct_loaded' );
}
Expand Down Expand Up @@ -258,12 +259,14 @@ public function init_tracker() {

/**
* Add High Performance Order Storage Support
*
* @see https://developer.woocommerce.com/docs/hpos-extension-recipe-book/
*
* @since 2.0.11
* @since 2.1.0
*
* @return void
*/
public function add_hpos_support() {
public function declare_woocommerce_feature_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
Expand Down Expand Up @@ -345,7 +348,7 @@ public function happy_addons_ads_banner() {
</div>
<div class="wcct-message-action">
<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>
<p></strong><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
<p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
</p>
Comment on lines +351 to 352
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Maintain valid HTML structure.

The removal of the closing </p> tag could lead to invalid HTML structure. This change doesn't appear to be related to the main objectives of the PR.

Consider reverting this change to maintain valid HTML:

-                    <p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
-                    </p>
+                    <p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a></p>

This will ensure that the HTML structure remains valid and properly formatted.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a>
</p>
<p><a target="_blank" href="https://wordpress.org/plugins/happy-elementor-addons/">Read more details ➔</a></p>

</div>
</div>
Expand All @@ -364,8 +367,8 @@ function wcct_init() {
/**
* Manage Capability
*
* @return void
* @return string
*/
function wcct_manage_cap() {
return apply_filters( 'wcct_capability', 'manage_options' );
}
}
4 changes: 1 addition & 3 deletions includes/class-abstract-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function get_name() {
public function is_enabled() {
$settings = $this->get_integration_settings();

if ( $settings && $settings[ 'enabled' ] == true ) {
if ( $settings && wc_string_to_bool($settings[ 'enabled' ]) ) {
return true;
}

Expand All @@ -107,8 +107,6 @@ public function event_enabled( $event ) {
/**
* Integration settings get options
*
* @param string $integration_id
*
* @return array|false
*/
public function get_integration_settings() {
Expand Down
4 changes: 2 additions & 2 deletions includes/class-integration-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function includes_integration() {
/**
* Get all active integrations
*
* @return void
* @return array
*/
public function get_active_integrations() {
$integrations = $this->integrations;
Expand All @@ -60,7 +60,7 @@ public function get_active_integrations() {
*/
public function get_integrations() {
if ( empty( $this->integrations ) ) {
return;
return array();
}

return $this->integrations;
Expand Down
74 changes: 55 additions & 19 deletions includes/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ function validate_textarea_field( $key, $value ) {
* @param int $post_id
*/
function product_options_save( $post_id ) {

if ( isset( $_POST['_wc_conv_track'] ) ) {
$value = trim( sanitize_text_field( wp_unslash( $_POST['_wc_conv_track'] ) ) );
update_post_meta( $post_id, '_wc_conv_track', $value );

$product = wc_get_product( $post_id );
if ( $product ) {
$product->update_meta_data( '_wc_conv_track', $value );
$product->save();
}
}
}

Expand Down Expand Up @@ -225,25 +229,48 @@ function code_handler() {
public function thankyou_page( $order_id ) {
$order = wc_get_order( $order_id );

if ( $items = $order->get_items() ) {
foreach ( $items as $item ) {
$product = $order->get_product_from_item( $item );
if ( ! $order ) {
return;
}

if ( ! $product ) {
continue;
}
/**
* @var WC_Order_Item_Product $item
*/
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();

$code = get_post_meta( $product->get_id(), '_wc_conv_track', true );
if ( ! $product ) {
continue;
}

if ( empty( $code ) ) {
continue;
}
$code = $this->get_product_conversion_code( $product->get_id() );

echo $this->print_conversion_code( $this->process_product_markdown( $code, $product ) );
if ( empty( $code ) ) {
continue;
}

echo $this->print_conversion_code( $this->process_product_markdown( $code, $product ) );
}
}

/**
* Get product conversion code
*
* @param int $product_id
*
* @return string
*/
private function get_product_conversion_code( $product_id ) {
$product = wc_get_product( $product_id );

if ( ! $product ) {
return '';
}

return $product->get_meta( '_wc_conv_track' );
}


/**
* Registration code print handler
*
Expand All @@ -258,16 +285,20 @@ function print_reg_code() {
*
* @param string $code
*
* @return void
* @return string
*/
function print_conversion_code( $code ) {
if ( $code == '' ) {
return;
return '';
}

ob_start();

echo "<!-- Tracking pixel by WooCommerce Conversion Tracking plugin by Tareq Hasan -->\n";
echo $code;
echo "\n<!-- Tracking pixel by WooCommerce Conversion Tracking plugin -->\n";

return ob_get_clean();
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -293,8 +324,7 @@ function process_order_markdown( $code ) {
return $code;
}

if ( version_compare( WC()->version, '3.0', '<' ) ) {
// older version
if ( version_compare( WC()->version, '3.0', '<=' ) ) {
$order_currency = $order->get_order_currency();
$payment_method = $order->payment_method;

Expand All @@ -303,9 +333,15 @@ function process_order_markdown( $code ) {
$payment_method = $order->get_payment_method();
}

if ( version_compare( WC()->version, '3.7', '<=' ) ) {
$used_coupons = $order->get_used_coupons();

} else {
$used_coupons = $order->get_coupon_codes();
}

$customer = $order->get_user();
$used_coupons = $order->get_used_coupons() ? implode( ',', $order->get_used_coupons() ) : '';
$order_currency = $order_currency;
$used_coupons = implode( ',', $used_coupons );
$order_total = $order->get_total();
$order_number = $order->get_order_number();
$order_subtotal = $order->get_subtotal();
Expand Down
18 changes: 12 additions & 6 deletions includes/integrations/class-integration-custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,30 @@ function process_order_markdown( $code, $order_id ) {
return $code;
}

if ( version_compare( WC()->version, '3.0', '<' ) ) {
// older version
if ( version_compare( WC()->version, '3.0', '<=' ) ) {
$order_currency = $order->get_order_currency();
$payment_method = $order->payment_method;
$order_shipping = $order->get_total_shipping();
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved

} else {
$order_currency = $order->get_currency();
$payment_method = $order->get_payment_method();
$order_shipping = $order->get_shipping_total();
}
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved

if ( version_compare( WC()->version, '3.7', '<=' ) ) {
$used_coupons = $order->get_used_coupons();

} else {
$used_coupons = $order->get_coupon_codes();
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved
}

$customer = $order->get_user();
$used_coupons = $order->get_used_coupons() ? implode( ',', $order->get_used_coupons() ) : '';
$order_currency = $order_currency;
$order_total = $order->get_total() ? $order->get_total() : 0;
$used_coupons = implode( ',', $used_coupons );
$order_total = $order->get_total();
$order_number = $order->get_order_number();
$order_subtotal = $order->get_subtotal();
$order_discount = $order->get_total_discount();
$order_shipping = $order->get_total_shipping();


// customer details
Expand Down