php - 仅允许在特定日期购买特定 WooCommerce 产品
问题描述
如何在特定日期销售特定 Woocommerce 商品,并在时间范围代码答案上销售其他商品,以使用 ID 为 625 的产品并将日期更改为星期一?
我在我的网站上试过,但代码适用于所有产品。有人可以帮助弄清楚如何适应特定的 ID 和日期吗?
解决方案
要仅在星期一启用特定产品的购买,请使用:
// 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 文件中。测试和工作。
推荐阅读
- c++ - 如何在我的 Xamarin.Android 项目中使用我的 .so 库?
- excel - Excel函数用特定数据填充网格,数据点可以使用多少次
- python - 从 tkinter 按钮运行 IntVar.get() 不返回值
- visual-studio - 无法下载应用程序。应用程序缺少所需的文件
- json - mvc控制器json返回
- python - 如何在两个 csv 文件之间显示相似的值并在 python 中创建一个具有所需输出的新 csv 文件?
- netlogo - NetLogo:在 NetLogo 中查找“random-gamma”计算错误及其范围的详细信息
- firebase - Cloud Functions 位置 - 伦敦
- react-native - 单击图像时导航不起作用
- c# - 在 asp.net core 2.2 中使用 autofac