且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-18 16:29:28

更新:针对2个不同的产品类别(2个不同的按钮)

Updated: for 2 different product categories (2 different buttons)

针对衬板"产品类别的产品的全球完整解决方案:

A global and complete solution for your products from 'liners' product category:

  1. 如果您的某个产品(衬里"产品类别中的产品)不是可变产品,则首先需要用链接到该产品的简单按钮来替换商店和存档页面中的购物车按钮.
  2. li>
  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', '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文件或任何插件文件中.

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

This code is tested and works for all product types (simple, variable…). You will get (example):