仅允许在特定日期购买特定 WooCommerce 产品

时间:2021-02-05 19:25:29

标签: php wordpress date woocommerce product

如何使 Sell specific Woocommerce items on a specific day and others on a time range 代码答案与 ID 为 625 的产品一起使用并将日期更改为星期一?

我在我的网站上尝试过,但代码适用于所有产品。有人可以帮忙弄清楚如何适应特定的 ID 和日期吗?

1 个答案:

答案 0 :(得分:1)

要仅在星期一启用特定产品的购买,请使用:

// Enable purchases for specific items on monday only
add_filter( 'woocommerce_variation_is_purchasable', 'restrict_specific_products_to_specific_days', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'restrict_specific_products_to_specific_days', 10, 2 );
function restrict_specific_products_to_specific_days( $purchasable, $product ) {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');

    $restricted_product_ids = array( 625 ); // Set in this array, your product Ids to be restricted

    // Target specific product ID(s)
    if( in_array( $product->get_id(), $restricted_product_ids ) ) {
        // Enable only on mondays
        $purchasable = date('w') == 1 ? true : false;
    }
    return $purchasable;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试和工作。