programing

wocommerce 한 페이지에 카트 버튼에 추가 후 컨텐츠 추가

javajsp 2023. 10. 21. 09:56

wocommerce 한 페이지에 카트 버튼에 추가 후 컨텐츠 추가

단품 페이지에서 짧은 설명 후 컨텐츠 추가에 성공하였습니다.

if (!function_exists('my_content')) {
    function my_content( $content ) {
        $content .= '<div class="custom_content">Custom content!</div>';
        return $content;
    }
}

add_filter('woocommerce_short_description', 'my_content', 10, 2);

저도 봤어요.short-description.php있었다apply_filters( 'woocommerce_short_description', $post->post_excerpt )

그래서 저는 그것에 꽂혔습니다.

마찬가지로 카트에 추가 버튼 뒤에 내용을 추가하고 싶어서 찾은 것입니다.do_action( 'woocommerce_before_add_to_cart_button' ), 그리고 지금 나는 갈고리를 걸고 있습니다.woocommerce_before_add_to_cart_button. 사용하고 있습니다.

if (!function_exists('my_content_second')) {
    function my_content_second( $content ) {
        $content .= '<div class="second_content">Other content here!</div>';
        return $content;
    }
}

add_action('woocommerce_after_add_to_cart_button', 'my_content_second');

하지만 아무 일도 일어나지 않습니다.안에 갈고리만 걸 수 있습니까?apply_filters? 제가 지금까지 후크를 사용해서 이해한 바로는 후크 이름만 있으면 후크를 걸 수 있는 것으로 알고 있습니다.첫 번째는 필터 훅이었기 때문에 제가.add_filter, 그리고 두번째는 액션 훅이라서 사용해야합니다.add_action, 모든 것이 작동할 겁니다.그럼 왜 안 되는 거지?

여기서는 add_action hook 그대로의 내용을 에코해야 합니다.

add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
/*
 * Content below "Add to cart" Button.
 */
function add_content_after_addtocart_button_func() {

        // Echo content.
        echo '<div class="second_content">Other content here!</div>';

}

답례가 아니라 에코를 해야 합니다.

add_action( 'woocommerce_after_add_to_cart_button', 'ybc_after_add_to_cart_btn' );
 
function ybc_after_add_to_cart_btn(){
    //add text OR HTML here 
    echo '<p>After custom text here</p>';
}

상점 보관 페이지에서 동일한 내용을 원할 경우 필터를 사용하여 카트에 추가 버튼을 수정해야 합니다.

액션 훅'을 사용할 때는 php에서 내용(html)을 추가하는 것이 쉬울 것입니다.

if (!function_exists('my_content_second')) {
    function my_content_second( $content ) {
        ?>
        <div class="second_content">Other content here!</div>;
        <?php
    }
}

add_action('woocommerce_after_add_to_cart_button', 'my_content_second');

동적 컨텐츠를 추가해야 하는 경우 변수를 사용하여 해당 컨텐츠를 반향하거나 일부 조건을 사용하여 추가할 수 있습니다.


필터 후크는 기존 내용을 수정하는 데 유용하며 반환문이 필요합니다(수정).

액션 후크는 대부분 내용을 추가하는 데 유용합니다.

위 코드는 모든 상품 페이지에 동일한 링크를 추가합니다.함수에 삽입합니다.단일 제품 페이지만 편집할 수 있는 php 파일:

add_action( 'woocommerce_after_add_to_cart_button', 'additional_single_product_button_2', 20 );
function additional_single_product_button_2() {
    global $product;
    // Define your targeted product IDs in the array below 
    $targeted_product_ids = array( put product ID here );

    if( in_array( $product->get_id(), $targeted_product_ids ) ) {
        
        $link = 'put your link here';
        $name = esc_html ( "button with external link", "woocommerce" ); // <== Here set button name 
        $class = 'button alt';
        $style = 'margin-left: 1em';
    
        // Output
        echo '<button type="button" style="' . $style . '" class="' . $class . '" onclick="window.open(\'' . $link . '\', \'_blank\')">' . $name . '</button>';
    }
}

다른 링크와 함께 추가적인 다른 제품 페이지에 대한 규칙을 가지려면 함수 이름을 변경해야 합니다.

언급URL : https://stackoverflow.com/questions/28875514/adding-content-after-add-to-cart-button-on-woocommerce-single-page