상품 페이지에 배송비 표시 - WooCommerce
각 3가지 변형(월, 6개월, 12개월)의 형태로 2개의 구독 유형을 보여주는 맞춤 페이지를 워드프레스에서 만들려고 합니다.각 버전마다 라디오 버튼이 나오고 사용자가 라디오 버튼을 클릭하면 총 가격이 생중계됩니다.이 부분은 잘 되고 있습니다.
하지만 지금은 3개의 라디오 버튼을 추가해서 배송방법을 선택하려고 합니다. (사용자가 1개를 선택하면 총 가격도 라이브 업데이트 됩니다.
제품 배송비를 어떻게 구해야 하는지 오랫동안 찾아봤지만 아무 것도 작동하지 않습니다.
이 질문은 너무 광범위합니다.그래서 부분적으로 답변을 드릴 수 있습니다. 직접 작업을 해보셔야 하고, 나중에 좀 더 구체적인 질문을 할 필요가 있습니다.
이제 제품 페이지에서 배송 데이터를 설정하는 올바른 방법은 Ajax를 사용하여 데이터를 업데이트하는 것입니다. 이 작업은 고객이 클라이언트 측(브라우저)에서 수행하기 때문에 '게시'를 피하고 페이지를 다시 로드합니다.하지만 이건 당신의 일이 될겁니다
1) 고객 위치(배송 구역의 경우):
고객 위치나 배송 지역을 먼저 파악하셔야 합니다.
그런 다음 고객 국가를 업데이트해야 합니다.WC()->session
그리고 안에WC()->customer
물건들.이 작업은 다음과 같이 수행할 수 있습니다.
## Get the geolocated customer country code *(if enabled)*:
$country_code = WC()->customer->get_billing_country();
// or
// $country_code = WC()->customer->get_shipping_country();
## Set a new country code
$new_country_code = 'FR';
## 1. WC_session: set customer billing and shipping country
// Get the data
$customer_session = WC()->session->get( 'customer' );
// Change some data
$customer_session['country'] = $new_country_code; // Billing
$customer_session['shipping_country'] = $new_country_code; // Shipping
// Set the changed data
$customer_session = WC()->session->set( 'customer', $customer_session );
## 2. WC_Customer: set customer billing and shipping country
WC()->customer->set_billing_country( $new_country_code );
WC()->customer->set_shipping_country( $new_country_code );
2) 배송 방법(배송 구역별, 비용 포함):
Wocommerce에서는 고객이 카트에 제품을 추가하는 경우에만 배송 구역에 대한 배송 방법을 사용할 수 있습니다.
다음 답변 코드를 기준으로 합니다.관리 패널에서와 같이 프론트엔드로 배송 방법을 표시합니까?
우리는 비용과 필요한 모든 것을 포함하여 Zone 배송 방법을 얻기 위해 필요한 데이터를 맞춤형으로 배열할 수 있습니다.
아래 코드는 훨씬 더 완전하며 배송비가 포함되어 있습니다.
// Initializing variable
$zones = $data = $classes_keys = array();
// Rest of the World zone
$zone = new \WC_Shipping_Zone(0);
$zones[$zone->get_id()] = $zone->get_data();
$zones[$zone->get_id()]['formatted_zone_location'] = $zone->get_formatted_location();
$zones[$zone->get_id()]['shipping_methods'] = $zone->get_shipping_methods();
// Merging shipping zones
$shipping_zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );
// Shipping Classes
$shipping = new \WC_Shipping();
$shipping_classes = $shipping->get_shipping_classes();
// The Shipping Classes for costs in "Flat rate" Shipping Method
foreach($shipping_classes as $shipping_class) {
//
$key_class_cost = 'class_cost_'.$shipping_class->term_id;
// The shipping classes
$classes_keys[$shipping_class->term_id] = array(
'term_id' => $shipping_class->term_id,
'name' => $shipping_class->name,
'slug' => $shipping_class->slug,
'count' => $shipping_class->count,
'key_cost' => $key_class_cost
);
}
// For 'No class" cost
$classes_keys[0] = array(
'term_id' => '',
'name' => 'No shipping class',
'slug' => 'no_class',
'count' => '',
'key_cost' => 'no_class_cost'
);
foreach ( $shipping_zones as $shipping_zone ) {
$zone_id = $shipping_zone['id'];
$zone_name = $zone_id == '0' ? __('Rest of the word', 'woocommerce') : $shipping_zone['zone_name'];
$zone_locations = $shipping_zone['zone_locations']; // array
$zone_location_name = $shipping_zone['formatted_zone_location'];
// Set the data in an array:
$data[$zone_id]= array(
'zone_id' => $zone_id,
'zone_name' => $zone_name,
'zone_location_name' => $zone_location_name,
'zone_locations' => $zone_locations,
'shipping_methods' => array()
);
foreach ( $shipping_zone['shipping_methods'] as $sm_obj ) {
$method_id = $sm_obj->id;
$instance_id = $sm_obj->get_instance_id();
$enabled = $sm_obj->is_enabled() ? true : 0;
// Settings specific to each shipping method
$instance_settings = $sm_obj->instance_settings;
if( $enabled ){
$data[$zone_id]['shipping_methods'][$instance_id] = array(
'$method_id' => $sm_obj->id,
'instance_id' => $instance_id,
'rate_id' => $sm_obj->get_rate_id(),
'default_name' => $sm_obj->get_method_title(),
'custom_name' => $sm_obj->get_title(),
);
if( $method_id == 'free_shipping' ){
$data[$zone_id]['shipping_methods'][$instance_id]['requires'] = $instance_settings['requires'];
$data[$zone_id]['shipping_methods'][$instance_id]['min_amount'] = $instance_settings['min_amount'];
}
if( $method_id == 'flat_rate' || $method_id == 'local_pickup' ){
$data[$zone_id]['shipping_methods'][$instance_id]['tax_status'] = $instance_settings['tax_status'];
$data[$zone_id]['shipping_methods'][$instance_id]['cost'] = $sm_obj->cost;
}
if( $method_id == 'flat_rate' ){
$data[$zone_id]['shipping_methods'][$instance_id]['class_costs'] = $instance_settings['class_costs'];
$data[$zone_id]['shipping_methods'][$instance_id]['calculation_type'] = $instance_settings['type'];
$classes_keys[0]['cost'] = $instance_settings['no_class_cost'];
foreach( $instance_settings as $key => $setting )
if ( strpos( $key, 'class_cost_') !== false ){
$class_id = str_replace('class_cost_', '', $key );
$classes_keys[$class_id]['cost'] = $setting;
}
$data[$zone_id]['shipping_methods'][$instance_id]['classes_&_costs'] = $classes_keys;
}
}
}
}
// Row output (for testing)
echo '<pre>'; print_r($data); echo '</pre>';
맞춤 배송 방법
이제 맞춤형 배송 방법(때로는 타사 배송 플러그인을 사용할 수 있음)을 사용하는 경우 코드를 약간 변경해야 합니다.비용 및 세금계산
세금 계산을 해야 합니다. 비용은 배송 설정에 따라 표시되기 때문입니다.
3) 상품페이지
고객 위치:
먼저 위치 선택기(배송 구역을 정의하기 위한)를 갖거나 Wocommerce 지리적 위치를 기준으로 위치를 설정해야 합니다.
배송 방법:
배송 구역이 정의되면 해당 배송 방법 및 요금(비용)을 확인할 수 있으며, 이 제품 페이지에 배송 방법에 대한 라디오 버튼이 표시됩니다.
이를 얻으려면 단일 제품 페이지를 변경해야 합니다.
- 테마를 통해 WC Subscriptions 템플릿 재지정(및 WC Subscriptions 템플릿도 포함)
- 사용 가능한 필터 및 작업 후크 사용
- 자바스크립트, jQuery를 이용하여 가격을 변경하고 Ajax를 이용하여 고객 데이터를 업데이트 합니다 (
WC_Session
그리고.WC_Customer
).
다음 코드(Ajax)로 "choosed_shipping_methods"를 가져오거나 설정/업데이트해야 합니다.
선택한 배송 방법 가져오기:
$chosen_shipping = WC()->session->get('chosen_shipping_methods')[0];
Choosed Shipping 방법 설정/업데이트 (Javascript/Ajax를 통해)admin-ajax.php
):
// HERE the new method ID
$method_rate_id = array('free_shipping:10');
// Set/Update the Chosen Shipping method
WC()->session->set( 'chosen_shipping_methods', $method_rate_id );
언급URL : https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce
'programing' 카테고리의 다른 글
워드프레스 WP_QUERY를 위해 $_POST를 소독하는 방법은? (0) | 2023.11.01 |
---|---|
TCP 연결이 열린 후 다른 포트로 이동합니까? (0) | 2023.11.01 |
PHP 파일을 직접 액세스로부터 보호 (0) | 2023.11.01 |
이 페이지에 Google 지도 API를 여러 번 포함시켰습니다. (0) | 2023.11.01 |
forceLayout(), requestLayout() 및 invalidate() 사용 (0) | 2023.10.27 |