【发布时间】:2019-02-25 13:12:51
【问题描述】:
通过 Woocommerce,我想仅在周日销售某些已定义的商品。如果有人试图在周日以外购买,应该会出错。商店中剩余的物品可以随时购买。
这可能吗?任何曲目都非常感谢。
示例 - 我有一个名为“Sunday Tapas”的产品,产品代码为“2795”。我希望这个产品只在周日有售。商店里的其他东西应该每天都可以正常使用。
【问题讨论】:
标签: php wordpress date woocommerce product
通过 Woocommerce,我想仅在周日销售某些已定义的商品。如果有人试图在周日以外购买,应该会出错。商店中剩余的物品可以随时购买。
这可能吗?任何曲目都非常感谢。
示例 - 我有一个名为“Sunday Tapas”的产品,产品代码为“2795”。我希望这个产品只在周日有售。商店里的其他东西应该每天都可以正常使用。
【问题讨论】:
标签: php wordpress date woocommerce product
基于“Enable Woocommerce shop purchases only during a daily time range” 答案代码,改为处理“星期几” (所以“星期日” 给你)。
以下内容将允许购买特定商品(产品 ID 2795),仅限周日。
如果商品在购物车中但无法再购买,它会自动删除(在购物车或结账时)并显示错误消息。
周日以外,产品页面会显示错误信息,提示该产品只能在周日购买。
你必须在第一个函数中set your time zone,在第二个函数中你的特定产品。
代码:
// Utility conditional function that check if day is sunday (returns boolean)
function is_sunday() {
// Set Your shop time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/London');
// If the current day is "sunday" return true (else retun false)
return ( date('w') == 0 ) ? true : false;
}
// Utility function (setting your product IDS in the array)
function sunday_products() {
// HERE your product IDs in the array (need to be coma separated)
return array( 37 );
}
// Enable purchases for specific items on sundays only
add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
function enable_specific_products_on_sundays( $purchasable, $product ) {
// Enable purchases for specific defined item only on sunday
if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) )
$purchasable = false;
return $purchasable;
}
// Add a notice in specific products outside sundays
add_action( 'woocommerce_before_single_product', 'filter_before_single_product' );
function filter_before_single_product() {
global $product;
if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) {
wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' );
}
}
// IN CASE OF (but not needed): Cart and checkout validation + error message
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
if ( ! is_sunday() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// Check cart items
if( in_array( $cart_item['data']->get_id(), sunday_products() ) ){
wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
break;
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
【讨论】:
此代码 sn-p 将阻止周日产品添加到购物车。
add_filter( 'woocommerce_variation_is_purchasable', 'restrict_products_on_sunday', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'restrict_products_on_sunday', 10, 2 );
function restrict_products_on_sunday( $is_purchasable, $product ){
$sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product waht to sell on sundays only
date_default_timezone_set("Asia/Bangkok"); // give here the time zone of your country
$sku = $product->get_sku();
$today = date('w');
if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
return true;
}
return false;
}
由于您的要求是从结帐开始,您可以使用以下代码 sn-p。如果购物车商品包含无效产品,它将在结帐时显示一些消息。
add_action( 'woocommerce_check_cart_items', 'prevent_chckout_for_sunday_products' );
function prevent_chckout_for_sunday_products(){
$sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product which will sell on Sundays only
global $woocmmerce;
date_default_timezone_set("Asia/Bangkok");
$today = date('w');
$items = WC()->cart->get_cart();
foreach( $items as $key => $item ){
$sku = $item['data']->get_sku();
if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
wc_add_notice( sprintf( 'Look like your cart contains product which is not eligible to checkout today, Consider removing those.' ), 'error' );
break;
}
}
}
【讨论】: