Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jilt support for WooCommerce Phone Orders
<?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() {
if ( defined( 'WC_PHONE_CUSTOMER_COOKIE' ) && '' !== WC_PHONE_CUSTOMER_COOKIE ) {
if ( isset( $_COOKIE[ WC_PHONE_CUSTOMER_COOKIE ] ) ) {
wp_dequeue_script( 'wc-jilt' );
}
}
}, 15 );
// Don't report the first creation of a phone order to Jilt, but still set the cart token.
add_action( 'woocommerce_checkout_order_processed', function( $order_id ) {
if ( doing_action( 'wp_ajax_phone-orders-for-woocommerce' ) ) {
remove_action( 'woocommerce_checkout_order_processed', [ wc_jilt()->get_checkout_handler_instance(), 'checkout_order_processed' ] );
$order = new WC_Jilt_Order( $order_id );
if ( ! $order->get_jilt_cart_token() ) {
$order->set_jilt_cart_token();
}
}
}, 1 );
// Disable order webhooks for phone orders
add_filter( 'woocommerce_webhook_should_deliver', function( $should_deliver, $webhook, $arg ) {
// If we're currently processing a phone order submission
if ( doing_action( 'wp_ajax_phone-orders-for-woocommerce' ) ) {
// 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 );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment