【问题标题】:Customize "Add to cart" button for a specific product category in WooCommerce在 WooCommerce 中为特定产品类别自定义“添加到购物车”按钮
【发布时间】:2018-02-10 20:28:28
【问题描述】:

这是我的代码:

    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 和文本。

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

我怎样才能让它按预期工作?

这是它在 this link 上的实际工作方式。

【问题讨论】:

    标签: php wordpress button woocommerce product


    【解决方案1】:

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

    为您的“衬垫”产品类别的产品提供全球和完整的解决方案:

    1. 如果您的产品之一(在“衬里”产品类别中)不是可变产品,您需要首先将商店和存档页面中的添加到购物车按钮替换为链接到该产品的简单按钮。李>
    2. 在单个产品页面中,您需要删除“添加到购物车”按钮和数量字段,以将其替换为您的自定义按钮。

    代码如下:

    // 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', 'conditionally_replacing_add_to_cart_button', 10, 2 );
    function conditionally_replacing_add_to_cart_button( $button, $product  ) {
    
        $categories = array('liners','custom-classics');
    
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        // For 'liners' product category
        if( has_term( $categories, 'product_cat', $product_id ) ){
            $button_text = __("View product", "woocommerce");
            $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
        }
        return $button;
    }
    
    // replacing add to cart button and quantities by your custom button in Single product pages
    add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
    function conditionally_replacing_template_single_add_to_cart() {
        global $product;
    
        $categories = array('liners','custom-classics');
    
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        function custom_button_replacement(){
            global $product;
    
            $categories = array('liners','custom-classics');
    
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
            if( has_term( $categories[0], 'product_cat', $product_id ) )
                $class_id = "923"; // liners
            elseif( has_term( $categories[1], 'product_cat', $product_id ) )
                $class_id = "925"; // custom-classics
            else $class_id = ""; // none
    
            // set below your custom text
            $button_text = __('Request а Quote', 'woocommerce');
    
            // Output your custom text
            echo '<a href="#" class="popmake-'.$class_id.' button">'.$button_text.'</a>';
        }
    
        // Only for 'liners' and 'custom-classics' product categories
        if( has_term( $categories, 'product_cat', $product_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_replacement', 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_replacement', 30 );
            }
        endif;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

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


    【讨论】:

    • 非常感谢!如果我需要为 2 个不同的类别做这件事怎么办?我的意思是我需要为一个类别调用一个弹出窗口,为第二个类别调用不同的弹出窗口?我只需要克隆下面的代码并将'liners'更改为'custom-samplers'?
    • 谢谢!但它会调用相同的弹出窗口。如何制作“回声'';” “cat2”的值不同?
    • array('liners','custom-classics')
    • echo ''; ....... echo '';
    • @PhilippNoris 已更新、测试并适用于 2 个类别
    【解决方案2】:

    这是我的解决方案,将 Add to Cart 按钮替换为 Read More 按钮,并使用特定类别中的 class 和 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;
          }
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2020-09-01
      • 2017-05-30
      • 1970-01-01
      • 2015-12-04
      • 2022-01-03
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多