Custom Date-Specific Product Badges

// Add custom fields to WooCommerce backend under General tab add_action( ‘woocommerce_product_options_general_product_data’, ‘wpsh_add_text_field_below_sale_price’ ); function wpsh_add_text_field_below_sale_price() { global $product_object; // Original custom field woocommerce_wp_text_input( array( ‘id’ => ‘_badge’, ’label’ => __( ‘Badge text’, ‘woocommerce’ ), ‘description’ => __( ‘Enter your pduct badge text here’, ‘woocommerce’ ), ‘desc_tip’ => ’true’, ’type’ => ’text’, ‘value’ => $product_object->get_meta( ‘_badge’ ), ) ); // Start date field woocommerce_wp_text_input( array( ‘id’ => ‘_start_date’, ’label’ => __( ‘Dislay start date’, ‘woocommerce’ ), ...

June 4, 2024 · 2 min · Sayfullah Sayeb

add a country code prefix in billing phone field

// Add country calling code prefix in woocommerce billing phone // Register WordPress hooks and callbacks for the WooCommerce checkout. add\_action( 'wp\_footer', 'wpsh\_add\_callback\_script' ); add\_action( 'wp\_ajax\_nopriv\_append\_country\_prefix\_in\_billing\_phone', 'wpsh\_add\_phone\_prefix' ); add\_action( 'wp\_ajax\_append\_country\_prefix\_in\_billing\_phone', 'wpsh\_add\_phone\_prefix' ); add\_action( 'woocommerce\_checkout\_process', 'wpsh\_validate\_phone' ); /\* Outputs the JavaScript required for updating the billing phone with the country prefix. \*/ function wpsh\_add\_callback\_script() { // Securely pass data to JavaScript. $ajax\_url = admin\_url('admin-ajax.php'); ?> <script type="text/javascript"> (function($) { $(document.body).on('updated\_checkout', function() { var country\_code = $('#billing\_country').val(); var ajax\_data = { action: 'append\_country\_prefix\_in\_billing\_phone', country\_code: country\_code }; $.post('<?php echo esc\_js($ajax\_url); ?>', ajax\_data, function(response) { $('#billing\_phone').val(response); }); }); })(jQuery); </script> <?php} /\* Handles AJAX request to append the country calling code to the billing phone field. \*/ function wpsh\_add\_phone\_prefix() { $country\_code = isset($\_POST\['country\_code'\]) ? sanitize\_text\_field($\_POST\['country\_code'\]) : ''; $calling\_code = ''; if ($country\_code) { $calling\_codes = WC()->countries->get\_country\_calling\_code($country\_code); $calling\_code = is\_array($calling\_codes) ? $calling\_codes\[0\] : $calling\_codes; } echo $calling\_code; wp\_die(); } /\* Validates the phone number length during the WooCommerce checkout process. \*/ function wpsh\_validate\_phone() { if (isset($\_POST\['billing\_phone'\]) && strlen(preg\_replace('/\[^0-9\]/', '', $\_POST\['billing\_phone'\])) < 6) { wc\_add\_notice(\_\_('Billing Phone must be at least 6 digits long.', 'woocommerce'), 'error'); }}

June 4, 2024 · 1 min · Sayfullah Sayeb

Plugin Info

 Admin and Site Enhancements (ASE) GTranslate WP-Optimize - Clean, Compress, Cache White Label CMS Code Snippets UpdraftPlus: WordPress Backup & Migration Plugin optimize wpvivid for security wp hide wordfence for Analytics Independent Analytics

January 31, 2024 · 1 min · Sayfullah Sayeb