/* To add a date-type select field on the single product page for specific product categories and ensure that the product cannot be added to the cart unless the date is selected in WooCommerce */

// Add a date field to the product page for specific categories
add_action('woocommerce_before_add_to_cart_button', 'add_required_date_field_for_specific_categories');
function add_required_date_field_for_specific_categories() {
    global $product;

    // List of categories where the date field should be displayed
    $target_categories = array('flodeer', 'choria');

    // Check if the product belongs to the specified categories
    if (has_term($target_categories, 'product_cat', $product->get_id())) {
		echo '<div class="custom-date-select-field">';
		echo '<div class="custom-title-01">- ๊ฝƒํ’์„ ์€ ์ฃผ๋ฌธ ์ œ์ž‘ ์ƒํ’ˆ์œผ๋กœ ์ตœ์†Œ 3์ผ ์ „์— ์ฃผ๋ฌธํ•ด ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.</div>';
		echo '<div class="custom-title-02">- 3์ผ ์ดํ›„ ํฌ๋ง ๋ฐฐ์†ก์ผ ์˜ˆ์•ฝ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.</div>';
        echo '<div class="custom-title-03">- ์ง€์—ญ์— ๋”ฐ๋ฅธ ๋ฐฐ์†ก ์š”์ผ์„ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.</div>';
        echo '<label for="custom_date">๋ฐฐ์†ก๋ฐ›์œผ์‹ค ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.</label>';
        echo '<input type="date" id="custom_date" name="custom_date" />';
        echo '</div>';
    }
}
// Validate that the date field is filled before adding to cart
add_filter('woocommerce_add_to_cart_validation', 'validate_date_field_before_add_to_cart', 10, 3);
function validate_date_field_before_add_to_cart($passed, $product_id, $quantity) {
    // Check if the date field exists and is not empty
    if (isset($_POST['custom_date']) && empty($_POST['custom_date'])) {
        wc_add_notice(__('Please select a date before adding the product to the cart.'), 'error');
        $passed = false;
    }
    return $passed;
}
// Save the selected date to the cart item data
add_filter('woocommerce_add_cart_item_data', 'save_date_field_to_cart', 10, 2);
function save_date_field_to_cart($cart_item_data, $product_id) {
    if (isset($_POST['custom_date'])) {
        $cart_item_data['custom_date'] = sanitize_text_field($_POST['custom_date']);
    }
    return $cart_item_data;
}

// Display the selected date in the cart and checkout pages
add_filter('woocommerce_get_item_data', 'display_custom_date_in_cart', 10, 2);
function display_custom_date_in_cart($item_data, $cart_item) {
    if (isset($cart_item['custom_date'])) {
        $item_data[] = array(
            'name' => __('Selected Date'),
            'value' => sanitize_text_field($cart_item['custom_date']),
        );
    }
    return $item_data;
}

/* Shop ํŽ˜์ด์ง€์—์„œ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š” ์ œํ’ˆ์˜ "Add to Cart" ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ, ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ƒํ’ˆ์ด ์ถ”๊ฐ€๋˜์ง€ ์•Š๊ณ , ํ•ด๋‹น ์ œํ’ˆ์˜ ์ƒ์„ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด woocommerce_loop_add_to_cart_link ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Shop ํŽ˜์ด์ง€์—์„œ์˜ ๋ฒ„ํŠผ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  */

// Redirect Add to Cart button to the single product page for categories 'A' and 'B'
// Modify the Add to Cart URL on product pages
add_filter('woocommerce_product_add_to_cart_url', 'custom_redirect_to_single_product_page', 10, 2);

function custom_redirect_to_single_product_page($url, $product) {
    // ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ ์Šฌ๋Ÿฌ๊ทธ๋ฅผ ์„ค์ •
    $target_categories = array('flodeer', 'choria');

    // ์ƒํ’ˆ์ด ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š”์ง€ ํ™•์ธ
    if (has_term($target_categories, 'product_cat', $product->get_id())) {
        // ์ƒํ’ˆ ์ƒ์„ธ ํŽ˜์ด์ง€ URL๋กœ ๋ฆฌ๋””๋ ‰ํŠธ
        return get_permalink($product->get_id());
    }

    return $url;
}

// Modify the Add to Cart button in shop loop (e.g., shop page)
add_filter('woocommerce_loop_add_to_cart_link', 'custom_redirect_loop_add_to_cart_link', 10, 2);

function custom_redirect_loop_add_to_cart_link($button, $product) {
    // ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ ์Šฌ๋Ÿฌ๊ทธ๋ฅผ ์„ค์ •
    $target_categories = array('flodeer', 'choria');

    // ์ƒํ’ˆ์ด ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š”์ง€ ํ™•์ธ
    if (has_term($target_categories, 'product_cat', $product->get_id())) {
        // "Add to Cart" ๋ฒ„ํŠผ์„ ์ƒํ’ˆ ์ƒ์„ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰ํŠธํ•˜๋Š” ๋งํฌ๋กœ ๋ณ€๊ฒฝ
        $url = get_permalink($product->get_id());
        $button = '<a href="' . esc_url($url) . '" class="button">' . __('View Product') . '</a>';
    }

    return $button;
}

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *