如果您想对结帐提交按钮进行一些更改,您将有两种方法:
1) 使用 woocommerce_order_button_html 过滤器挂钩中的自定义函数,这样:
add_filter( 'woocommerce_order_button_html', 'custom_order_button_html');
function custom_order_button_html( $button ) {
// The text of the button
$order_button_text = __('Place order', 'woocommerce');
// HERE your Javascript Event
$js_event = "fbq('track', 'AddPaymentInfo');";
// HERE you make changes (Replacing the code of the button):
$button = '<input type="submit" onClick="'.$js_event.'" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />';
return $button;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
2) 覆盖模板 checkout/payment.php,您将针对此代码(在第 50 行):
<?php echo apply_filters( 'woocommerce_order_button_html', '<input type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />' ); ?>
改成这样:
<?php
// Set HERE your javascript event
$js_event = $js_event = "fbq('track', 'AddPaymentInfo');";
echo apply_filters( 'woocommerce_order_button_html', '<input type="submit" onClick="'.$js_event.'" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />' ); ?>
相关文档:
所有代码都经过测试并且可以正常工作。这是两种解决方案的输出: