Custom ajax add to cart buttons

Viewed 91

I'm trying this code to work with ajax add to cart handler but I not able to. :( I have infos from this post Woocommerce - Product Page - How to create AJAX on "Add To Cart" button? and this Add ajax to add to cart buttons and prevent the page reload

I try to work with the ajax_add_to_cart add_to_cart_button class but I think i totally miss something here...

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 "bier" AND/OR "Cider"
        if ( has_term( array('bier', 'cider', 'getraenke'), 'product_cat', $product->get_id() ) ) {
        
        // Open parent layout wrapper
        echo '<div class="flex-container-pack-wrapper">';
            
        // Open child left layout wrapper
        echo '<div class="flex-container-pack-child left">';
            
            // 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 = 'single_add_to_cart_button-6 button alt';
                $style = 'display: inline-block; margin-top: 12px;';
                $button_text = __( "6er Pack", "woocommerce" );

                // Output add to cart button
                echo '<a rel="no-follow" href="'.$href.'" class="ajax_add_to_cart add_to_cart_button'.$class.'" style="'.$style.'">'.$button_text.'</a>';
                                
            }
            
            else {
                echo '<a class="single_sold_out_button button alt disabled ">6er Pack<p class="pack_soldout">(Ausverkauft)</p></a>';
            }
            
            // Close child left layout wrapper
            echo '</div>';
            
            // Open child right layout wrapper
            echo '<div class="flex-container-pack-child right">';

            // Only display when more than 12 are available
            if( $product->get_stock_quantity() >= 12 ) {

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

                // Output add to cart button
                echo '<a rel="no-follow" href="'.$href.'" class="ajax_add_to_cart add_to_cart_button'.$class.'" style="'.$style.'">'.$button_text.'</a>';
                
            }
            
            else {
                echo '<a class="single_sold_out_button button alt disabled ">12er Pack<p class="pack_soldout">(Ausverkauft)</p></a>';
            }

            // Close child right layout wrapper
            echo '</div>';
            
            // Close parent layout wrapper
            echo '</div>';
            
            // Open wrapper shipping info
            echo '<div class="container_pack_wrapper_shipping_ino">';
            
            // Tsting link to vendor
            global $product;
            $seller = get_post_field( 'post_author', $product->get_id());
            $author  = get_user_by( 'id', $seller );
            $vendor = dokan()->vendor->get( $seller );

            $store_info = dokan_get_store_info( $author->ID );

            if ( !empty( $store_info['store_name'] ) ) { 
                
            // Output Vendor link
            printf( '<i class="fas fa-angle-double-right pack"></i><a class="vendor_pack_info" href="%s">Mehr von %s</a>', $vendor->get_shop_url(),  $vendor->get_shop_name() );

            // Output shipping Info             
            // Do something

            }
      
            // Close wrapper shipping info
            echo '</div>';
        }
}
0 Answers
Related