Creare un e-commerce con WordPress e WooCommerce come abbiamo visto è la soluzione migliore sotto molti aspetti e punti di vista. Tuttavia il famoso e gratuito plugin per WordPress alle volte non ci offre, con la sua versione standard, la soluzione a tutte le nostre esigenze. Ecco che nasce quindi la necessità di personalizzare WooCommerce facendo ricorso a plugin (gratuiti o a pagamento) oppure a semplici snippet di codice che ci permettono di ampliarne funzionalità e potenzialità.
Cercherò di tenere sempre aggiornato questo articolo inserendo, di volta in volta, porzioni di codice “pronte all’uso” sempre nuove e di grande utilità per tutti (se magari cerchi qualcosa di particolare, fammelo sapere così magari posso postare qui la soluzione a ciò che cerchi aiutando così anche altre persone):
Rimuovere il campo selezione quantità
Se il tuo obiettivo è quello di rimuovere la selezione di quantità per i tuoi prodotti (per capirci, quelle freccette + e – che ti permettono di aumentare e diminuire la quantità da aggiungere al carrello), questi snippet di codice fanno proprio il caso tuo. Eccone due diversi per avere soluzioni differenti:
Rimuovere la selezione per tutti i prodotti
1 2 3 4 5 |
/* Remove Quantity Field */ function wc_remove_all_quantity_fields( $return, $product ) { return true; } add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 ); |
Rimuovere la selezione in base al tipo di prodotto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/* Remove Quantity Field Relationship type product */ add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 ); function wc_remove_all_quantity_fields( $return, $product ) { switch ( $product->product_type ) : case "variable": return true; break; case "grouped": return true; break; case "external": return true; break; default: // simple product type return true; break; endswitch; } |
Rimuovere il messaggio di prodotto aggiunto al carrello
Per rimuovere il messaggio “Il tuo prodotto è stato correttamente aggiunto al carrello” la cosa più semplice da fare è utilizzare i css inserendo questa regola nello style.css del tuo tema
1 2 3 |
.single-product .woocommerce-message { display: none !important; } |
Rimuovere la tab di informazioni aggiuntive
1 2 3 4 5 6 7 8 |
// removes Order Notes Title - Additional Information add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); //remove Order Notes Field add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' ); function remove_order_notes( $fields ) { unset($fields['order']['order_comments']); return $fields; } |
Rimuovere o personalizzare “Da..” nella pagina di categoria
Se nel tuo shop vendi prodotti variabili ti sarai accorto come nella pagina di categoria o nel tuo shop appaia la voce “Da.:” e accanto il prezzo indicando il range di prezzo o il prezzo più basso (spesso dipende anche dal template). Se vuoi eliminare questa dicitura o cambiare il testo, ecco due snippet di codice che fanno il caso tuo:
Eliminare la voce “Da”
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Nascondi Da: nella pagina di categoria */ function woocommerce_hide_from_label( $price ) { $price = str_replace( '<span class="from">From: </span>', "", $price ); return $price; } add_filter( 'woocommerce_variable_price_html', 'woocommerce_hide_from_label', 10 ); add_filter( 'woocommerce_grouped_price_html', 'woocommerce_hide_from_label', 10 ); add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_hide_from_label', 10 ); |
Cambiare il testo alla voce “Da”
1 2 |
// cambia il testo a Da.. $price = str_replace( '<span class="from">From: </span>', "New Label", $price ); |
Creare una pagina per visualizzare gli ordini recenti
Mi è capitato diverse volte che alcuni clienti mi chiedessero di realizzare una pagina a se stante dove gli utenti potessero avere un riepilogo di tutti gli ordini fatti (o prodotti scaricati). Nulla di più facile. Crea un nuovo template di pagina, chiamato ad esempio riepilogo-ordini.php e inserisci al suo interno questo codice.
Salva il file, caricalo all’interno del tuo child-theme, crea una nuova pagina sul tuo sito e richiama il template di pagina nominato “Riepilogo Ordini”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?php /* Template Name: Riepilogo Ordini */ ?> <?php $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array( 'numberposts' => $order_count, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => 'shop_order', 'post_status' => 'publish' ) ) ); if ( $customer_orders ) : ?> <table class="shop_table my_account_orders" width="100%"> <thead> <tr> <th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th> <th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th> <th class="order-status"><span class="nobr"><?php _e( 'Status', 'woocommerce' ); ?></span></th> <th class="order-total"><span class="nobr"><?php _e( 'Total', 'woocommerce' ); ?></span></th> <th class="order-actions"> </th> </tr> </thead> <tbody><?php foreach ( $customer_orders as $customer_order ) { $order = new WC_Order(); $order->populate( $customer_order ); $status = get_term_by( 'slug', $order->status, 'shop_order_status' ); $item_count = $order->get_item_count(); ?><tr class="order"> <td class="order-number"> <a href="<?php echo $order->get_view_order_url(); ?>"> <?php echo $order->get_order_number(); ?> </a> </td> <td class="order-date"> <time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time> </td> <td class="order-status" style="text-align:left; white-space:nowrap;"> <?php echo ucfirst( __( $status->name, 'woocommerce' ) ); ?> </td> <td class="order-total"> <?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?> </td> <td class="order-actions"> <?php $actions = array(); if ( in_array( $order->status, apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ) ) ) { $actions['pay'] = array( 'url' => $order->get_checkout_payment_url(), 'name' => __( 'Pay', 'woocommerce' ) ); } if ( in_array( $order->status, apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) { $actions['cancel'] = array( 'url' => $order->get_cancel_order_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ), 'name' => __( 'Cancel', 'woocommerce' ) ); } $actions['view'] = array( 'url' => $order->get_view_order_url(), 'name' => __( 'View', 'woocommerce' ) ); $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ); if ($actions) { foreach ( $actions as $key => $action ) { echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>'; } } ?> </td> </tr><?php } ?></tbody> </table> <?php endif; ?> |
I prodotti che ti consiglio