plugins - How to remove the woocommerce_checkout_process action hook in woocommerce if particular project in cart

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I need to remove checkout page field validation if a particular product is in the cart.

Plugin code :

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['developer_name'] )
        wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}

I need to remove this action hook my_custom_checkout_field_process only if the customer added the product_id (19) to the cart. Else there's no need to remove the add_action.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I need to remove checkout page field validation if a particular product is in the cart.

Plugin code :

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['developer_name'] )
        wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}

I need to remove this action hook my_custom_checkout_field_process only if the customer added the product_id (19) to the cart. Else there's no need to remove the add_action.

Share Improve this question edited Apr 2, 2019 at 14:12 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 2, 2019 at 12:26 KanewilliamKanewilliam 611 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

try this below code in your function.php file or in your plugin

add_action("init", function () {
    // removing the woocommerce hook
    foreach( WC()->cart->get_cart() as $cart_item ){
            $product_id = $cart_item['product_id'];
            if($product_id!='19')
            {
        remove_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    }
}
});

WordPress compiles a list of actions quite early in the process, possibly before the product_id is known. So you probably (I don't know where WooCommerce executes this action) cannot execute this action conditionally.

However, what you can do is remove the action completely and define a new action that includes the condition. Also you must make sure this is done before the old action is executed. Like this:

add_action ('woocommerce_checkout_process', 'wpse333234_change_hook', 1); // early priority

function wpse333234_change_hook () {
  remove_action ('woocommerce_checkout_process', 'my_custom_checkout_field_process'); // remove old hooked function
  add_action ('woocommerce_checkout_process', 'wpse333234_new_hook', 10); // define new hooked function with later priority
  }

function wpse333234_new_hook () {
  // Check if set, if its not set add an error.
  if ( ! $_POST['developer_name'] && !$product_id==19)
    wc_add_notice( __( 'Please fill in your name.' ), 'error' );
    }

Beware that the latter function will give an error initially, because $product_id is not defined in the function. I don't know how this is defined in WooCommerce. You'll need a way to access this (global?) variable in some way.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632581a4637195.html

相关推荐

  • 流固耦合:基本概念、适用软件及 Abaqus 与 Powerflow 的协同仿真

    在工程和科学研究的诸多领域,流固耦合现象广泛存在且对系统性能有着关键影响。理解流固耦合的本质及其应用,对于优化设计、保障安全运行意义重大。同时,借助专业的流固耦合软件,能够更高效地对相关问题进行分析与模拟。接下来,让我们深入探究流固耦合的奥

    1小时前
    00
  • MySQL 8.4 配置SSL组复制(八个步骤)

    环境这里有三台MySQL主机,分别是192.168.3.71,72,73,主机名分别对应71.3_mgr1,72.3_mgr2,73.3_mgr3,操作系统均为Oracle Linux 8.10 X64,MySQL版本均为MySQL 8.4

    1小时前
    00
  • 利用小提琴图探索帕尔默企鹅数据

    利用小提琴图探索帕尔默企鹅数据代码语言:javascript代码运行次数:0运行复制import numpy as npimport matplotlib.pyplot as pltimport scipy.stats as stfro

    1小时前
    00
  • MySQL 8.4 配置复制

    参考文档:.4enreplication-configuration.html1.先在源数据库主机的myf添加这几项代码语言:javascript代码运行次数:0运行复制[mysqld]server-id = 2binlog_forma

    1小时前
    00
  • MySQL8使用物理文件恢复MyISAM表测试

    我们现场测试一个场景,drop一张MyISAM表后,单独对这表进行物理恢复首先我们看一下secure_file_priv文件目录的位置代码语言:javascript代码运行次数:0运行复制mysql> show global vari

    1小时前
    00
  • Oracle linux 8 二进制安装 MySQL 8.4企业版

    使用命令ldd --version ldd 检查,确定MySQL 8二进制包版本代码语言:javascript代码运行次数:0运行复制[root@mysql8_3 ~]# ldd --version ldd安装libaio代码语言:java

    1小时前
    00
  • Claude 不是只能聊天:我用这5个 MCP 工具,让它像程序员一样工作

    最近在折腾 Claude,原本只是拿来写点文案、回答技术问题。但渐渐地我有点不满足了——AI 不能只会说话,它得“动”起来。于是,我遇见了 MCP Server,彻底打开了新世界大门。

    1小时前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    49分钟前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    47分钟前
    00
  • 20 万 POC,直接拿来用,这不是测试,这是拒绝服务!!!

    之前看到很多人分享 github 上的一个项目,自动收录全网 nuclei 模板文件,目前已经 19 万了,如果直接拿来对目标进行漏洞探测,无疑会对目标造成巨大伤害,意味着可能要对目标发起十九万次请求以上,可以说是一次小型的 DDoS 攻击

    41分钟前
    00
  • 电脑开机会默认一件GHOST

    关于电脑开机会自己重装系统 前段时间电脑一开机就遇到会自己ghost的问题,而且一直再重复同样的操作,我点击restart的时候到开启页面又会自动ghost,而且此页面停留

    39分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    36分钟前
    00
  • 最简 Odoo 部署方法:Websoft9 企业应用托管平台

    传统方式部署 Odoo 通常依赖 Docker 技术,主要分为以下步骤:1 . 安装 Docker需在服务器上安装 Docker 引擎,涉及操作系统兼容性检查、依赖包安装、镜像源配置等操作。代码语言:bash复制 # 以 Ubu

    33分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    28分钟前
    00
  • Go 语言 Mock 实践

    Mock 是软件测试中的一项关键技术,尤其在单元测试领域,可谓是“顶梁柱”般的存在,几乎不可或缺。它通过模拟真实对象的行为,使我们能在不依赖外部系统的情况下,专注测试代码的核心逻辑。对于测试开发、自动化测试,乃至性能测试中的某些场景,合理使

    25分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    23分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

    21分钟前
    00
  • 什么是docker?它是如何工作的?

    想象一个场景,你要部署一个服务,然后它对环境有很多依赖,不同的操作系统又是不同的需求,而且还可能遇到有些源不能使用,又得一番折腾,折腾完上线后,假设要在新的环境再来一套,又得再来一遍。那么有没有什么办法可以解决呢?有办法,docker就是干

    17分钟前
    00
  • module 'torch.

    踩坑Ascend, 安装 pytorch 2.5.1 和 pytorch_npu 2.5.1, import torch 报错.执行 python -c "import torch;import torch_npu;"时

    12分钟前
    10
  • 在VMware虚拟机中安装Windows 7全攻略(避坑指南)

    ⚠️写在前面 最近发现很多开发者在调试老旧系统时都需要用到Windows 7环境(特别是银行、医疗等行业的遗留系统),但实体机安装既不现实也不安全。今天就手把手教你在虚拟机

    2分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信