2017-05-19 1 views
0

최종 도착 도시에서 배달 날짜와 시간을 선택할 수있는 맞춤 코드가 있습니다. 배달 속도는 도시에 따라 변경되어야합니다. 도시 선택 상자가 결제 페이지에 있습니다.WooCommerce 사이트의 결제 페이지에서 배송비를 업데이트하십시오.

도시를 변경하면 주문 검토 섹션에 표시된 배달 비용이 성공적으로 변경됩니다. 여태까지는 그런대로 잘됐다. 주문을 제출하면 가격이 주문에 미치지 못합니다. 그것은 어딘가에서 길을 잃는다.

같은 도시 선택 상자가 제품 페이지에도 있으며, 사용자가 장바구니에 추가 할 때 선택하고 결제하기로 이동하면 배달 속도가 표시되고 주문을 제출할 때 분실되지 않습니다.

배송비를 업데이트하려면 새로 고침해야합니까?

(성공적으로) 요금

function adjust_shipping_rate($rates){ 
    global $woocommerce; 

     foreach ($rates as $rate) { 
      $cost = $rate->cost; 
      $rate->cost = $_COOKIE['shipping_city_cost']; 
     } 
     return $rates; 
} 
add_filter('woocommerce_package_rates', 'adjust_shipping_rate', 50, 1); 

UPDATE 21-05-2017

이 쿠키를 업데이트하는 방법입니다 업데이트 기능 : 선택 상자가 변경되면, 비동기를 ajax 호출이 시작되고 다음 PHP 코드가 실행됩니다. 여기

function get_and_set_shipping_rate(){ 
    $shipping_city = $_POST['city']; 
    $shipping_cost = get_shipping_cost_by_city($shipping_city); 
    setcookie('shipping_city_cost', $shipping_cost, time() + (86400 * 30), '/'); 
    $_COOKIE['shipping_city_cost'] = $shipping_cost; 
    echo 'Shipping cost updated: '.$shipping_city.' : '.$shipping_cost; 
} 

add_action('wp_ajax_get_and_set_shipping_rate', 'get_and_set_shipping_rate'); 
add_action('wp_ajax_nopriv_get_and_set_shipping_rate', 'get_and_set_shipping_rate'); 

가 아약스 호출입니다 :이

jQuery(document).on('change', '#shipping_delivery_city', function(){ 
    var requested_city = jQuery(this).val(); 
     var data = { 
       'action': 'get_and_set_shipping_rate', 
       'city': requested_city 
      }; 

      jQuery.ajax({ 
       type: "POST", 
       url: shipping_dates.ajax_url, 
       data: data, 
       async: false, 
       success: function (response) { 
        console.log(response); 
       } 
      }); 
}); 
+0

:이 테스트에 대한

, 나는이 기능을 사용할 수 있습니까? – Reigel

+0

@Reigel 도움을 주셔서 감사합니다. 위의 업데이트를 참조하십시오. –

+0

이 코드는 결제 페이지에서만 필요합니까? – Reigel

답변

0

, 나는 두 가지 가정이있다. 1.) 체크 아웃 페이지에서이 코드를 사용하고 있습니다. 2) 당신은 이미 자신의 기능 get_shipping_cost_by_city

add_action('woocommerce_checkout_update_order_review', 'woocommerce_checkout_update_order_review'); 
function woocommerce_checkout_update_order_review($post_data){ 

    $data = array(); 
    $vars = explode('&', $post_data); 
    foreach ($vars as $k => $value){ 
     $v = explode('=', urldecode($value)); 
     $data[$v[0]] = $v[1]; 
    } 
    $shipping_cost = get_shipping_cost_by_city($data['billing_city']); 
    WC()->session->set('shipping_city_cost', $shipping_cost); 
    foreach (WC()->cart->get_shipping_packages() as $package_key => $package) { 
     // this is needed for us to remove the session set for the shipping cost. Without this, we can't set it on the checkout page. 
     WC()->session->set('shipping_for_package_' . $package_key, false); 
    } 

} 

add_filter('woocommerce_package_rates', 'adjust_shipping_rate', 50); 
function adjust_shipping_rate($rates){ 

    foreach ($rates as $rate) { 
     $cost = $rate->cost; 
     $rate->cost = WC()->session->get('shipping_city_cost'); 
    } 
    return $rates; 
} 

설정이/shipping_city_cost의 점점이 WC()->session->setWC()->session->get에 의해 수행되어 있습니다.에 대한 ajax 함수를 넣지 않아도됩니다. 체크 아웃 페이지에서 업데이트가 완료 될 때마다 ajax 호출 내의 액션 훅입니다. 당신은`$의 _COOKIE [ 'shipping_city_cost를']`설정하는 방법

function get_shipping_cost_by_city($city) { 
    if ($city == 'Ormoc City'){ 
     return 100; 
    } 
    return 130; 
} 
관련 문제