Woocommerce - 删除添加到购物车按钮功能并更改按钮文本

时间:2017-09-21 15:20:37

标签: php wordpress woocommerce cart product

在WooCommerce中我想替换添加到购物车按钮并将其更改为"阅读更多" 但仅适用于某些产品类别

我尝试过使用相当多的不同代码段,但似乎没有什么能给我带来我想要的结果。

原因是我想将我店铺的某些部分用作显示商品及其价格的产品目录,但不允许用户购买。

有谁知道如何做到这一点?

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

以下是一些仅替换某些已定义产品类别的的功能

  1. 在商店和档案页面中:该按钮通过产品链接添加到购物车
  2. 在单个产品页面中:通过自定义按钮添加到购物车按钮和数量
  3. 你必须在那个函数中定义:

    1. 阵列中的产品类别(2次)
    2. "阅读更多"按钮产品链接(适用于简单的产品页面)
    3. 以下是代码:

      // Replacing the button add to cart by a link to the product in Shop and archives pages
      add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_conditionally_loop_add_to_cart_button', 10, 2 );
      function replacing_conditionally_loop_add_to_cart_button( $button, $product  ) {
          // Set HERE your product categories in the array
          $categories = array( 't-shirts', 'gloves' );
      
          if( has_term( $categories, 'product_cat', $product->get_id() ) ){
              $button_text = __( "View product", "woocommerce" );
              $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
          }
          return $button;
      }
      
      
      // Button replacement function for Single product pages (SET the link below)
      function custom_button_read_more( $product_link ){
          global $product;
      
          // Set HERE your product link
          $product_link = home_url( '/some-link-to-define/' );
      
          $button_text = __('Read more', 'woocommerce');
          echo '<a href="'.$product_link.'" class="button">'.$button_text.'</a>';
      }
      
      
      // replacing add to cart button and quantities by your custom button in Single product pages
      add_action( 'woocommerce_single_product_summary', 'replacing_conditionally_template_single_add_to_cart', 1, 0 );
      function replacing_conditionally_template_single_add_to_cart() {
          global $product;
      
          // Set HERE your product categories in the array
          $categories = array( 't-shirts', 'gloves' );
      
          if( has_term( $categories, 'product_cat', $product->get_id()) ){
      
              // For variable product types
              if( $product->is_type( 'variable' ) ){
                  // Removing add to cart button and quantities
                  remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
      
                  // The button replacement
                  add_action( 'woocommerce_single_variation', 'custom_button_read_more', 20 );
              }
              else // For all other product types
              {
                  // Removing add to cart button and quantities
                  remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
      
                  // The button replacement
                  add_action( 'woocommerce_single_product_summary', 'custom_button_read_more', 30 );
              }
          }
      }
      

      代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

      所有代码都在Woocommerce 3+上进行测试并且有效。

      类似的答案:Customize "Add to cart" button for a specific product category in WooCommerce

相关问题