skip to Main Content

I’m trying to crate a snipped to display a second "add to cart" button on all product single pages from a specific categorie. This button should add 6 of the current product to the cart.

UPDATE:
With the info from @kmclaws and here additional add to cart button with fixed quantity in woocommerce single product pages I was able to get this to work. The only problem now is, that the IF in order to only display on "biere" product category does not work.

Problem:
Some Products are from "biere" or "cider" only and some are from "biere" AND "cider" and have some other categories. Means, sometime there are more than one sometime only one category is defined. We want to display this always if "biere" OR/AND "cider" is one of the product category.

How can I code it that way, that if "biere" OR "cider" is the categorie to display that all?

This is my current code:

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;
                
        // Only for products from category "biere" AND/OR "Cider"
        if ( has_term( 'biere', 'cider', 'product_cat' ) ) {
    
            // Output Info text for "biere" or "cider" products
            echo '<p class="rtrn">Infotext block</p>';
                
            // Only display when more than 6 are available
            if( $product->get_stock_quantity() >= 6 ) {

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

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

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    This is my solutions:

    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;
                    
            // Only for products from category "biere" AND/OR "Cider"
            if ( has_term( array('biere', 'cider'), 'product_cat', $product->get_id() ) ) {
    
        
                // Output Info text for "biere" or "cider" products
                echo '<p class="rtrn">Infotext block</p>';
                    
                // Only display when more than 6 are available
                if( $product->get_stock_quantity() >= 6 ) {
    
                    $href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=6';
                    $class = 'ingle_add_to_cart_button-12 button alt';
                    $style = 'display: inline-block; margin-top: 12px;';
                    $button_text = __( "6 Pack bestellen", "woocommerce" );
    
                    // Output add to cart button
                    echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
                }
            }
    }
    

  2. It looks like you are not grabbing the ID of the current product in PHP correctly. Try this:

    add_action( 'woocommerce_after_add_to_cart_quantity', 'add_quantity_6' );
    
    function add_quantity_6() {
        global $product;
        $id = $product->get_id();
        if ( is_product_category( 'jeans' ) ) {
            echo '<a href="https://example.com/?add-to-cart' . $id . '&quantity=6" class="button">Add 6 to Cart</a>';
        }    
    }
    

    You need to replace example.com with either a static or relative path to your site

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search