获取 is_purchasable 钩子也适用于 Woocommerce 产品变体
Get is_purchasable hook working for Woocommerce product variations too
自 when/until 以来,我制作了 2 个自定义产品字段 - 可用性 -。因此,如果当前日期在这些设置的可用日期之间,则可以购买产品,否则不能购买。一切都完美无缺,但直到我 post 一个有变体的产品。然后就像产品变体忽略这些自定义可用性 fields/values 并且仍然允许将变体添加到购物车,即使当前日期不在设置的可用日期之间。
function hide_product_if_unavailable( $is_purchasable, $object ) {
$date_from = get_post_meta( $object->get_id(), '_availability_schedule_dates_from' );
$date_to = get_post_meta( $object->get_id(), '_availability_schedule_dates_to' );
$current_date = current_time('timestamp');
if ( strlen($date_from[0]) !== 0 ) {
if ( ( $current_date >= (int)$date_from[0] ) && ( $current_date <= (int)$date_to[0] ) ) {
return true;
} else {
return false;
}
} else {
# Let adding product to cart if Availability fields was not set at all
return true;
}
}
add_filter( 'woocommerce_is_purchasable', 'hide_product_if_unavailable', 10, 2 );
我试图在 woocommerce_is_purchasable
下方添加另一个过滤器:
add_filter( 'woocommerce_variation_is_purchasable', 'hide_product_if_unavailable', 10, 2 );
但变体仍然忽略可用性字段。
为所有产品类型(包括产品变体)尝试此重新访问的代码:
add_filter( 'woocommerce_is_purchasable', 'purchasable_product_date_range', 20, 2 );
function purchasable_product_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
要使产品变体正常工作,您需要获取父产品 ID,因为您的变体没有此日期范围自定义字段:
add_filter( 'woocommerce_variation_is_purchasable', 'purchasable_variation_date_range', 20, 2 );
function purchasable_variation_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
自 when/until 以来,我制作了 2 个自定义产品字段 - 可用性 -。因此,如果当前日期在这些设置的可用日期之间,则可以购买产品,否则不能购买。一切都完美无缺,但直到我 post 一个有变体的产品。然后就像产品变体忽略这些自定义可用性 fields/values 并且仍然允许将变体添加到购物车,即使当前日期不在设置的可用日期之间。
function hide_product_if_unavailable( $is_purchasable, $object ) {
$date_from = get_post_meta( $object->get_id(), '_availability_schedule_dates_from' );
$date_to = get_post_meta( $object->get_id(), '_availability_schedule_dates_to' );
$current_date = current_time('timestamp');
if ( strlen($date_from[0]) !== 0 ) {
if ( ( $current_date >= (int)$date_from[0] ) && ( $current_date <= (int)$date_to[0] ) ) {
return true;
} else {
return false;
}
} else {
# Let adding product to cart if Availability fields was not set at all
return true;
}
}
add_filter( 'woocommerce_is_purchasable', 'hide_product_if_unavailable', 10, 2 );
我试图在 woocommerce_is_purchasable
下方添加另一个过滤器:
add_filter( 'woocommerce_variation_is_purchasable', 'hide_product_if_unavailable', 10, 2 );
但变体仍然忽略可用性字段。
为所有产品类型(包括产品变体)尝试此重新访问的代码:
add_filter( 'woocommerce_is_purchasable', 'purchasable_product_date_range', 20, 2 );
function purchasable_product_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
要使产品变体正常工作,您需要获取父产品 ID,因为您的变体没有此日期范围自定义字段:
add_filter( 'woocommerce_variation_is_purchasable', 'purchasable_variation_date_range', 20, 2 );
function purchasable_variation_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。