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

前端 未结 2 1154
执念已碎
执念已碎 2021-01-16 20:13

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 s

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 20:24

    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 '
    '.$button_text.''; }

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

    Tested and works.

提交回复
热议问题