2017-08-30 44 views
1

我想在主頁上彈出一個簡單的模型,讓用戶在結帳/購物車頁面之前選擇他們的運送國家。更新客戶Woocommerce結賬前的地址頁面

我做了一個小的選擇框彈出式模型,然後用AJAX運行這個函數來改變用戶的運送國家,但它沒有被更新。

//本地化

//*********** Ajax Change shipping country on press ***********/ 

function localize_array() { 

$localize = array(
    'ajax_url' => admin_url('admin-ajax.php'), 
    'nonce' => wp_create_nonce('yo-ajax-call-ak'), 
); 

return $localize; 

} 

function ajax_change_shipping() { 
wp_enqueue_script('ak-country-change', get_template_directory_uri() . '/inc/js/select-country.js', array('jquery',), '1.0', true); 
wp_localize_script('ak-country-change', 'akselect', localize_array()); 
} 

add_action('wp_enqueue_scripts', 'ajax_change_shipping'); 

// Ajax調用

$(document).ready(function() { 

    $('body').on('change', '.yo-lang-select', function() { 

     var select = $(this); 
     var value = select.val(); 
     $.ajax({ 
      url: akselect.ajax_url, 
      method: 'POST', 
      dataType: 'json', 
      data: { 
       action: 'get_the_defualt_lang', 
       value: value 
       nonce: akselect.nonce 
      }, 
      success: function (respond) { 
       console.log(respond); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 

       console.log(jqXHR, textStatus, errorThrown); 

      } 
     }); 
    }); 

}); 

//這是函數

add_action('wp_ajax_get_the_defualt_lang', 'get_the_defualt_lang'); 
add_action('wp_ajax_nopriv_get_the_defualt_lang','get_the_defualt_lang'); 

function get_the_defualt_lang() { 

$lang = $_POST['value']; 

add_action('woocommerce_add_to_cart', 'set_country_befor_cart_page'); 
function set_country_befor_cart_page() { 

    WC()->customer->set_country($lang); 
    WC()->customer->set_shipping_country($lang); 

} 

$respond = $lang; 

echo json_encode($respond); 


die(); 

} 

//彈出形式

<div data-effect="mfp-zoom-out" id="shop-pop_1" class="mfp-hide"> 
<select class="yo-lang-select" data-type="lang" title=""> 
    <option value="US">United States</option> 
    <option value="GB">United Kingdom (UK)</option> 
    <option value="US">Other Countries</option> 

</select> 

//使用掛機,可從該店鋪頁面

add_action('woocommerce_after_shop_loop', 'ak_store_popup'); 

function ak_store_popup() { 
    get_template_part('template-parts/popups/popup', 'store-main'); 
} 
+1

我添加的形式和鉤檢查之前得到它在店鋪頁面的代碼,希望它能幫助,感謝您的反饋 – Akarob

+1

我現在又增加了,謝謝 – Akarob

回答

1

更新插入:一些其他更改成功測試:

  • jQuery的:當頁面當選擇字段被加載時,'US'默認值現在通過ajax設置。
  • 選擇字段:只需要2個值(默認值:'US'|其他:'GB')。您可以添加許多其他人。
  • 該國家現在正確設置(開票國家是必要的)。

1)jQuery代碼:

有什麼特別的事與WordPress與jQuery,就是肖特蘭$沒有工作...所以爲了讓你的工作jQuery代碼需要啓動搭配:jQuery(document).ready(function ($) {

jQuery(document).ready(function ($) { 
    var select = $('.yo-lang-select'), 
     value = select.val(); 

    function ajax_update(value){ 
     $.ajax({ 
      url: akselect.ajax_url, 
      method: 'POST', 
      dataType: 'json', 
      data: { 
       action: 'get_the_defualt_lang', 
       value: value 
       //nonce: akselect.nonce 
      }, 
      success: function (respond) { 
       console.log(respond); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       console.log(jqXHR, textStatus, errorThrown); 
      } 
     }); 
    } 

    ajax_update(value); 

    $('body').on('change', select, function() { 
     value = select.val(); 
     ajax_update(value); 
    }); 
}); 

2)HTML選擇字段:

<div data-effect="mfp-zoom-out" id="shop-pop_1" class="mfp-hide"> 
<select class="yo-lang-select" data-type="lang" title=""> 
    <option value="US">United States</option> 
    <option value="GB">United Kingdom (UK)</option> 
</select> 

3)PHP函數:

  • 現在你得到正確的響應和Ajax是工作的罰款。
  • 的另一個問題是使用set_country_befor_cart_page掛鉤函數在woocommerce_add_to_cart在你的PHP腳本的AJAX內嵌因爲它永遠不會,永遠不會得到$lang值。
  • 我已經加入了結算國家真正定國多爲結賬...

所以,而不只是使用這種方式:

add_action('wp_ajax_get_the_defualt_lang', 'get_the_defualt_lang'); 
add_action('wp_ajax_nopriv_get_the_defualt_lang','get_the_defualt_lang'); 
function get_the_defualt_lang() { 

    $lang = $_POST['value']; 

    WC()->customer->set_country($lang); 
    WC()->customer->set_billing_country($lang); // Updated HERE Too 
    WC()->customer->set_shipping_country($lang); 

    echo json_encode($lang); 

    die(); 
} 

代碼放在的function.php文件您活躍的兒童主題(或主題)或任何插件文件。

這一次,這是測試和工作。

  • 作爲您的選擇字段在彈出加載,你也許應該需要做一些ajustements jQuery中,而不是處理此事件。
  • 可能是您應該需要限制您的腳本與if (! is_user_logged_in()) {,爲非登錄/註冊的客戶只有
+0

功能仍然不更新客戶國家。 – Akarob

+0

現在正常工作,感謝您的幫助! – Akarob

相關問題