Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - additional add to cart button with fixed quantity in woocommerce single product pages

With woocommerce enabled, we sell wine on our e-shop.

I would like an additional button so that customer can buy a case of wine (12 bottles) rather than having to select qty= 12.

I would like to stick the button after the 'add to cart' button on each single product page.

Until now I can't find exactly the way to do it.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It can be done easily with a custom hooked function displaying an additional button that will add 12 products on 1 click on single product pages for simple products only:

add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart', 20 );
function additional_simple_add_to_cart() {
    global $product;

    // Only for simple product type
    if( ! $product->is_type('simple') ) return;

    $href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=12';
    $class = 'ingle_add_to_cart_button-12 button alt';
    $style = 'display: inline-block; margin-top: 12px;';
    $button_text = __( "Add a case of 12", "woocommerce" );

    // Output
    echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
}

Code goes in function.php file of your active child theme (active theme).

Tested and works.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...