WooCommerce管理订单中的自定义操作按钮,用于发送电子邮件

时间:2019-05-07 22:49:26

标签: php ajax wordpress woocommerce orders

我在订单页面上添加了一个操作按钮,问题是没有向该函数传递任何数据。

add_filter( 'woocommerce_admin_order_actions', 'add_customer_second_payment_reminder_button', 100, 2 );
function add_customer_second_payment_reminder_button( $actions, $order ) {
    if ( $order->has_status(array( 'partially-paid' ) )) {
        $actions['email_reminder'] = array(
            'url'       => wp_nonce_url( admin_url('admin-ajax.php?action=customer_second_payment_reminder_button&order_id=' . $order->get_id() )),
            'name'      => __( 'Email Second Payment Reminder' , 'woocommerce-deposits' ),
            'action'    => 'email_reminder',
        );
    }
    return $actions;
}
add_action( 'wp_ajax_customer_second_payment_reminder_button', 'customer_second_payment_reminder_button' );
function customer_second_payment_reminder_button( $order_id ) {
  do_action( 'woocommerce_deposits_second_payment_reminder_email' , $order_id );
}

add_action( 'admin_head', 'add_customer_second_payment_reminder_button_css' );
function add_customer_second_payment_reminder_button_css() {
    echo '<style>.wc-action-button-'.'email_reminder'.'::after { font-family: woocommerce !important; content: "\e030" !important; }</style>';
}

所以当我使用按钮时总是显示0。 customer_second_payment_reminder_button函数未从URL接收?order_id参数。 我把一个var_dump($ order_id);在函数中并显示string(0)“” 0

如何将订单ID传递给函数?

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误和遗漏的东西:

  • 没有带有processing file: Right.Rmd |......... | 14% ordinary text without R code |................... | 29% label: setup (with options) List of 1 $ include: logi FALSE |............................ | 43% ordinary text without R code |..................................... | 57% label: cars |.............................................. | 71% ordinary text without R code |........................................................ | 86% label: pressure (with options) List of 1 $ echo: logi FALSE |.................................................................| 100% ordinary text without R code /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Right.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output Right.tex --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes' output file: Right.knit.md tlmgr search --file --global '/framed.sty' tlmgr: Remote repository is newer than local (2018 < 2019) Cross release updates are only supported with update-tlmgr-latest(.sh/.exe) --update Please see https://tug.org/texlive/upgrade.html for details. ! LaTeX Error: File `framed.sty' not found. ! Emergency stop. <read *> Error: Failed to compile Right.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See Right.log for more info. In addition: Warning message: In system2("tlmgr", args, ...) : running command ''tlmgr' search --file --global '/framed.sty'' had status 1 Execution halted wp_ajax_{action}动作钩子的函数参数。
  • 订单ID是通过URL发送的,因此您可以通过wp_ajax_nopriv_{action}变量来捕获
  • 您必须保护Ajax功能并在最后嵌入重定向。如果没有,您将获得白页。也永远不要忘记最后的$_GET ...

所以您的功能代码将是:

exit;

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试并可以正常工作。