Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
@braddalton
braddalton / functions.php
Last active November 13, 2023 15:05
Add Product Expiration Date To Order Emails Full Tutorial https://wpsites.net/product/woocommerce/add-expiration-date-to-order-emails/
add_action('woocommerce_email_order_details', 'add_custom_fields_to_order_email', 10, 4);
function add_custom_fields_to_order_email($order, $sent_to_admin, $plain_text, $email) {
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$expiration_date = get_post_meta($product_id, '_expiration_date', true);
if ( ! empty( $expiration_date ) ) {
@braddalton
braddalton / functions.php
Created November 12, 2023 13:00
Exclude products from search results in WordPress
function exclude_products_from_search($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_search) {
$query->set('post_type', array('post', 'page')); // Exclude products from search results
}
}
@braddalton
braddalton / functions.php
Last active November 12, 2023 06:27
Shortcode to add custom fields to the product description https://wpsites.net/?p=114180
// Add custom field to product general settings metabox
function custom_product_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom field
woocommerce_wp_textarea_input(
array(
'id' => '_custom_product_description',
@braddalton
braddalton / functions.php
Last active November 10, 2023 05:19
Coupon code in WooCommerce that provides a % discount and includes a free product https://wpsites.net/?p=114158
add_action('woocommerce_before_cart', 'apply_discount_and_add_free_product');
function apply_discount_and_add_free_product() {
global $woocommerce;
if (WC()->cart->has_discount('30OFF')) {
$free_product_id = 14646;
$quantity = 1;
WC()->cart->add_to_cart($free_product_id, $quantity);
@braddalton
braddalton / style.css
Created November 9, 2023 12:02
Media Query for Filter Custom Post Type Archive Using Isotope https://wpsites.net/?p=114135
/* Media query for mobile screens */
@media only screen and (max-width: 767px) {
/* Adjust styles for filter links on mobile screens */
.isotope-filter a {
display: block; /* Make links display as block elements */
margin: 0 0 10px; /* Add margin below each link */
width: 100%; /* Make links take up full width */
}
}
register_taxonomy( 'portfolio-type', 'portfolio',
array(
'labels' => array(
'name' => _x( 'Portfolio Types', 'taxonomy general name' , 'genesis-portfolio-pro' ),
'singular_name' => _x( 'Portfolio Type' , 'taxonomy singular name', 'genesis-portfolio-pro' ),
'search_items' => __( 'Search Portfolio Types' , 'genesis-portfolio-pro' ),
'popular_items' => __( 'Popular Portfolio Types' , 'genesis-portfolio-pro' ),
'all_items' => __( 'All Types' , 'genesis-portfolio-pro' ),
'edit_item' => __( 'Edit Portfolio Type' , 'genesis-portfolio-pro' ),
'update_item' => __( 'Update Portfolio Type' , 'genesis-portfolio-pro' ),
@braddalton
braddalton / Daily Random Free Product.php
Created November 9, 2023 06:08
Daily Random Free Product for WooCommerce Changes The Price to Zero for 1 Day Only. Full Code https://wpsites.net/?p=114123
add_action('init', 'assign_random_free_product');
function assign_random_free_product() {
$today = date('Y-m-d');
$assigned_product_date = get_option('random_free_product_date');
if ( $today != $assigned_product_date ) {
$random_product_id = get_random_free_product();
@braddalton
braddalton / wp_schedule_event.php
Created November 8, 2023 18:19
Random Free Product For Each Day in WooCommerce https://wpsites.net/?p=114123
// Schedule the function to run hourly
add_action('init', 'schedule_update_random_free_product');
// Function to select and update a random free product
function update_random_free_product() {
$random_product_id = get_random_free_product();
if ($random_product_id) {
update_option('random_free_product_id', $random_product_id);
update_option('random_free_product_date', current_time('Y-m-d'));
}
}
add_filter( 'woocommerce_available_variation', 'unset_variation_attribute', 10, 3 );
function unset_variation_attribute( $data, $product, $variation ) {
// Define the attribute name you want to hide (e.g., "Size")
$attribute_to_hide = 'Size';
// Unset the attribute if it exists in the variation data
if (isset($data['attributes']['attribute_' . sanitize_title($attribute_to_hide)])) {
unset($data['attributes']['attribute_' . sanitize_title($attribute_to_hide)]);
}
@braddalton
braddalton / role.php
Created October 29, 2023 19:17
Add Checkbox To WooCommerce Checkout Page https://wpsites.net/?post_type=product&p=113989
&& in_array( 'administrator', (array) $user->roles )