当前位置: 首页 > 知识库问答 >
问题:

显示联系按钮,如果WooCommerce产品价格为零,否则显示添加到购物车按钮

隆功
2023-03-14

我正在使用一个以Divi为主题的Woocommerce网站。我在Divi主题的shop模块的帮助下展示特色产品。目前,Divi shop模块未显示“添加到购物车”按钮。所以我添加了一个钩子来实现同样的功能。

链接:https://intercom.help/elegantthemes/faq-s-and-troubleshooting/how-to-add-add-to-cart-button-in-divi-shop-pages

但问题是addtocart按钮是ajax类型的。我需要按钮被重定向到相应的产品页面。另外,如果woocommerce产品价格为零,请删除“添加到购物车”并显示“联系我们”按钮。

我尝试了以下解决方案,但无法满足我的要求。

当产品价格为零时隐藏“添加到购物车”按钮当价格为0时将“添加到购物车”按钮更改为“请求报价”

我需要实现如下功能:https://imgur.com/a/GaQAgUg

共有1个答案

商和雅
2023-03-14

将其放在theme functions.php文件中。谢谢

function replace_add_to_cart() {
   global $product;

   if( $product->get_price() == 0 ) {
     $link = 'YOUR CONTACT PAGE URL';
     $button_text = 'Contact Us';
   } else {
     $link = $product->get_permalink();
     $button_text = 'Buy Now';
   }

   echo do_shortcode('[button link="' . esc_attr($link) . '"]'.$button_text.'[/button]');
}
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
 类似资料: