programing

WooCommerce의 기존 주문 내 배송 방법/요금 계산

linuxpc 2023. 3. 2. 22:02
반응형

WooCommerce의 기존 주문 내 배송 방법/요금 계산

기본적으로 사용자가 자신의 기능을 추가할 수 있는 카트 페이지와 같은 기능을 복제하려고 합니다.zip code배송료도 계산해 주는데, 백엔드에서 이미 작성된 주문부터 계산하려고 합니다.

나는 그것을 직접 하는 방법을 찾을 수 없었다.WC_Order예를 들어 카트 세션을 클리어하고 주문 내 모든 항목을 카트 세션에 추가한 후 계산해 봅니다.

지금까지 제가 알아낸 건 이렇습니다.저는 항상 전체 주문에 대한 요금을 어떻게 계산해야 할지 모르겠어요.

$order_id       = isset($_POST['order_id'])?$_POST['order_id']:0;
$country        = isset($_POST['country'])?$_POST['country']:0;
$state          = isset($_POST['state'])?$_POST['state']:0;
$postcode       = isset($_POST['postcode'])?$_POST['postcode']:0;
$city           = isset($_POST['city'])?$_POST['country']:0;
$order          = wc_get_order( $order_id );
$order_items    = $order->get_items();

// Don't know if this would save country of logged in user, or only create a temporary guest user session which is what I'd need
if ( $country != '' ) {
    WC()->customer->set_billing_location( $country, $state, $postcode, $city );
    WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
    WC()->customer->set_billing_address_to_base();
    WC()->customer->set_shipping_address_to_base();
}

// Remove all current items from cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    WC()->cart->empty_cart();
}

// Add all items from the order to the cart
foreach ($order_items as $order_item) {
    WC()->cart->add_to_cart($order_item['product_id'], $order_item['qty']);
}

$totals = WC()->shipping->get_packages();

// $totals returns rates but I believe it is per each "package". It's not a cumulative rate like the cart page shows.

네, @muju only 덕분에 알 수 있었습니다.

다음은 카트 페이지에 표시된 것과 동일한 방법으로 계산된 배송료를 얻는 방법입니다.

// Post variables
$order_id   = isset($_POST['order_id'])?$_POST['order_id']:0;
$country    = isset($_POST['country'])?$_POST['country']:0;
$state      = isset($_POST['state'])?$_POST['state']:0;
$postcode   = isset($_POST['postcode'])?$_POST['postcode']:0;
$city       = isset($_POST['city'])?$_POST['city']:0;

// Order and order items
$order          = wc_get_order( $order_id );
$order_items    = $order->get_items();

// Reset shipping first
WC()->shipping()->reset_shipping();

// Set correct temporary location
if ( $country != '' ) {
    WC()->customer->set_billing_location( $country, $state, $postcode, $city );
    WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
    WC()->customer->set_billing_address_to_base();
    WC()->customer->set_shipping_address_to_base();
}

// Remove all current items from cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    WC()->cart->empty_cart();
}

// Add all items to cart
foreach ($order_items as $order_item) {
    WC()->cart->add_to_cart($order_item['product_id'], $order_item['qty']);
}

// Calculate shipping
$packages = WC()->cart->get_shipping_packages();
$shipping = WC()->shipping->calculate_shipping($packages);
$available_methods = WC()->shipping->get_packages();

$available_methods[0]['rates']에는 주문 내 제품에 대해 해당 지역에서 이용 가능한 모든 배송료가 적용됩니다.

사용 방법:

// Calculate totals
WC()->cart->calculate_totals();
WC()->cart->calculate_shipping();

// Retrieve the shipping total
$shipping_total = WC()->cart->get_shipping_total();

WC_Cart 클래스에는 필요한 카트 기능을 재생성하는 데 필요한 모든 방법이 포함되어 있어야 합니다.여기서의 클래스 정의를 끝까지 읽고 숙지해 둘 것을 제안합니다.

https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html

주의: 답변이 아닙니다. 코멘트를 드릴 수 없기 때문에 여기에 글을 씁니다.내 경우, 승인된 답변에 문제가 있습니다.

고객이 이미 카트에 일부 아이템을 추가한 경우 다음 코드 행으로 인해 아이템이 손실됩니다.

WC()->cart->empty_cart()

대신 카트는 과거 주문 품목으로 채워집니다.

현재까지는 배송 패키지를 올바르게 계산하기 위해 카트를 사용할 필요가 있는 것 같습니다.그렇다면 현재 카트의 클론을 생성하여 배송 패키지를 계산할 수 있는 방법이 있습니까?이 계산을 할 때 고객의 원래 카트에 영향을 주고 싶지 않습니다.

언급URL : https://stackoverflow.com/questions/55151092/calculate-shipping-methods-rates-within-an-existing-order-in-woocommerce

반응형