【问题标题】:Allow purchases for specific WooCommerce product only on a a specific day仅允许在特定日期购买特定 WooCommerce 产品
【发布时间】:2021-02-05 19:25:29
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress date woocommerce product


    【解决方案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 文件中。经过测试和工作。

    【讨论】:

    • 它不适用于特定产品。该代码正在应用于我在商店中拥有的所有产品。我应该更改代码中的某些内容吗?
    • @JordyUpdated,经过测试并且可以工作......如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    猜你喜欢
    • 2019-08-12
    • 2019-05-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多