programing

Woocommerce, "카트에 추가" 메시지 편집 방법

linuxpc 2023. 3. 27. 21:04
반응형

Woocommerce, "카트에 추가" 메시지 편집 방법

카트에 추가 버튼을 클릭하면 Woocommerce에 메시지, 보기 카트, 이 메시지를 편집하고 싶다, 실제로 모든 스팬을 편집하고 싶다, 아이콘 등을 표시합니다.

테마/기능.php 에 필터를 추가합니다.아래 코드는 기존 $message보다 우선합니다.그러면 메시지에 "체크아웃" 링크를 추가하는 거의 동일한 메시지로 $message를 덮어씁니다.

반드시 $메시지를 돌려주세요.

물론 기존 메시지만 수정할 수 있습니다.이것 전체가 첫 번째 param 또는 $message var를 통해 문자열로 전달되기 때문입니다.

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
    $titles[] = get_the_title( $product_id );

    $titles = array_filter( $titles );
    $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );

    $message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>',
                    esc_html( $added_text ),
                    esc_url( wc_get_page_permalink( 'checkout' ) ),
                    esc_html__( 'Checkout', 'woocommerce' ),
                    esc_url( wc_get_page_permalink( 'cart' ) ),
                    esc_html__( 'View Cart', 'woocommerce' ));

    return $message;
}

다음과 같은 필터를 사용해 본 적이 있습니까?

function your_add_to_cart_message() {
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
    $message = sprintf( '%s<a href="%s" class="your-style">%s</a>', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) );
else :
    $message = sprintf( '%s<a href="%s" class="your-class">%s</a>', __( 'Successfully added to cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'View Cart', 'woocommerce' ) );
endif;
return $message;
}
add_filter( 'wc_add_to_cart_message', 'your_add_to_cart_message' );

Ajax 메시지 업데이트에 대한 응답으로 다음과 같은 변환 기능을 사용해 보십시오.

function your_woo_ajax_solution( $translation, $text, $domain ) {
  if ( $domain == 'woocommerce' ) { // your domain name
    if ( $text == 'View Cart' ) { // current text that shows
        $translation = 'Basket updated.'; // The text that you would like to show
    }
  }

  return $translation;
}
add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 );

2017 - 2019 - Woocommerce 3+용 (카트에 추가된 여러 제품 취급)

필터 후크로 대체되어 두 번째 함수 인수가 (대신)…로 변경되었습니다.

다음 스레드와 같이 후크 함수의 코드를 변경할 수 있습니다.

add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message_html', 10, 2 );
function custom_add_to_cart_message_html( $message, $products ) {
    $titles = array();
    $count  = 0;

    foreach ( $products as $product_id => $qty ) {
        $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
        $count += $qty;
    }

    $titles     = array_filter( $titles );
    $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );

    // The custom message is just below
    $added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ),
        $count, __("been added to your basket.", "woocommerce") );

    // Output success messages
    if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
        $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
    } else {
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
    }
    return $message;
}

관련 스레드(Woocommerce 3+의 경우):

보시면add-to-cart.js방아쇠를 당기다added_to_cart장바구니에 제품을 추가할 때.거기에 꽂혀서 이렇게 했어요.

jQuery(document.body).on("added_to_cart", function( data ) {
    jQuery('button.added').nextAll().remove();
    jQuery('button.added').after(' <span style="text-align:center;display:block;" class="cart_updated_ajax"><a href="' + wc_add_to_cart_params.cart_url + '" title="' +
                            wc_add_to_cart_params.i18n_view_cart + '">Cart Updated</a></span>');
});

카트에 제품을 추가한 후 원하는 항목을 추가할 수 있습니다.

도움이 됐으면 좋겠네요!

Woocommerce 3.0에서 "wc_add_to_cart_message"는 사용되지 않으며 이상 작동하지 않습니다.따라서 @zmonteca의 답변은 괜찮았지만 Woocommerce 3.0에서는 동작하지 않습니다.

"wc_add_to_cart_message"를 "wc_add_to_cart_message_html"로 대체하고 voile...작동하다.

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );

$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );

$message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>',
                esc_html( $added_text ),
                esc_url( wc_get_page_permalink( 'checkout' ) ),
                esc_html__( 'Checkout', 'woocommerce' ),
                esc_url( wc_get_page_permalink( 'cart' ) ),
                esc_html__( 'View Cart', 'woocommerce' ));

return $message;}

@Dante가 맞습니다.샵 페이지의 @BradleyD에서 제공하는 솔루션은 ax_add_to_cart에서는 동작하지 않습니다.

@Abstract에서 제공하는 솔루션은 예상대로 작동하고 있습니다.나도 그의 해결책을 사용하고 있다.

또 다른 jQuery 접근법은 문서 오브젝트에서 ajaxSuccess 이벤트를 수신하고 클릭된 버튼을 원하는 대로 수정하는 것입니다.

다음과 같은 것이 작동해야 합니다.

$(document).ajaxSuccess(function(event, xhr, settings) {
    if (settings.url.indexOf('?wc-ajax=add_to_cart') !== -1) {
        // You can find the clicked button element under the event.target.activeElement
        // Than you can do whatever you want here. Add new html element and text, etc.
    }

});

언급URL : https://stackoverflow.com/questions/25880460/woocommerce-how-to-edit-the-added-to-cart-message

반응형