首页 > 解决方案 > 仅允许在特定日期购买特定 WooCommerce 产品

问题描述

如何在特定日期销售特定 Woocommerce 商品,并在时间范围代码答案上销售其他商品,以使用 ID 为 625 的产品并将日期更改为星期一?

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

标签: phpwordpressdatewoocommerceproduct

解决方案


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

// 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 文件中。测试和工作。


推荐阅读