我正在努力实现我自己的预购系统,我为每个产品设置了一个is_preorder自定义字段.
我试图修改WooCommerce的Is_Purchasable选项,这样,如果产品具有预订状态且已经超过预订截止日期,则无法购买.我尝试了很多方法,但似乎没有任何效果.
这是我做过的事(粗略的想法)
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable');
function preorder_is_purchasable() {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder');
if($is_preorder && "not yet passed deadline")
return true;
else
return false;
}
我不只是想禁用add_to_cart按钮,我也想禁用该功能(如果用户试图通过url中的硬编码添加产品,则应该提示错误).
我该怎么做呢?
================================================== =========================
这是我的最终代码:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
// if product is Pre-Order
if($is_preorder)
{
$today = date('Ymd');
// another field added using 'Advance Custom Fields' plugin
$preorder_deadline = get_field('preorder_deadline', $object->id);
if($today <= $preorder_deadline) // if not yet pass deadline
return true;
else
return false;
}
else
return $is_purchasable; // normal
解决方法:
不确定它是否解决了问题,因为必须在您自己的自定义设置上进行测试.但是你使用get_field是错误的:如果它没有在循环中使用,you should provide the post ID.
分析过滤器woocommerce_is_purchasable,我们看到it takes two parameters,一个布尔(is_purchasable)和一个对象(WC_Product).
试试这个:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
if($is_preorder && $is_purchasable)
return true;
else
return false;
}
标签:php,wordpress,woocommerce
来源: https://codeday.me/bug/20190728/1566994.html