woocommerce php,php – 修改WooCommerce Is_Purchasable

我正在努力实现我自己的预购系统,我为每个产品设置了一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值