php – 修改WooCommerce Is_Purchasable
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 修改WooCommerce Is_Purchasable,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1911字,纯文字阅读大概需要3分钟。
内容图文
我正在努力实现我自己的预购系统,我为每个产品设置了一个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 – 修改WooCommerce Is_Purchasable全部内容,希望文章能够帮你解决php – 修改WooCommerce Is_Purchasable所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。