在 WooCommerce 中自動控制商品購買時間

在電子商務中,靈活地管理商品的銷售時間是非常重要的。透過設置購買時間限制,您可以自動控制商品的上架和下架時間,提升銷售策略的有效性。本文將介紹如何在 WooCommerce 中自動實現商品的發布和草稿狀態切換,並確保管理員始終可以購買商品。

功能亮點

  1. 自動發布與草稿切換
    • 系統會根據您設定的時間,自動將商品從“發布”狀態變為“草稿”,或從“草稿”狀態恢復到“發布”狀態,無需手動操作。
  2. 靈活的時間管理
    • 您可以為每個商品設置開始和結束時間,使其僅在指定時間段內可購買。
  3. 管理員特權
    • 即使在商品被設置為草稿狀態,管理員仍然可以無時間限制地購買商品。

完整程式碼

要在 WooCommerce 中實現這些功能,您需要將以下程式碼添加到 WordPress 主題的 functions.php 文件中:

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 update_product_status_based_on_time($post_id) {
        $enable_time_restriction = get_post_meta($post_id, '_enable_time_restriction', true);
        $purchase_start_time = get_post_meta($post_id, '_purchase_start_time', true);
        $purchase_end_time = get_post_meta($post_id, '_purchase_end_time', true);

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

            // 如果不在購買時間內,將產品設為草稿
            if ((!empty($purchase_start_time) && $current_time < strtotime($purchase_start_time)) ||
                (!empty($purchase_end_time) && $current_time > strtotime($purchase_end_time))) {
                if ($product->get_status() !== 'draft') {
                    $product->set_status('draft');
                    $product->save();
                }
            } else {
                // 在購買時間內,確保產品狀態不是草稿
                if ($product->get_status() === 'draft') {
                    $product->set_status('publish');
                    $product->save();
                }
            }
        }
    }

    // 檢查並更新產品狀態
    function check_and_update_product_status() {
        $args = array(
            'post_type'   => 'product',
            'post_status' => array('publish', 'draft'),
            'numberposts' => -1
        );

        $products = get_posts($args);
        foreach ($products as $product) {
            update_product_status_based_on_time($product->ID);
        }
    }

    // 允許管理員在任何時間購買產品
    function admin_can_always_purchase($purchasable, $product) {
        if (current_user_can('administrator')) {
            return true;
        }
        return $purchasable;
    }

    // 將功能掛鉤到 WooCommerce
    add_action('woocommerce_product_options_general_product_data', 'add_custom_time_fields');
    add_action('woocommerce_process_product_meta', 'save_custom_time_fields');
    add_action('init', 'check_and_update_product_status');
    add_filter('woocommerce_is_purchasable', 'admin_can_always_purchase', 10, 2);
}

// 初始化功能
setup_product_time_restriction();

使用指南

自訂字段設置:
在產品編輯頁面,您可以選擇是否啟用購買時間限制,並設置具體的開始和結束時間。

自動狀態管理:
系統會自動根據設置的時間範圍,將產品從“發布”狀態轉換為“草稿”,或從“草稿”狀態恢復為“發布”。這樣可以確保產品在正確的時間可供購買,而無需手動管理。

管理員權限:
無論產品狀態如何,管理員都可以在後台自由操作,包括購買和編輯產品。

通過這種自動化的管理方式,您可以輕鬆控制 WooCommerce 商店中的產品銷售時間,無論是進行限時促銷,還是管理庫存,這一功能都能幫助您實現更高效的業務運營。同時,對管理員的特權保護確保了您在操作上的靈活性。
瀏覽次數:113