N Hossain
N Hossain

Reputation: 13

Single Woocommerce product custom variations add to cart buttons

I have customized WoCommerce single product page and added a cart button using simply this line

echo woocommerce_simple_add_to_cart();

but now I need two added two cart button for two types of variation. My all attribute and child is same. type_of_audio and child is mac & windows.

All I want to create two custom add to cart link. but I cannot configure the product id dynamically. bottom is the code. (Variable product id is not getting properly so it's not adding product to cart)

<?php if( $product->is_type( 'simple' ) ){                             
   echo woocommerce_simple_add_to_cart();                                
}                          
elseif( $product->is_type( 'variable' ) ){
   echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=mac">MAC BUTTON</a>');
   echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=windows">WINDOWS BUTTON</a>');
                                }
?>

Upvotes: 1

Views: 7077

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Try the following that will display an add to cart button for each variation with the correct button label:

<?php 
if ( $product->is_type( 'simple' ) ){
   echo woocommerce_simple_add_to_cart();
}
elseif ( $product->is_type( 'variable' ) ){
    // Loop through available variation data
    foreach ( $product->get_available_variations() as $variation_data ) {
        $url = '?add-to-cart='.$variation_data['variation_id']; // The dynamic variation ID (URL)

        // Loop through variation product attributes data
        foreach ( $variation_data['attributes'] as $attr_key => $term_slug ) {
            // The text button
            if ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'mac' ) 
            { // MAC
                $button_text = __("MAC BUTTON", "woocommerce");
            } elseif  ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'windows' ) 
            { // WINDOWS
                $button_text = __("WINDOWS BUTTON", "woocommerce");
            } else 
            { // OTHER (IF NEEDED)
                $button_text = __("Add to cart", "woocommerce"); 
            }

            $url .= '&'.$attr_key.'='.$term_slug; // Adding the product attribute name with the term value (to Url)
        }
        // Displaying the custom variations add to cart buttons
        if( isset($button_text))
            echo '<a href="'.$url.'" class="button alt">'.$button_text.'</a> ';
    }
}
?>

Tested and works.

Upvotes: 3

mujuonly
mujuonly

Reputation: 11861

 if ($product->is_type('simple')) {
    echo woocommerce_simple_add_to_cart();
} elseif ($product->is_type('variable')) {


    $children_ids = $product->get_children();

    foreach ($children_ids as $value) {
        $single_variation = new WC_Product_Variation($value);
        echo '<a href="/?add-to-cart=$variation_id=' . $value . '&attribute_pa_type_of_audio=mac">MAC BUTTON</a>';
    }
}

Upvotes: 0

Related Questions