如何在主题functions.php中对接WooCommerce插件及移除Amazon Pay动作
Hey there! Let's break down your two WooCommerce questions with practical, actionable solutions:
WooCommerce is built to be super extensible, and your theme's functions.php is the perfect place to hook into its system. Here's how to do it right:
Wait for WooCommerce to load first
Never run WooCommerce-related code before the plugin is initialized—this avoids fatal errors. Use thewoocommerce_loadedhook to wrap your code:add_action( 'woocommerce_loaded', 'my_theme_woocommerce_setup' ); function my_theme_woocommerce_setup() { // All your WooCommerce integration code goes here }Leverage WooCommerce's hooks and filters
WooCommerce provides hundreds of actions (for adding content/behavior) and filters (for modifying data). For example:- Add a custom checkout field:
add_action( 'woocommerce_after_order_notes', 'my_theme_add_custom_checkout_field' ); function my_theme_add_custom_checkout_field( $checkout ) { echo '<div id="custom_checkout_field"><h3>' . __('Special Instructions') . '</h3>'; woocommerce_form_field( 'special_instructions', array( 'type' => 'textarea', 'class' => array('form-row-wide'), 'label' => __('Tell us anything we need to know'), 'placeholder' => __('e.g., Leave package at back door'), ), $checkout->get_value( 'special_instructions' )); echo '</div>'; } - Modify product prices dynamically:
add_filter( 'woocommerce_get_price_html', 'my_theme_modify_product_price', 10, 2 ); function my_theme_modify_product_price( $price_html, $product ) { if ( is_user_logged_in() ) { $discounted_price = $product->get_price() * 0.9; // 10% discount for logged-in users return wc_price( $discounted_price ); } return $price_html; }
- Add a custom checkout field:
Directly access WooCommerce classes (when needed)
For deeper integrations (like working with orders or products), use WooCommerce's built-in classes. Just make sure they're loaded first:add_action( 'woocommerce_thankyou', 'my_theme_send_custom_order_notification' ); function my_theme_send_custom_order_notification( $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { $customer_email = $order->get_billing_email(); // Send a custom email or update external systems here wp_mail( $customer_email, __('Thanks for your order!'), __('We appreciate your business.') ); } }
I’ve been in your shoes—removing plugin actions can be tricky if you don’t match the exact parameters. Here’s how to fix it:
First, diagnose the plugin's code
From what you said, the Amazon Pay plugin adds the button via an action in /woocommerce-gateway-amazon-payments-advanced.php. Typically, it looks something like this (using a class method):
add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'render_amazon_pay_button' ), 10 );
The key here is to match three things exactly when removing: the hook name, the callback (including the class instance), and the priority.
The correct removal & repositioning code
Add this to your theme's functions.php:
add_action( 'plugins_loaded', 'my_theme_move_amazon_pay_button', 11 ); function my_theme_move_amazon_pay_button() { // Get the Amazon Pay gateway instance from WooCommerce $amazon_gateway = WC()->payment_gateways()->get_gateway( 'amazon_payments_advanced' ); if ( $amazon_gateway ) { // Remove the original action—match hook, callback, and priority exactly remove_action( 'woocommerce_checkout_before_customer_details', array( $amazon_gateway, 'render_amazon_pay_button' ), 10 ); // Add it to your desired location—adjust the hook and priority as needed // Example: Move to after the order review section add_action( 'woocommerce_checkout_after_order_review', array( $amazon_gateway, 'render_amazon_pay_button' ), 10 ); } }
Common mistakes to avoid
- Wrong priority: If the plugin uses priority
10, your removal code needs to run after that—hence using11onplugins_loaded. - Mismatched callback: If the plugin uses a class method, you can’t just use the function name—you need to pass the class instance (which we get via
WC()->payment_gateways()->get_gateway()). - Running too early: If you try to remove the action before the plugin adds it, it won’t work.
plugins_loadedensures all plugins are loaded first.
内容的提问来源于stack exchange,提问作者logrexton

