/* 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;
}