草庐IT

php - 添加隐藏字段以结帐并通过订单处理

coder 2024-04-26 原文

我想在结帐过程中添加一个验证码,该验证码是只读(或不可见)且预填写的,固定在订单上。 客户需要此代码来验证订单。

我在 woocommerce_checkout_fields 过滤器的账单字段中添加了一个自定义数组:

//VID
$fields['billing']['billing_vid'] = array( 
'label'     => __('', 'woocommerce'), 
'placeholder'   => _x('', 'placeholder', 'woocommerce'), 
'required'  => false,
'type'      => 'text', 
'class'     => array('form-row-wide'), 
'clear'     => false,
'default' => wp_rand(10000,99999)   
);

这可行,但客户仍然可以在结帐过程中在该字段中写入内容。

有人能帮我解决吗?

谢谢

最佳答案

更新 2:增加了对 Woocommerce 3+ 的兼容性并删除了一些错误

正如您所期望的,这里可以是一个更完整的解决方案:

// Outputting the hidden field in checkout page
add_action( 'woocommerce_after_order_notes', 'add_custom_checkout_hidden_field' );
function add_custom_checkout_hidden_field( $checkout ) {

    // Generating the VID number
    $vid_number = wp_rand(10000,99999);

    // Output the hidden field
    echo '<div id="user_link_hidden_checkout_field">
            <input type="hidden" class="input-hidden" name="billing_vid" id="billing_vid" value="' . $vid_number . '">
    </div>';
}

// Saving the hidden field value in the order metadata
add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_hidden_field' );
function save_custom_checkout_hidden_field( $order_id ) {
    if ( ! empty( $_POST['billing_vid'] ) ) {
        update_post_meta( $order_id, '_billing_vid', sanitize_text_field( $_POST['billing_vid'] ) );
    }
}

// Displaying "Verification ID" in customer order
add_action( 'woocommerce_order_details_after_customer_details', 'display_verification_id_in_customer_order', 10 );
function display_verification_id_in_customer_order( $order ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    echo '<p class="verification-id"><strong>'.__('Verification ID', 'woocommerce') . ':</strong> ' . get_post_meta( $order_id, '_billing_vid', true ) .'</p>';
}

 // Display "Verification ID" on Admin order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1 );
function display_verification_id_in_admin_order_meta( $order ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    echo '<p><strong>'.__('Verification ID', 'woocommerce').':</strong> ' . get_post_meta( $order_id, '_billing_vid', true ) . '</p>';
}

// Displaying "Verification ID" on email notifications
add_action('woocommerce_email_customer_details','add_verification_id_to_emails_notifications', 15, 4 );
function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $output = '';
    $billing_vid = get_post_meta( $order_id, '_billing_vid', true );

    if ( !empty($billing_vid) )
        $output .= '<div><strong>' . __( "Verification ID:", "woocommerce" ) . '</strong> <span class="text">' . $billing_vid . '</span></div>';

    echo $output;
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码已经过测试并且可以工作。

关于php - 添加隐藏字段以结帐并通过订单处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894452/

有关php - 添加隐藏字段以结帐并通过订单处理的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  3. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  4. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  5. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  6. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  7. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  8. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  9. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  10. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

随机推荐