在 WooCommerce 中實現自訂購買時間限制

在現代電商中,靈活的銷售策略是吸引消費者的重要手段之一。使用 WooCommerce 平台,您可以為每個商品設置購買的開始和結束時間,從而實現限時促銷或特定時段的銷售策略。在這篇文章中,我將向您介紹如何通過程式碼為 WooCommerce 產品添加購買時間限制功能。

功能概述

我們的目標是在 WooCommerce 的產品編輯頁面中添加一個簡單的界面,讓商家能夠輕鬆設定產品的購買時間限制。這項功能包括:

  • 啟用購買時間限制的選項:僅在勾選選項時才會啟用時間限制,未勾選時則不限制購買。
  • 設置開始和結束時間:商家可以為每個產品設置購買的開始和結束時間,以確保產品僅在指定的時間範圍內可購買。
  • 自動檢查購買條件:當消費者嘗試購買產品時,系統會自動檢查當前時間是否在允許的購買時間範圍內,並據此決定產品是否可購買。
  • 客製化提示信息:當產品不在購買時間範圍內時,消費者會在產品頁面上看到提示信息,告知何時可以購買該產品。

功能實現

要實現這一功能,我們需要在 WooCommerce 中添加一些自訂代碼。以下是主要的步驟和實現代碼:

  1. 在產品編輯頁面中添加自訂字段:為每個商品增加啟用時間限制的選項以及購買時間的設定字段。
  2. 保存自訂字段值:將商家設置的購買時間限制保存到產品的元數據中,以便在購買過程中檢查。
  3. 購買時間限制檢查:根據設置的購買時間範圍,自動檢查產品的可購買性。
  4. 顯示提示信息:如果產品不可購買,顯示適當的提示信息告知消費者購買的正確時間。

完整代碼

function setup_product_time_restriction() {
    // 添加自訂元字段到產品編輯頁面
    function add_custom_time_fields() {
        global $woocommerce, $post;

        echo '<div class="options_group">';

        // 啟用時間限制選項
        woocommerce_wp_checkbox(
            array(
                'id'          => '_enable_time_restriction',
                'label'       => '啟用購買時間限制',
                'description' => '勾選此項以啟用產品購買時間限制'
            )
        );

        // 開始時間字段
        woocommerce_wp_text_input(
            array(
                'id'          => '_purchase_start_time',
                'label'       => '開始時間',
                'placeholder' => 'YYYY-MM-DD HH:MM:SS',
                'description' => '設置產品的購買開始時間,格式:YYYY-MM-DD HH:MM:SS',
                'type'        => 'datetime-local',
                'desc_tip'    => true
            )
        );

        // 結束時間字段
        woocommerce_wp_text_input(
            array(
                'id'          => '_purchase_end_time',
                'label'       => '結束時間',
                'placeholder' => 'YYYY-MM-DD HH:MM:SS',
                'description' => '設置產品的購買結束時間,格式:YYYY-MM-DD HH:MM:SS',
                'type'        => 'datetime-local',
                'desc_tip'    => true
            )
        );

        echo '</div>';
    }

    // 保存自訂字段值
    function save_custom_time_fields($post_id) {
        $enable_time_restriction = isset($_POST['_enable_time_restriction']) ? 'yes' : 'no';
        $purchase_start_time = sanitize_text_field($_POST['_purchase_start_time']);
        $purchase_end_time = sanitize_text_field($_POST['_purchase_end_time']);

        update_post_meta($post_id, '_enable_time_restriction', $enable_time_restriction);
        update_post_meta($post_id, '_purchase_start_time', $purchase_start_time);
        update_post_meta($post_id, '_purchase_end_time', $purchase_end_time);
    }

    // 根據自訂字段限制產品購買
    function restrict_purchase_time_based_on_custom_fields($purchasable, $product) {
        // 獲取自訂字段值
        $enable_time_restriction = get_post_meta($product->get_id(), '_enable_time_restriction', true);
        $purchase_start_time = get_post_meta($product->get_id(), '_purchase_start_time', true);
        $purchase_end_time = get_post_meta($product->get_id(), '_purchase_end_time', true);

        // 如果啟用了時間限制,則進行檢查
        if ($enable_time_restriction === 'yes') {
            $current_time = current_time('timestamp');

            // 檢查當前時間是否在允許的購買時間內
            if (!empty($purchase_start_time) && $current_time < strtotime($purchase_start_time)) {
                $purchasable = false;
            }

            if (!empty($purchase_end_time) && $current_time > strtotime($purchase_end_time)) {
                $purchasable = false;
            }
        }

        return $purchasable;
    }

    // 顯示自訂不可購買訊息
    function custom_purchase_notice_based_on_custom_fields() {
        global $product;

        $enable_time_restriction = get_post_meta($product->get_id(), '_enable_time_restriction', true);
        $purchase_start_time = get_post_meta($product->get_id(), '_purchase_start_time', true);
        $purchase_end_time = get_post_meta($product->get_id(), '_purchase_end_time', true);

        if ($enable_time_restriction === 'yes') {
            $current_time = current_time('timestamp');

            if ((!empty($purchase_start_time) && $current_time < strtotime($purchase_start_time)) || (!empty($purchase_end_time) && $current_time > strtotime($purchase_end_time))) {
                echo '<div class="woocommerce-info">該產品僅能於 ' . date('Y-m-d H:i:s', strtotime($purchase_start_time)) . ' 至 ' . date('Y-m-d H:i:s', strtotime($purchase_end_time)) . ' 購買。</div>';
            }
        }
    }

    // 將功能掛鉤到 WooCommerce
    add_action('woocommerce_product_options_general_product_data', 'add_custom_time_fields');
    add_action('woocommerce_process_product_meta', 'save_custom_time_fields');
    add_filter('woocommerce_is_purchasable', 'restrict_purchase_time_based_on_custom_fields', 10, 2);
    add_action('woocommerce_single_product_summary', 'custom_purchase_notice_based_on_custom_fields', 15);
}

// 初始化功能
setup_product_time_restriction();

這段程式碼提供了一個靈活的工具,可以讓商家在 WooCommerce 中更輕鬆地管理產品的購買時間。您可以根據需要進一步擴展這一功能,例如為不同類型的產品設置不同的時間限制或在不同的情境下顯示不同的提示信息。

透過這一功能,商家能夠更好地控制銷售策略,吸引消費者在特定時段內購買,從而提升銷售業績。

希望這篇文章能幫助您更好地理解和實現 WooCommerce 中的自訂購買時間限制功能。

瀏覽次數:66