【问题标题】:Additional custom button for specific single product page in WooCommerceWooCommerce 中特定单个产品页面的附加自定义按钮
【发布时间】:2020-11-16 15:33:25
【问题描述】:

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

最终产品页面:

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

我正在使用 OceanWP 主题。

【问题讨论】:

    标签: php html wordpress woocommerce product


    【解决方案1】:

    根据additional add to cart button with fixed quantity in woocommerce single product pages的回答码,方法如下:

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

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    其他相关答案:

    【讨论】:

      【解决方案2】:

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

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

      或者你也可以像这样使用钩子在商店循环中添加按钮:

      add_action( 'woocommerce_after_shop_loop_item', 'new_add_to_cart_button' );
      function new_add_to_cart_button() {
          // Your button code.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-30
        • 2021-01-29
        • 1970-01-01
        • 1970-01-01
        • 2018-02-18
        • 2017-11-25
        • 1970-01-01
        相关资源
        最近更新 更多