In WooCommerce possiamo creare un link diverso per l'aggiunta al carrello se il prodotto è variabile.
In WooCommerce per aggiungere un prodotto variabile al carrello occorre che l'utente scelga una variazione. Quindi non è possibile aggiungere direttamente al carrello un prodotto variabile.
La soluzione è la seguente:
<?php
function my_atc_url( $id ) {
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
return $cart_url . '?add-to-cart=' . $id;
}
function my_cart_link( $id ) {
$prod = wc_get_product( $id );
if ( $prod->is_type( 'variable' ) ) {
return get_permalink( $id ); // Rimanda al singolo prodotto
} else {
return my_atc_url( $id ); // Aggiunge direttamente il prodotto al carrello
}
}