Adds a cancel button for orders in specific statuses within a defined duration.
PHP
/** * Snippet Name: Add Cancel Button for Specific Order Statuses * Snippet Author: coding-bunny.com * Description: Adds a cancel button for orders in specific statuses within a defined duration. */// Hook into WooCommerce to validate order statuses for cancellationadd_filter('woocommerce_valid_order_statuses_for_cancel','cb_add_cancel_button',20,2);/** * Function to add specific order statuses for cancellation * * @paramarray $statuses Existing valid order statuses. * @paramWC_Order|string $order The order object or order ID. * @returnarray Modified array of valid order statuses. */functioncb_add_cancel_button($statuses,$order=''){// Define the custom order statuses where the cancel button should appear$custom_statuses=array('pending','processing','on-hold','failed');// Set the duration (3 hours) for which the cancel button will be available$duration=3*60*60;// 3 hours in seconds// Retrieve the order object if not already passedif(!is_object($order)&&isset($_GET['order_id'])){$order=wc_get_order( absint($_GET['order_id']));}// Get the order creation and modification timestamps$date_created_time=strtotime($order->get_date_created());$now=strtotime("now");// Check if the order is within the allowed cancellation durationif(($date_created_time+$duration)>=$now){return$custom_statuses;}else{return$statuses;}}
CSS
.woocommerce-button.button.cancel{background-color:#000000;/* Set the button background color here */}.woocommerce-button.button.cancel:hover{background-color:#474747;/* Set the button background color on hover here */}.woocommerce-button.button.cancel::after{content:'\00a0order';/* Adds " ordine" to the button text */}
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.
Google Analytics è un servizio di analisi web fornito da Google Ireland Limited (“Google”). Google utilizza i dati personali raccolti per tracciare ed esaminare l’uso di questo sito web, compilare report sulle sue attività e condividerli con gli altri servizi sviluppati da Google. Google può utilizzare i tuoi dati personali per contestualizzare e personalizzare gli annunci del proprio network pubblicitario. Questa integrazione di Google Analytics rende anonimo il tuo indirizzo IP. I dati inviati vengono collezionati per gli scopi di personalizzazione dell'esperienza e il tracciamento statistico. Trovi maggiori informazioni alla pagina "Ulteriori informazioni sulla modalità di trattamento delle informazioni personali da parte di Google".