programing

WooCommerce 카트 항목 이름 변경

minimums 2023. 9. 25. 22:29
반응형

WooCommerce 카트 항목 이름 변경

상품명은 결제 게이트웨이로 전달되는 대로 변경하되, 상품 페이지에 표시하기 위해 그대로 두는 것이 목표입니다.

저는 이것을 제 기능으로.php:

function change_item_name( $item_name, $item ) {
    $item_name = 'mydesiredproductname';
    return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'change_item_name', 10, 1 );

하지만 저한테는 잘 안 되는 것 같아요.실제 아이템 아이디 같은 걸 입력해야 할 것 같아요.어떻게 해야 할지 잘 모르겠어요.

제가 여기서 무엇을 잘못하고 있는지 어떤 정보라도 주시면 대단히 감사하겠습니다.

필터 후크는 프론트 엔드 후크이며 다음 위치에 있습니다.

1) WooCommerce 템플릿:

  • e-메일/plain/이메일-주문-items.php
  • templates/order/order- details-item.
  • 템플릿/checkout/폼페이php
  • 템플릿/emails/이메일-주문-items.php

2)WooCommerce Core 파일:

  • 포함 /class-wc-structured-data.php

각 인수에는 $item_name 공통 첫 번째 인수가 있고 다른 인수에는 다릅니다.
자세한 내용은 여기를 참조하십시오.

함수에 2개의 인수가 설정되어 있으며(두 번째 인수는 모든 템플릿에 대해 정확하지 않음) 후크에 하나만 선언합니다.아래 코드를 테스트했습니다.

add_filter( 'woocommerce_order_item_name', 'change_orders_items_names', 10, 1 );
function change_orders_items_names( $item_name ) {
    $item_name = 'mydesiredproductname';
    return $item_name;
}

그리고 작동합니다.

  • 주문접수(감사합니다)페이지,
  • 이메일 알림
  • and My Account Orders > 단일 주문 내역

하지만 카트, 체크아웃 및 백엔드 주문 편집 페이지에는 없습니다.

따라서 카트와 체크아웃에서 작동시켜야 하는 경우에는 다음과 같은 다른 후크를 사용해야 합니다.
그러면 setter와 getter를 사용할 수 있습니다.

여기 당신의 새로운 코드가 있습니다.

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // Get an instance of the WC_Product object
        $product = $cart_item['data'];

        // Get the product name (Added Woocommerce 3+ compatibility)
        $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;

        // SET THE NEW NAME
        $new_name = 'mydesiredproductname';

        // Set the new name (WooCommerce versions 2.5.x to 3+)
        if( method_exists( $product, 'set_name' ) )
            $product->set_name( $new_name );
        else
            $product->post->post_title = $new_name;
    }
}

코드는 활성 하위 테마(또는 테마)의 모든 php 파일 또는 플러그인 php 파일에 들어갑니다.

이제 상점 보관소와 제품 페이지를 제외한 모든 곳에서 이름을 변경할 수 있습니다.

이 코드는 WooCommerce 2.5+ 및 3+에서 테스트되고 작동합니다.

원래 품목 이름만 카트에 보관하려면 이 조건부 WooCommerce 태그를 기능 안에 추가해야 합니다.

if( ! is_cart() ){
    // The code
}

이 답변은 2017년 8월 1일 이전 버전의 우커머스 호환성을 얻기 위해 업데이트되었습니다.

언급URL : https://stackoverflow.com/questions/44994359/changing-woocommerce-cart-item-names

반응형