Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
function conditionally_hide_local_pickup( $rates, $package ) {
$minimum_amount = 50; // Set your minimum order amount here
// Get the cart total
$cart_total = WC()->cart->get_displayed_subtotal();
// Check if cart total is less than the minimum amount
if ( $cart_total < $minimum_amount ) {
// Loop through the available shipping methods
foreach ( $rates as $rate_id => $rate ) {
// Add €40 deposit to the cart based on product ID
add_action('woocommerce_cart_calculate_fees', 'add_deposit_fee');
function add_deposit_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
$deposit_fee = 40; // Deposit amount
$product_ids = array(391); // Replace with your product IDs
$add_deposit = false;
add_action('init', 'custom_add_customer_groups');
function custom_add_customer_groups() {
add_role('group1', __('Group 1'), array());
add_role('group2', __('Group 2'), array());
add_role('group3', __('Group 3'), array());
}
@braddalton
braddalton / Products by Brand in WooCommerce.php
Last active May 24, 2024 03:32
Products by Brand in WooCommerce. Full Tutorial https://wpsites.net/?p=116042
add_action( 'woocommerce_after_single_product_summary', 'display_on_sale_products_by_brand', 15 );
function display_on_sale_products_by_brand() {
global $post;
// Get the brands of the current product
$brands = wp_get_post_terms( $post->ID, 'brand' );
if ( ! empty( $brands ) && ! is_wp_error( $brands ) ) {
foreach ( $brands as $brand ) {
@braddalton
braddalton / wc-product-functions.php
Last active May 24, 2024 07:47
Use woocommerce_placeholder_img_src filter hook. This is the original function containing the filter. Full Tutorial https://wpsites.net/?p=115999
function wc_placeholder_img_src( $size = 'woocommerce_thumbnail' ) {
$src = WC()->plugin_url() . '/assets/images/placeholder.png';
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( ! empty( $placeholder_image ) ) {
if ( is_numeric( $placeholder_image ) ) {
$image = wp_get_attachment_image_src( $placeholder_image, $size );
if ( ! empty( $image[0] ) ) {
$src = $image[0];
@braddalton
braddalton / functions.php
Last active May 17, 2024 11:48
Custom loop in WooCommerce that displays specific products between specific dates. Full Code https://wpsites.net/?p=115983
function display_custom_products_before_category() {
// Define the product IDs you want to display
$custom_product_ids = array(123, 456, 789); // Replace with your product IDs
// Set the date range to display the custom products (for 1 month)
$start_date = '2024-05-01'; // Start date in 'Y-m-d' format
$end_date = '2024-06-01'; // End date in 'Y-m-d' format
$current_date = date('Y-m-d');
// Check if current date is within the range
@braddalton
braddalton / widget.php
Last active May 16, 2024 15:07
Widget Slider PHP
genesis_register_sidebar( array(
'id' => 'slider',
'name' => __( 'Slider', 'agentpress' ),
'description' => __( 'This is the slider widget area.', 'agentpress' ),
) );
add_action( 'genesis_before_content_sidebar_wrap', 'slider_before_content', 5 );
function slider_before_content() {
if ( is_front_page() )
add_action( 'genesis_after_header', 'hook_slider' );
function hook_slider() {
if ( is_front_page() ) { ?>
<div class="carousel-slider">
<div class="slide"><img src="http://placehold.it/350x150&text=Slide1"></div>
<div class="slide"><img src="http://placehold.it/350x150&text=Slide2"></div>
<div class="slide"><img src="http://placehold.it/350x150&text=Slide3"></div>
<div class="slide"><img src="http://placehold.it/350x150&text=Slide4"></div>
<div class="slide"><img src="http://placehold.it/350x150&text=Slide5"></div>
<div class="slide"><img src="http://placehold.it/350x150&text=Slide6"></div>
@braddalton
braddalton / functions.php
Last active May 17, 2024 11:50
Conditional Button Text for Out of Stock Products and Specific Attribute Full Code https://wpsites.net/?p=115979
add_filter('woocommerce_product_add_to_cart_text', 'custom_combined_add_to_cart_button_text', 10, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_combined_add_to_cart_button_text', 10, 2);
function custom_combined_add_to_cart_button_text($text, $product) {
if ( ! $product->is_in_stock()) {
return __('Out of Stock', 'woocommerce');
} else {
@braddalton
braddalton / Discount per shipping method.php
Created May 15, 2024 11:50
Discount per shipping method in woocommerce
function apply_shipping_based_discounts() {
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
$chosen_shipping_method = $chosen_shipping_methods[0];
$discount = 0;
if ($chosen_shipping_method === 'free_shipping') {
$discount = 10; // Discount for free shipping
} elseif ($chosen_shipping_method === 'flat_rate') {
$discount = 5; // Discount for flat rate
} elseif ($chosen_shipping_method === 'local_pickup') {