Skip to content

Commit

Permalink
Merge pull request #24 from KhorshidLab/FIX-POD-58-Shipping-pro-conflict
Browse files Browse the repository at this point in the history
Fix pod 58 shipping pro conflict
  • Loading branch information
parsafatehi authored Jan 9, 2023
2 parents 39dfe62 + 0639c64 commit a80b5d0
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 85 deletions.
10 changes: 9 additions & 1 deletion admin/Enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ public function enqueue_admin_scripts() {
public function enqueue_public_styles() {

if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) {
$city_select_path = PODRO_PLUGIN_ROOT_URL . 'assets/js/cities.js';

if ( function_exists( 'PWS' ) || class_exists('PWS_Core') || in_array( 'persian-woocommerce-shipping/woocommerce-shipping.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$city_select_path = PODRO_PLUGIN_ROOT_URL . 'assets/js/cities.js';
}else{
$city_select_path = PODRO_PLUGIN_ROOT_URL . 'assets/js/only-podro-cities.js';
}



wp_enqueue_script(
'wc-city-select',
$city_select_path,
Expand Down
93 changes: 55 additions & 38 deletions assets/js/cities.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
jQuery(document).ready(function(){

jQuery('body').on('country_to_state_changed', function(p1,country){

if( 'IR' == country ){
//
jQuery('input#billing_city').replaceWith('<select id="billing_city" name="billing_city"></select>');
jQuery('input#shipping_city').replaceWith('<select id="shipping_city" name="shipping_city"></select>');
jQuery('select#billing_city').selectWoo();
jQuery('select#shipping_city').selectWoo();
getCities();

const data = {
action:'get_podro_cities',
method:'post'
}
ajaxRequest(data, podro_cities_callback, null);
let cities = [];
function podro_cities_callback(response, element){
cities = response;

}

jQuery('#billing_city').on('change', function(){

if( !is_this_podro_city(cities,jQuery(this).find('option:selected').text())){
jQuery("[id*='podro_method']").prop('disabled', true);
jQuery("[id*='podro_method']").prop('checked',false);
jQuery("[id*='podro_method']").removeAttr('checked');
const count = jQuery(this).find('#shipping_method input[type=radio]:checked').length;
if(count > 0)
return;
if(!jQuery('#shipping_method input[type=radio]:checked').attr('id'))
jQuery("[id*='podro_method']").parent().next('li').find("input[type=radio]").prop('checked',true);
}else{
jQuery('#billing_city').parent().find('.select2').remove();
jQuery('#shipping_city').parent().find('.select2').remove();
jQuery('#billing_city').replaceWith('<input type="text" name="billing_city" id="billing_city" class="input-text "/>');
jQuery('#shipping_city').replaceWith('<input type="text" name="shipping_city" id="shipping_city" class="input-text "/>');
jQuery("[id*='podro_method']").prop('disabled',false);

}
});

function getCities(){
jQuery('#billing_state, body').change(function(){
const province_code = jQuery(this).val();
const element = jQuery('#billing_city');
if(!province_code)
jQuery('body').on('updated_checkout', function() {
if( !is_this_podro_city(cities,jQuery('#billing_city').find('option:selected').text())){

jQuery("[id*='podro_method']").prop('disabled', true);
jQuery("[id*='podro_method']").prop('checked',false);
jQuery("[id*='podro_method']").removeAttr('checked');
const count = jQuery(this).find('#shipping_method input[type=radio]:checked').length;
if(count > 0)
return;
const data = {
action:'get_podro_cities_by_province',
//security: wp_podro_ajax_object.security,
province: province_code
};

ajaxRequest(data, callbackGetCities, element);

});
if(!jQuery('#shipping_method input[type=radio]:checked').attr('id'))
jQuery("[id*='podro_method']").parent().next('li').find("input[type=radio]").prop('checked',true);
}else{
jQuery("[id*='podro_method']").prop('disabled',false);

jQuery('#shipping_state, body').change(function(){
const province_code = jQuery(this).val();
const element = jQuery('#shipping_city');
if(!province_code)
return;
const data = {
action:'get_podro_cities_by_province',
//security: wp_podro_ajax_object.security,
province: province_code
};

ajaxRequest(data, callbackGetCities, element);
});
}
});


function is_this_podro_city(city_list, name){

let result = false;
Object.keys(city_list).forEach(key => {

if( city_list[key] === name ){
result = true;
return result;
}


});
return result;
}


function callbackGetCities(response, element){

const provinces = JSON.parse(response);
Expand All @@ -58,7 +75,7 @@ jQuery(document).ready(function(){
let cities = provinces.cities;

Object.keys(cities).forEach(function(key) {
console.log('Key : ' + key + ', Value : ' + cities[key])

element.append(`<option value='${key}'>${cities[key]}</option>`);

});
Expand Down
105 changes: 105 additions & 0 deletions assets/js/only-podro-cities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
jQuery(document).ready(function(){

setTimeout(reload_for_cities, 1000);

function reload_for_cities(){

if(jQuery('#billing_state option').length < 0){
setTimeout(reload_for_cities, 1000);
return;
}

const city_count = jQuery('#billing_city option').length;
if(city_count <= 0){
jQuery('#billing_state').trigger('change');
}
}

jQuery('body').on('country_to_state_changed', function(p1,country){

if( 'IR' == country ){
//
jQuery('input#billing_city').replaceWith('<select id="billing_city" name="billing_city"></select>');
jQuery('input#shipping_city').replaceWith('<select id="shipping_city" name="shipping_city"></select>');
jQuery('select#billing_city').selectWoo();
jQuery('select#shipping_city').selectWoo();
getCities();
}else{
jQuery('#billing_city').parent().find('.select2').remove();
jQuery('#shipping_city').parent().find('.select2').remove();
jQuery('#billing_city').replaceWith('<input type="text" name="billing_city" id="billing_city" class="input-text "/>');
jQuery('#shipping_city').replaceWith('<input type="text" name="shipping_city" id="shipping_city" class="input-text "/>');
}
});

function getCities(){
jQuery('#billing_state, body').change(function(){

const province_code = jQuery(this).val();
const element = jQuery('#billing_city');

if(!province_code)
return;
const data = {
action:'get_podro_cities_by_province',
//security: wp_podro_ajax_object.security,
province: province_code
};

ajaxRequest(data, callbackGetCities, element);

});

jQuery('#shipping_state, body').change(function(){
const province_code = jQuery(this).val();
const element = jQuery('#shipping_city');
if(!province_code)
return;
const data = {
action:'get_podro_cities_by_province',
//security: wp_podro_ajax_object.security,
province: province_code
};

ajaxRequest(data, callbackGetCities, element);
});

}





function callbackGetCities(response, element){

const provinces = JSON.parse(response);

element.find('option').remove();

let cities = provinces.cities;

Object.keys(cities).forEach(function(key) {

element.append(`<option value='${key}'>${cities[key]}</option>`);

});

}

function ajaxRequest(data, callback, element){

jQuery.ajax({
url: woocommerce_params.ajax_url,
type: 'post',
data: data,

success: function( response ) {
callback(response, element);
}
}).fail( function( response ) {
console.log(response);
})

}

});
27 changes: 27 additions & 0 deletions inc/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace WP_PODRO\Engine;

class Helper
{
public static function sanitize_recursive(&$input, $sanitizer){
if (empty($input))
return;
if( !is_array($input) ) {

$input = call_user_func($sanitizer, $input);

}
else{
foreach ($input as $key => &$item){
self::sanitize_recursive($item, $sanitizer);

}
}

}

public static function log($var){
file_put_contents(dirname(__DIR__) . '/log.txt', print_r($var,true),FILE_APPEND);
}
}
9 changes: 8 additions & 1 deletion inc/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public function delivery_step_1( $order ) {
$order_id = $order->get_id();
$destination_city_code = $order->get_shipping_city();

$method = $order->get_items( 'shipping' );
$method_id = reset( $method )->get_method_id();
if( 'podro_method' == $method_id &&( function_exists( 'PWS' ) || class_exists('PWS_Core'))){

$destination_city_code = $woo_setting->get_city_by_name($destination_city_code);

}

$destination_city_name = (WooSetting::get_instance())->get_cities()[$destination_city_code];
$destination_address = $destination_city_name . ' ' . $order->get_billing_address_1() . ' ' . $order->get_billing_address_2();
if( mb_strlen($destination_address) > $this->address_length )
Expand Down Expand Up @@ -194,7 +202,6 @@ public function delivery_step_1( $order ) {




?>
<ul class="pod-delivery-step">
<li>
Expand Down
50 changes: 14 additions & 36 deletions inc/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
* @license GPL-3.0+
* @link https://github.com/KhorshidLab/WP-Podro
*/

use WP_PODRO\Engine\Helper;
use WP_PODRO\Admin\Enqueue;
use WP_PODRO\Engine\API\V1\Payments;


class Setup {

/**
Expand Down Expand Up @@ -84,52 +87,27 @@ public function __construct() {
* @since 0.0.1
* @access private
*/


public function podro_get_cities(){
$woosetting = new WooSetting();
$cities = $woosetting->get_cities();

wp_send_json($cities);
wp_die();
}
private function load_dependencies() {

$Api_Key = new Api_Key;
$MetaBox = new MetaBox;
$this->loader = new Loader();

add_action('woocommerce_loaded', function(){


// Disable Persian Woocommerce City Select
if ( function_exists( 'PW' ) && PW()->get_options( 'enable_iran_cities' ) != 'no' ) {
$settings = PW()->get_options();
$settings['enable_iran_cities'] = 'no';
update_option( 'PW_Options', $settings );
}

// Disable Persian Woocommerce shipping City Select
if ( function_exists( 'PWS' ) || class_exists('PWS_Core') || in_array( 'persian-woocommerce-shipping/woocommerce-shipping.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

$pws = \PWS_Core::instance();


// Filters
remove_filter( 'woocommerce_states', [ $pws, 'iran_states' ], 20, 1 );
remove_filter( 'woocommerce_checkout_process', [ $pws, 'checkout_process' ], 20, 1 );
add_action( 'wp_ajax_nopriv_get_podro_cities', [$this, 'podro_get_cities'] );
add_action( 'wp_ajax_get_podro_cities', [$this, 'podro_get_cities'] );


add_action( 'wp_enqueue_scripts', function() {
wp_dequeue_script( 'pwsCheckout' );
}, 999999 );



}

}, 21);

if ( class_exists('WC_City_Select') ) {

$WC_City_Select = new WC_City_Select;

$this->loader->add_filter( 'woocommerce_billing_fields', $WC_City_Select, 'billing_fields', 999999, 2 );
$this->loader->add_filter( 'woocommerce_shipping_fields', $WC_City_Select, 'shipping_fields', 999999, 2 );
$this->loader->add_filter( 'woocommerce_form_field_city', $WC_City_Select, 'form_field_city', 999999, 4 );

}

$this->loader->add_action( 'admin_init', $Api_Key, 'set_pdo_api_key' );
$this->loader->add_action( 'add_meta_boxes', $MetaBox, 'add_meta_boxes' );
Expand Down
Loading

0 comments on commit a80b5d0

Please sign in to comment.