-
-
Save deivamagalhaes/7ffde9790adb41236103198bcfa74d9f to your computer and use it in GitHub Desktop.
Jilt support for WooCommerce Phone Orders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add the following snippet to your theme's functions.php file, or use a plugin like Code Snippets. | |
add_action( 'init', function() { | |
// Only make changes as long as Jilt for WooCommerce is installed and connected to Jilt | |
if ( function_exists( 'wc_jilt' ) && wc_jilt()->get_integration()->is_jilt_connected() ) { | |
// Disable Storefront JS for Phone Order carts | |
add_action( 'wp_enqueue_scripts', function() { | |
$is_admin_user = false; | |
$user = wp_get_current_user(); | |
if ( ! empty( $user ) && in_array( 'administrator', (array) $user->roles ) ) { | |
$is_admin_user = true; | |
} | |
$cookie_set = false; | |
if ( defined( 'WC_PHONE_CUSTOMER_COOKIE' ) && '' !== WC_PHONE_CUSTOMER_COOKIE ) { | |
if ( isset( $_COOKIE[ WC_PHONE_CUSTOMER_COOKIE ] ) ) { | |
$cookie_set = true; | |
} | |
} | |
if ( $is_admin_user || $cookie_set ) { | |
wp_dequeue_script( 'wc-jilt' ); | |
} | |
}, 15 ); | |
// Disable order webhooks for unpaid phone orders | |
add_filter( 'woocommerce_webhook_should_deliver', function( $should_deliver, $webhook, $arg ) { | |
if ( $arg instanceof \WC_Order ) { | |
$order = $arg; | |
} elseif ( is_numeric( $arg ) ) { | |
$order = wc_get_order( $arg ); | |
} else { | |
$order = null; | |
} | |
// If we're currently processing a phone order submission | |
if ( $order instanceof \WC_Order && $order->meta_exists( '_wpo_order_creator' ) && ! $order->is_paid() ) { | |
// And, if sending an order.{topic} webhook to Jilt | |
if ( 0 === strpos( $webhook->get_topic(), 'order.' ) && false !== strpos( $webhook->get_delivery_url(), wc_jilt()->get_app_hostname() ) ) { | |
// Don't do it. | |
$should_deliver = false; | |
} | |
} | |
return $should_deliver; | |
}, 10, 3 ); | |
// Prevent update_order API calls for unpaid phone orders | |
add_filter( 'wc_jilt_should_update_order', function ( $should_update, $cart_token, $args ) { | |
if ( ! empty( $args['order_id'] ) ) { | |
$order_id = $args['order_id']; | |
$order = wc_get_order( $order_id ); | |
if ( $order instanceof \WC_Order && $order->meta_exists( '_wpo_order_creator' ) && ! $order->is_paid() ) { | |
$should_update = false; | |
} | |
} | |
return $should_update; | |
}, 10, 3 ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment