programing

Woocommerce:폐지된 "woocommerce_add_order_item_meta"를 대체할 후크

iphone6s 2023. 2. 26. 09:17
반응형

Woocommerce:폐지된 "woocommerce_add_order_item_meta"를 대체할 후크

주문 항목에 커스텀 메타를 추가해야 합니다.검색해보니 대부분의 기사에서는 "woocommerce_add_order_item_meta" 후크를 사용하라고 합니다.이 후크는 최신 버전 2.3.7에서 더 이상 사용되지 않습니다. 대신 어떤 후크를 사용해야 하는지 알려 주십시오.

http://docs.woothemes.com/wc-apidocs/function-woocommerce_add_order_item_meta.html

2017/2018 올바른 방법 (새로운 CRUD 설정기 및 Getters 메서드 사용)

관련:Woocommerce 3.4에서 woocommerce_add_order_item_meta 훅을 바꿉니다.

것을 3 훅은 woocommerce 3 입니다.woocommerce_add_order_item_meta.3woocommerce 3.3에서도 합니다.

이 후크는 다음에 의해 유효하게 됩니다.WC_Checkout클래스 메서드 및 관련 함수는 체크 아웃 프로세스에서 제외됩니다.WC_Order카트 데이터를 더 이상 사용할 수 없는 클래스입니다.

Woocommmerce 3이 새로운 CRUD 세터getters 메서드를 도입했기 때문에 카트 데이터와 유사한 유용한 인수를 가진 교체 훅이 사용됩니다.

카트의 데이터에 액세스 할 수 없기 때문에,는 정말로 편리하지 않습니다.

의 조작 방법을 참조해 주세요.사용할 수 있는 인수는 4개입니다.

  • $item 의 예다.WC_Order_Item_Product 도입
  • $cart_item_key 카트 항목의 고유 해시 키입니다.
  • $values 카트 항목입니다.
  • $order WC_Order 객체의 인스턴스(특정 경우에 따라서는 매우 유용한 추가 인수)

함수 wc_add_order_item_meta()로.WC_Data update_meta_data() 사용하는 방법$item★★★★★★ 。

예:

## --- New way --- ##
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $item->get_product_id(), '_meta_key', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        $item->update_meta_data( 'meta_key1', $custom_field_value );
    }
    // … … Or … …

    // Get cart item custom data and update order item meta
    if( isset( $values['custom_data'] ) ) {
        $item->update_meta_data( 'meta_key2', $values['custom_data'] );
    }
}

할 수 .woocommerce_add_order_item_meta훅은 다음과 같은 유용한 인수를 가지고 있습니다.

## --- Old way --- ##
add_action( 'woocommerce_add_order_item_meta', 'custom_add_order_item_meta', 20, 3 );
function custom_add_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $values['data']->get_id(), '_meta_key', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        wc_add_order_item_meta( $item_id, 'meta_key1', $custom_field_value );
    }
    // … … Or … …

    // Get cart item custom data and update order item meta
    if( isset( $values['custom_data'] ) ) {
        wc_add_order_item_meta( $item_id, 'meta_key2', $values['custom_data'] );
    }
}

★★★★ woocommerce_checkout_create_order_line_itemWooCommerce 3+의 CRUD의 getters.

★★을 wc-deprecated-functions.php 알게 될 것이다

/**
 * @deprecated
 */
function woocommerce_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
    return wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique );
}

기본적으로 함수는 다음과 같이 이름이 변경되었습니다.wc_add_order_item_meta()기능이 필요한 경우는, 그 기능을 사용해 주세요.작업 후크의 이름은 변경되지 않았으며 현재 위치에 있습니다.class-wc-checkout.php같이요.

// Allow plugins to add order item meta
do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );

버전 3.0.4에서는 훅도 폐지된 것 같습니다.이 알림이 표시됩니다.

The The "woocommerce_add_order_item_meta" hook uses out of date data structures and function is deprecated since version 3.0.4. Replace with woocommerce_new_order_item.

문제가 되는 플러그인의 add_action 스테이트먼트에서 액션 이름 'woocommerce_add_order_item_meta'를 'woocommerce_new_order_item'으로 대체하여 폐지 통지가 사라졌습니다.문제는 현재 일부 파라미터가 a 내부에 표시되는 것입니다.legacy_valuesarray. 플러그인 YITH WooCommerce Product Add Ons를 사용하고 있는데, 주문에 첨부해야 할 제품 메타 데이터가 플러그인에 의해 픽업되지 않아 주문과 함께 저장되지 않습니다.따라서 플러그인으로 이 문제를 해결할 때까지 권장 해제 알림을 감수해야 합니다.

이미 답변이 끝난 것으로 알고 있으며, 이미 수락된 답변이 있습니다.실제로 추천되지 않는 메시지를 받지 않고 이 문제를 해결할 수 있는 다른 방법을 제공하고자 했을 뿐입니다(참조 참조).

add_action('woocommerce_new_order_item', 'saveMetaData', 10, 3); // or use just 2 instead of 3; if you don't need order id

/**
 * Add meta to order item
 *
 * @param int $itemId
 * @param WC_Order_Item_Product|WC_Order_Item_Shipping $item
 * @param int @orderId
*/
function saveMetaData($itemId, $item, $orderId)
{
    if (!isItemValid($item))
    {
        return;
    }

    wc_add_order_item_meta($itemId, 'my_custom_data', $item->legacy_values['my_custom_data']);
}

/**
 * @param WC_Order_Item_Product|WC_Order_Item_Shipping $item
 *
 * @return bool
*/
function isItemValid($item)
{
    return (
        $item instanceof WC_Order_Item_Product &&
        isset($item->legacy_values) &&
        isset($item->legacy_values['my_custom_data']) &&
        !empty($item->legacy_values['my_custom_data'])
    );
}

구체적인 사용 사례는 명확하지 않지만(이 메타 정보를 추가해야 할 시기와 위치를 지정하지 않았습니다)woocommerce_checkout_update_order_meta체크 아웃중에.

체크아웃 필드 커스터마이즈에 대한 자세한 내용은 여기를 참조하십시오.

아니요, 후크도 권장되지 않는 것 같습니다.PHP 오류:"woocommerce_add_order_item_meta" 후크는 오래된 데이터 구조를 사용하며, 이 함수는 버전 3.1.2 이후 사용되지 않습니다.woocommerce_new_order_item으로 대체합니다.

https://docs.woocommerce.com/wc-apidocs/hook-docs.html에서도 찾을 수 없습니다.

Ilgüt Yldürlim의 답변에 추가하고 싶었습니다.내 경우, 커스텀 값은 item->legacy_values 배열에 존재하지 않았습니다.이를 수정하기 위해 woocommerce_checkout_create_order_line_item hook을 사용하여 woocommerce_new_order_item hook을 호출하기 전에 항목에 커스텀 값을 추가합니다.예를 들어 다음과 같습니다.

add_action('woocommerce_checkout_create_order_line_item', 'save_values_in_item', PHP_INT_MAX, 4 );

함수 save_values_in_item($item, $sign_item_key, $values, $order) {

                $item->myCustomValues = $values;

}

//그러면 새로운 후크를 호출합니다.add_action ( 'woocommerce_new_order_item', 'add_product_input_fields_to_order_item_wc3', PHP_INT_MAX, 3);

함수 add_product_input_fields_to_order_item_dem_wc3($item_id, $item, $order_id) {

            if ( isset( $item->myCustomValues ) ) 
            {
                  //iterate through array and place desired values into the meta data using the wc_add_order_item_meta function
            }

}

확실히 하기 위해 이 기능은 폐지되었지만, 은 아직 정상입니다.

언급URL : https://stackoverflow.com/questions/29666820/woocommerce-which-hook-to-replace-deprecated-woocommerce-add-order-item-meta

반응형