在WooCommerce中自定义特定产品类别的“添加到购物车”按钮

时间:2017-09-01 18:15:44

标签: php wordpress button woocommerce product

这是我的代码:

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +

function woo_custom_cart_button_text( $text ) {
    if( has_term( 'liners', 'product_cat' ) ){
        $text = __( ' ', 'your-plugin' );

          echo do_shortcode('<a href="#" class="popmake-923">Request а Quote</a>');
    }
    return $text;
}

我需要创建功能来替换“添加到购物车”按钮Url和Text仅针对一个特定的产品类别。

此按钮将触发带有联系表单的灯箱,此按钮的文本将为:申请报价

如何让它按预期工作?

以下是它在this link上实际运作的方式。

2 个答案:

答案 0 :(得分:3)

已更新:,适用于2种不同的产品类别(2个不同的按钮)

来自“内衬”产品类别的全球性产品解决方案:

  1. 如果您的某个产品(在“衬里”产品类别中)不是可变产品,则首先需要通过链接到产品的简单按钮替换商店和存档页面中的“添加到购物车”按钮。
  2. 在单个商品页面中,您需要移除“添加到购物车”按钮和数量字段,以便通过自定义按钮替换它。
  3. 以下是代码:

    {{1}}

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

    此代码经过测试,适用于所有产品类型(简单,可变......)。您将获得(示例)

    enter image description here

    enter image description here

答案 1 :(得分:0)

在这里,我的解决方案是将添加到购物车按钮替换为 Read More 按钮,并在特定类别中添加类别和ID。


   /* // Replace the Add to Cart Btn in Category Ammunition with View Product Btn  */

      add_filter('woocommerce_loop_add_to_cart_link','change_simple_shop_add_to_cart',10,2);
      function change_simple_shop_add_to_cart( $html, $product ){


           $category_ammunition = $product->get_categories();


          if (strstr($category_ammunition, 'Ammunition')) { // Add Your Category Here 'Ammuntion'

     $html = sprintf( '<a id="read-more-btn" rel="nofollow" href="%s" data-product_id="%s"  class="button vp-btn">%s</a>',
                      esc_url( get_the_permalink() ),
                      esc_attr( $product->get_id() ),
                      esc_html(  __( 'Read More', 'woocommerce' ) )
              );
              $category_ammunition = $product->get_categories();
          }

          return $html;
      }
相关问题