programing

특정 제품 카테고리에 따른 WooCommerce 체크아웃 메시지

minimums 2023. 3. 4. 14:27
반응형

특정 제품 카테고리에 따른 WooCommerce 체크아웃 메시지

워드프레스 스토어는 WooCommerce를 사용하고 있으며, WooCommerce Checkout에 표시해야 하는 작은 구매 노트가 있지만 특정 제품을 구입할 때만 표시됩니다.

주문 버튼 아래에 표시되는 커스텀 메시지를 추가했습니다.그러나 카트에 무엇이 들어있든 그것은 나타난다.

현재 사용하고 있는 코드는 다음과 같습니다.

add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
echo '<div class="checkoutdisc">Custom message appears here fine.</div>';
}

이 행 앞에 추가할 수 있는 간단한 코드가 있습니까?이 코드는 특정 카테고리의 제품이 카트에 있을 때만 적용됩니다.

감사해요.

특별 카테고리의 제품 아이템이 카트에 있는 것을 확인합니다.조건이 일치하면(카트의 항목 중 하나에서) 메시지가 표시됩니다.

코드는 다음과 같습니다.

add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your special category name, slug or ID here:
    $special_cat = 'special_category';
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( has_term( $special_cat, 'product_cat', $item->id ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}

제품 카테고리 대신 제품 ID 배열을 사용할 수도 있습니다.

이 경우 코드는 약간 다릅니다.

add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your products IDs here:
    $product_ids = array( 31, 68, 87, 124);
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( in_array( $item->id, $product_ids ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}

이 코드는 기능합니다.php 파일 또는 임의의 플러그인 파일에 있는 활성 자식 테마(또는 테마)입니다.

이 코드는 테스트되어 동작합니다.

카트 내용물을 확인하셔야 할 것 같습니다.

add_action( 'woocommerce_after_checkout_form', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    $cart = WC()->cart;
    foreach ( $this->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if ( has_term( 'special-category', 'product_cat', $_product->id ) ){
            echo '<div class="checkoutdisc">Your custom message.</div>';
        }
    }
}

언급URL : https://stackoverflow.com/questions/39300332/woocommerce-checkout-message-based-on-specific-product-category

반응형