우커머스:체크아웃 필드 값 설정
제 시스템에 아직 WP 사용자가 아닌 특별한 고객 그룹의 경우, 한정된 제품 세트 중에서 선택할 수 있는 특별한 페이지로 안내합니다.저는 이미 그들의 모든 정보를 가지고 있고 이 랜딩 페이지에 미리 입력할 것입니다.정보를 확인하면 카트에 제품을 추가하고 바로 계산대로 건너뜁니다.지금까지 그 모든 것을 다 알고 있습니다.
제가 하고 싶은 일은 제가 가지고 있는 고객 이름과 청구 정보를 체크아웃 데이터에 미리 입력하는 것인데, 어떻게 해야 할지 완전히 모르겠습니다.하지만 지금까지 제가 얻은 것은 다음과 같습니다.
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
$fields['billing']['billing_first_name']['value'] = 'Joe';
var_dump( $fields );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
체크아웃 필드의 값을 변경하려면 어떻게 해야 합니까?위의 코드에서는 작동하지 않습니다.하나만 제대로 알려주면 나머지는 내가 할 수 있습니다.
여기를 찾아봤지만 제가 찾던 것도 잘 찾지 못했습니다.
감사합니다!
체크아웃 필드를 미리 채우기 위해 만들어졌으며 정의된 전용 WooCommerce 후크를 사용해야 합니다.WC_Checkout
방법:
add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function populating_checkout_fields ( $value, $input ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
// Define your checkout fields values below in this array (keep the ones you need)
$checkout_fields = array(
'billing_first_name' => 'John',
'billing_last_name' => 'Wick',
'billing_company' => 'Murders & co',
'billing_country' => 'US',
'billing_address_1' => '7 Random street',
'billing_address_2' => 'Royal suite',
'billing_city' => 'Los Angeles',
'billing_state' => 'CA',
'billing_postcode' => '90102',
'billing_phone' => '555 702 666',
'billing_email' => 'jhon.wick@murders.com',
'shipping_first_name' => 'John',
'shipping_last_name' => 'Wick',
'shipping_company' => 'Murders & co',
'shipping_country' => 'USA',
'shipping_address_1' => '7 Random street',
'shipping_address_2' => 'Royal suite',
'shipping_city' => 'Los Angeles',
'shipping_state' => 'California',
'shipping_postcode' => '90102',
// 'account_password' => '',
'order_comments' => 'This is not for me',
);
foreach( $checkout_fields as $key_field => $field_value ){
if( $input == $key_field && ! empty( $field_value ) ){
$value = $field_value;
}
}
}
return $value;
}
코드가 작동합니다.활성 하위 테마(또는 테마)의 php 파일 또는 플러그인 파일에 있습니다.
사용자가 로그인하지 않은 경우 사용자의 상태에 다음을 추가할 수 있습니다.
if( 'testtoken' == $token && ! is_user_logged_in() ) {
이 코드는 테스트되고 작동합니다(특정 코드 조건에서는 테스트되지 않음).테스트를 위해 사용해본 적이 있습니다.! is_user_logged_in()
조건으로
함수에 정의된 배열에 대해 다음을 얻을 수 있습니다.
필터를 사용하면 정보를 수정할 수 있지만 기능에서 해당 정보를 반환해야 합니다.
그럼, 이 경우에, 당신은 단순히 당신이 그를return $fields;
사용자 기능:
function onboarding_update_fields( $fields = array() ) {
// check if it's set to prevent notices being thrown
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
// yoda-style to prevent accidental assignment
if( 'testtoken' == $token ) {
// if you are having issues, it's useful to do this below:
var_dump( $fields );
// remove the var_dump once you've got things working
// if all you want to change is the value, then assign ONLY the value
$fields['billing']['billing_first_name']['value'] = 'Joe';
// the way you were doing it before was removing core / required parts of the array - do not do it this way.
// $fields['billing']['billing_first_name']['value'] = array( 'value' => 'Joe');
}
// you must return the fields array
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
업데이트:
어떤 이유에서인지 위의 내용이 작동하지 않는 것을 보고 다른 플러그인의 코드를 냄새를 맡았는데, 그들이 그렇게 하는 방식(그리고 그것은 분명히 작동합니다)은 다음과 같습니다.
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
// Assign the value to the $_POST superglobal
$_POST['billing_first_name'] = 'Joe';
}
return $fields;
}
따라서 사용자가 입력한 정보를 덮어쓰거나 스톰프하지 않았다는 점을 긍정적으로 인식하기 위해서는 이와 같은 작업을 고려해 보는 것이 좋습니다(물론 이 작업이 제대로 작동하는지 테스트해 보는 것도 좋습니다).
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
// Assign the value to the $_POST superglobal ONLY if not already set
if ( empty( $POST['billing_first_name'] ) ) {
$_POST['billing_first_name'] = 'Joe';
}
}
return $fields;
}
다음과 같이 'default'를 사용하십시오.
$fields['billing']['billing_first_name']['default'] = "Thomas";
WooCommerce 참조 | 과금 필드 값 설정
언급URL : https://stackoverflow.com/questions/45602936/woocommerce-set-checkout-field-values
'programing' 카테고리의 다른 글
ASP.NET 핵심 데이터 암호화/보호 (0) | 2023.10.04 |
---|---|
C에서 변수 선언을 해제(삭제)하는 방법은? (0) | 2023.10.04 |
Wordpress Woocommerce 제품을 카트 새로 고침 페이지에 추가하고 있습니다. (0) | 2023.10.04 |
Submit 버튼을 클릭한 후 비활성화하고 몇 초 후 다시 활성화합니다. (0) | 2023.10.04 |
각도로 앵커 태그에 클릭 부착 (0) | 2023.10.04 |