加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 修改WooCommerce Is_Purchasable

发布时间:2020-12-13 21:48:30 所属栏目:PHP教程 来源:网络整理
导读:我正在努力实现我自己的预购系统,我为每个产品设置了一个is_preorder自定义字段. 我试图修改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',$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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读