向 woocommerce 中的单个产品添加外部链接按钮

Adding an externally linked button to a single product in woocommerce

我已经尝试了其他相关问题中提供的编码,但它们不起作用 - 可能是因为我不擅长这个。 我只想在 'add to cart' 按钮旁边为一个产品添加一个按钮,该按钮将 link 连接到外部网站。这必须出现在商店页面和产品描述页面上,因为我不希望国际买家 'add to cart' 不知道他们有一个可以节省运费的国际选项。 请注意 - 我不是开发人员,但可以在代码中导航......请帮助这个迷失的灵魂

下面的代码可以帮助您添加外部 link 按钮。 复制代码并添加到您的主题 function.php 文件中。 在我的示例中,它仅适用于产品 ID 41 和 53,您可以替换为要在其中显示此按钮的产品 ID。

add_action('woocommerce_after_add_to_cart_button', 'additional_single_product_button', 20); // add button in single product page
add_action('woocommerce_after_shop_loop_item', 'additional_single_product_button', 20); // add button in shop page
function additional_single_product_button()
{
   global $product;

   // Define your targeted product IDs in the array below 
   $targeted_product_ids = array(41, 53);

   if (in_array($product->get_id(), $targeted_product_ids)) {
       echo '<br><a  href="https://google.com" class="button">i am external button</a>'; // your button
    }
}

希望它能帮助您解决问题。

谢谢。