首页 文章

如果Woocommerce产品价格为零则显示联系人按钮其他显示添加到购物车按钮

提问于
浏览
0

我正在使用Divi主题的Woocommerce网站 . 我在Divi主题的商店模块的帮助下展示特色产品 . 目前Divi商店模块不显示添加到购物车按钮 . 所以我添加了一个实现相同的钩子 .

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

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

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

Hide "Add to cart" button when the product price is zero When price is 0 change add to cart button to "request quote"

我需要实现这样的功能:https://imgur.com/a/GaQAgUg

1 回答

  • 1

    把它放在你的主题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');
    

相关问题