Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

php - Replace add to cart button based on weight in WooCommerce single products

I've searched for replacing add to cart button conditionally on woocommerce. I got a code and I've modified it as per my need. Its working perfectly on the shop page.

Here is My code:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
    global $product;
    $weight = $product->get_weight();
    preg_replace('/D/', '', $weight);
    if ( $product->has_weight() && ($weight > '8') ){
        $button = '<a href="#" class="button alt">' . __( "Add to Quote", "woocommerce" ) . '</a>';
    }
    return $button;
}

But I need a similar code for single product pages as well. How should I do that? Any help?

question from:https://stackoverflow.com/questions/65879488/replace-add-to-cart-button-based-on-weight-in-woocommerce-single-products

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First you can simplify your code a bit like:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){

    $weight = $product->get_weight();
    preg_replace('/D/', '', $weight);

    if ( $weight > 8 ){
        $button = '<a href="#" class="button alt">' . __( "Add to Quote", "woocommerce" ) . '</a>';
    }
    return $button;
}

Then to have something similar for single products pages use the following:

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
    global $product;

    $weight = $product->get_weight();
    preg_replace('/D/', '', $weight);

    if ( $weight > 8 ) {

        // For variable product types
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'add_to_quote_replacement_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'add_to_quote_replacement_button', 30 );
        }
    }
}

function add_to_quote_replacement_button(){
    echo '<a href="#" class="button alt">' . __( "Add to Quote", "woocommerce" ) . '</a>';
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...