WooCommerce 中特定单个产品页面的附加自定义按钮

EWG*_*EWG 5 html php wordpress product woocommerce

在 WooCommerce 中,需要在特定产品页面当前的“添加到购物车”按钮下方创建另一个重定向到“联系我们”表单的按钮(例如: http: //offers.elements.com.sg/product/ha-power-剂量-面部/)。

最终产品页面:

  • 将有2个不同的按钮供用户选择
  • 一个将是“添加到购物车”,导致 PayPal 页面,另一个将导致“联系我们”表单
  • 用户可以选择其中之一。

我正在 OceanWP 主题上使用。

Loi*_*tec 6

基于woocommerce 单一产品页面答案代码中具有固定数量的附加添加到购物车按钮,以下是执行此操作的方法:

\n
add_action( \'woocommerce_after_add_to_cart_button\', \'additional_single_product_button\', 20 );\nfunction additional_single_product_button() {\n    global $product;\n\n    // Define your targeted product IDs in the array below \n    $targeted_product_ids = array( 37, 53 );\n\n    if( in_array( $product->get_id(), $targeted_product_ids ) ) {\n        \n        $link = home_url(\'/contact-us/\'); // <== Here set button link\n        $name = esc_html ( "Contact Us", "woocommerce" ); // <== Here set button name \n        $class = \'button alt\';\n        $style = \'display: inline-block; margin-top: 12px;\';\n    \n        // Output\n        echo \'<br><a rel="no-follow" href="\'.$link.\'" class="\'.$class.\'" style="\'.$style.\'">\'.$name.\'</a>\';\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(活动主题)的 function.php 文件中。经过测试并有效。

\n

其他相关回答:

\n\n


小智 -1

您可以使用一些第三方插件,这些插件将提供在单个产品页面上添加按钮的功能。

或者您可以使用编码在单个产品文件中添加按钮。您可以在 WooCommerce 模板文件夹中的子主题中使用单个产品文件。

或者您也可以使用钩子在商店循环中添加按钮,如下所示:

add_action( 'woocommerce_after_shop_loop_item', 'new_add_to_cart_button' );
function new_add_to_cart_button() {
    // Your button code.
}
Run Code Online (Sandbox Code Playgroud)