Site Overlay

Woocommerce Bir kategoriyi Sepet Sayfasında Zorunlu Kılma

Aşağıdaki kod sayesinde Woocommerce bir kategoriyi sepet sayfasında zorunlu kılma işlemi gerçekleştirebilirsiniz.

// Function that define the mandatory product category
 function your_mandatory_category_slug(){

     // DEFINE HERE the SLUG of the needed product category
    $category = 'baski-fiyatlari';
    return $category;
 }


// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category(){
    $category_needed = your_mandatory_category_slug();
    $has_cat = false;

    // Iterrating each item in cart and detecting…
    foreach ( WC()->cart->get_cart() as $item ) {

        // Detects if the needed product category is in cart items
        if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
            $has_cat = true;
            break;
        }
    }
    return $has_cat;
 }


// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
        $category_needed = your_mandatory_category_slug();

    // check that cart is not empty (for cart and product category archives)
    if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) ) ){
        $category_obj = get_term_by( 'slug', $category_needed, 'product_cat' );
        if ( is_wp_error( $category_obj ) ) return;

        // Display message when product category is not in cart items
        if ( !has_mandatory_category() ) {
            $category_name = $category_obj->name;
            $category_url = get_term_link( $category_needed, 'product_cat' );

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( __( '*** Ödeme yapmadan önce sepetinize <a href="%2$s"> "%1$s"</a> kategorisinden bir ürün eklemeniz gerekmektedir.</a>', 'your_theme_domain'), $category_name, $category_url ), 'error' );
        }
    }
}
add_action( 'woocommerce_before_main_content', 'mandatory_category_display_message', 30 ); // for product mandatory category archives pages
add_action( 'woocommerce_check_cart_items', 'mandatory_category_display_message' ); // for cat page


// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {

    // If cart is not empty on checkout page
    if( !WC()->cart->is_empty() && is_checkout() ){
        $category_needed = your_mandatory_category_slug();

        // If missing product category => redirect to the products category page
        if ( !has_mandatory_category() )
            wp_redirect( get_term_link( $category_needed, 'product_cat' ) );
    }
}
add_action('template_redirect', 'mandatory_category_checkout_redirect');
image 15
Woocommerce Bir kategoriyi Sepet Sayfasında Zorunlu Kılma

image 3
Arif Akyüz İçerik Üreticisi Siber Güvenlik Uzmanı

Arif Akyüz
İçerik Üreticisi & Siber Güvenlik Uzmanı
[email protected]