Skip to content

Instantly share code, notes, and snippets.

@kdctek
Created August 13, 2020 21:44
Show Gist options
  • Save kdctek/31c73c0464bf4e08b709951022935f7a to your computer and use it in GitHub Desktop.
Save kdctek/31c73c0464bf4e08b709951022935f7a to your computer and use it in GitHub Desktop.
WooCommerce Stripe Gateway - India FIX | In version 4.5.1, WooCommerce Stripe plugin restricted use to the plugin, if the Store Address is not listed in the supported country list. This piece code, will (1) Re-Enable India as a supported country, even in the Preview phase. (2) Allow Indian merchants to enable `Export of Goods` compatibility, thu…
<?php
/**
* Plugin Name: WooCommerce Stripe Gateway - India FIX
* Plugin URI: https://github.com/woocommerce/woocommerce-gateway-stripe/issues/1284
* Description: Stripe India, requires to pass some addtional parameters. [Read Here](https://stripe.com/docs/india-exports)
* Author: KDC
* Author URI: https://kdc.in/
* Version: 1.0.0
* Requires at least: 4.4
* Tested up to: 5.5
* WC requires at least: 3.0
* WC tested up to: 4.3
* Text Domain: woocommerce-gateway-stripe-in
*
*/
/**
* Avoid direct access to file
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Check if 'WooCommerce Stripe Gateway' is installed & active
*/
if ( ! is_plugin_active( 'woocommerce-gateway-stripe/woocommerce-gateway-stripe.php' ) ) {
echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe - India FIX requires WooCommerce Stripe Gateway to be installed and active. You can download %s here.', 'woocommerce-gateway-stripe-in' ), '<a href="https://wordpress.org/plugins/woocommerce-gateway-stripe/" target="_blank">WP Plugin</a>' ) . '</strong></p></div>';
exit;
}
/**
* Enable support to Store Country as IN
*
* @since 1.0.0
* @see https://gist.github.com/tommyshellberg/6e1d1ddae944f3cbdd4b0aad87844b13
*/
if( ! function_exists( 'kdc_wc_stripe_in_add_supported_country' ) ) {
function kdc_wc_stripe_in_add_supported_country( $countries ) {
$countries[] = 'IN'; // India
return $countries;
}
add_filter( 'wc_stripe_supported_countries', 'kdc_wc_stripe_in_add_supported_country', 10, 2 );
}
/**
* Add Post Data to Stripe's PaymentIntent
*
* @since 1.0.0
*/
if( ! function_exists( 'kdc_wc_stripe_in_add_post_data' ) ) {
function kdc_wc_stripe_in_add_post_data( $post_data, $order ) {
/**
* Check if the cart has only Virtual &/or Downloadable Items,
* Accordingly mark as `Export of Goods`
*
* @see https://stripe.com/docs/india-exports#export-of-goods
* @credit https://wordpress.org/support/topic/skip-shipping-for-virtual-variable-products/#post-10125403
*/
// By default, virtual/downloadable product only
$kdc_virtual_downloadable_products_only = true;
// Get all products in cart
$products = WC()->cart->get_cart();
// Loop through cart products
foreach( $products as $product ) {
// is variation downloadable
$is_downloadable = $product['data']->downloadable;
// is variation virtual
$is_virtual = $product['data']->virtual ;
// Update $virtual_downloadable_products_only if product is not virtual or downloadable and exit loop
if( 'no' == $is_virtual && 'no' == $is_downloadable ) {
$kdc_virtual_downloadable_products_only = false;
break;
}
}
if( false === $kdc_virtual_downloadable_products_only ) {
/**
* Add Shipping Info
*
* @see https://github.com/woocommerce/woocommerce-gateway-stripe/issues/1266#issuecomment-667976567
* @credit marcinbot
*/
if ( ! empty( $order->get_shipping_postcode() ) ) {
$post_data['shipping'] = array(
'name' => $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(),
'address' => array(
'line1' => $order->get_shipping_address_1(),
'line2' => $order->get_shipping_address_2(),
'city' => $order->get_shipping_city(),
'country' => $order->get_shipping_country(),
'postal_code' => $order->get_shipping_postcode(),
'state' => $order->get_shipping_state(),
)
);
}
}
return $post_data;
}
add_filter( 'wc_stripe_generate_create_intent_request', 'kdc_wc_stripe_in_add_post_data', 10, 2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment