Woocommerce Variation ID를 얻는 방법
이 코드는 이미 함수에 적용되어 있습니다.php
이 코드는 각 제품 변동에 새로운 필드를 추가하여 날짜 입력 필드를 가지기 때문에 현재 날짜가 입력 날짜를 넘으면 변동 내용이 자동으로 만료됩니다.
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
//Create New fields for Variations
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field_date_expire[' . $variation->ID . ']',
'class' => '_text_field_date_expire',
'type' => 'datetime-local',
'label' => __( 'Date of Expiration', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Expiration of Each Product Variation', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field_date_expire', true )
)
);
}
//Save New Fields for Variation
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field_date_expire'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field_date_expire', esc_attr( $text_field ) );
}
}
이것은 Wocommerce의 각 변형에 새로운 필드를 추가하는 것입니다.
각 제품의 유효기간이 지난 변형을 삭제할 수 있도록 각 "_text_field_date_expire" 메타를 가져오려고 합니다.
함수의 각 woocommerce 바리에이션 ID를 취득하려면 어떻게 해야 합니까?php 그럼 현재 날짜가 지났을 때 만료된다는 규칙을 추가할 수 있을까요?
var_dump(get_post_meta( $variation_id , '_text_field_date_expire', true ));
이 코드는 datetime 필드를 가져오는 데 사용하는 코드이지만 각 코드를 가져오는 방법은 확실하지 않습니다.$variation_id필드?
이것을 사용해 보세요.
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => get_the_ID() // get parent post-ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
// get variation ID
$variation_ID = $variation->ID;
// get variations meta
$product_variation = new WC_Product_Variation( $variation_ID );
// get variation featured image
$variation_image = $product_variation->get_image();
// get variation price
$variation_price = $product_variation->get_price_html();
get_post_meta( $variation_ID , '_text_field_date_expire', true );
}
이게 도움이 되길 바라.상세한 것에 대하여는, 다음을 참조해 주세요.
WooCommerce 3+에서는$variationfunction 인수:의 인스턴스입니다.
방법get_id()클래스에서 상속됩니다.
따라서 코드에서는 다음과 같이 해야 합니다.
'id' => '_text_field_date_expire[' . $variation->get_id() . ']',
WooCommerce 3 이후 모든 것
WC_Product속성에 직접 액세스할 수 없습니다.대신 사용 가능한 방법을 사용해야 합니다.
후크 기능에서도save_variation_settings_fields()2개의 인수를 선언하고 있기 때문에 1개가 누락되어 있습니다.다음 중 하나여야 합니다.
//Save New Fields for Variation// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
// Text Field
$text_field = $_POST['_text_field_date_expire'][ $variation_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $variation_id, '_text_field_date_expire', esc_attr( $text_field ) );
}
}
언급URL : https://stackoverflow.com/questions/47646939/how-to-get-woocommerce-variation-id
'programing' 카테고리의 다른 글
| WP REST API 요청 여부를 확인하시겠습니까? (0) | 2023.04.05 |
|---|---|
| jsonp with jquery (0) | 2023.04.05 |
| mongo 쉘에 모든 데이터베이스를 나열하려면 어떻게 해야 합니까? (0) | 2023.04.05 |
| 문자열을 jsx로 변환하려면 어떻게 해야 합니까? (0) | 2023.04.05 |
| WordPress에서 프로그래밍 방식으로 게시물을 만드는 방법 (0) | 2023.04.05 |