如何在WooCommerce中用Max Button插件生成带动态产品ID的自定义按钮
Hey there, let's figure out how to make your Max Button dynamically pull the current product ID instead of using a static one—perfect for auto-applying coupons and linking straight to checkout. Here's a step-by-step breakdown:
Step 1: Prep Your Max Button
- Head to your Max Button dashboard and edit the button you’ve already set up.
- In the URL field, you can either leave it blank (since we’ll override it via shortcode) or use a placeholder like
{{product_id}}as a reminder—we’ll replace this dynamically later. - Save your button settings when you’re done.
Step 2: Dynamic Shortcode for PHP Templates (Single Product/Product Loops)
If you’re adding the button directly to a WooCommerce template file (like single-product.php or content-product.php), use WooCommerce’s built-in functions to grab the current product ID:
- Replace your static Max Button shortcode with this PHP snippet:
<?php // Get the ID of the current product $current_product_id = get_the_ID(); // Replace with your actual coupon code $coupon_code = "YOUR_ACTIVE_COUPON"; // Build the checkout URL with product ID and coupon $checkout_url = wc_get_checkout_url() . "?add-to-cart=" . $current_product_id . "&coupon=" . $coupon_code; // Output the Max Button with the dynamic URL echo do_shortcode('[maxbutton id="1" url="' . esc_url($checkout_url) . '"]'); ?>
- Don’t forget to swap
id="1"with your actual Max Button ID, andYOUR_ACTIVE_COUPONwith your coupon code.
Step 3: Custom Shortcode for Visual Editors (Product Descriptions/Pages)
If you need to add the button in the WordPress visual editor (like product descriptions or custom pages), create a reusable custom shortcode to handle the dynamic logic:
- Open your theme’s
functions.phpfile (or use a code snippet plugin if you don’t want to edit theme files directly). - Paste this code:
function dynamic_checkout_button_with_coupon() { // Only run this on WooCommerce product pages if (!is_product()) return; // Grab the current product's ID $product_id = get_the_ID(); // Update this to your coupon code $coupon_code = "YOUR_ACTIVE_COUPON"; // Generate a safe, formatted checkout URL $checkout_url = esc_url(add_query_arg( array( 'add-to-cart' => $product_id, 'coupon' => $coupon_code ), wc_get_checkout_url() )); // Return the Max Button shortcode with the dynamic URL return do_shortcode('[maxbutton id="1" url="' . $checkout_url . '"]'); } add_shortcode('dynamic_product_checkout_button', 'dynamic_checkout_button_with_coupon');
- Now, in any product’s description (or on a product page), just add the shortcode
[dynamic_product_checkout_button]—it will automatically pull the current product’s ID, apply the coupon, and use your Max Button styling.
Step 4: Test & Adjust
- Visit a few product pages to verify the button’s URL: right-click the button, select "Copy link address", and check that it includes the correct product ID and coupon code.
- If you need to update the coupon later, just edit the
$coupon_codevalue in the code snippet—all buttons will automatically use the new coupon. - Note: If you’re using a caching plugin, clear your cache after making changes to ensure the dynamic URLs load correctly.
内容的提问来源于stack exchange,提问作者DrMTR
相关产品推荐
相关产品推荐

