You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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, and YOUR_ACTIVE_COUPON with 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:

  1. Open your theme’s functions.php file (or use a code snippet plugin if you don’t want to edit theme files directly).
  2. 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');
  1. 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_code value 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:29:55