Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
@braddalton
braddalton / free-shipping.php
Last active October 30, 2025 09:04
Swap out the product ID 6455 on line 3 of the code to match your free shipping product. Full Tutorial https://wpsites.net/free-tutorials/free-shipping-per-product-woocommerce/
add_filter( 'woocommerce_package_rates', 'wpsites_show_only_free_shipping_for_specific_product', 20, 2 );
function wpsites_show_only_free_shipping_for_specific_product( $rates, $package ) {
$target_product_id = 6455; // 🔹 Replace with your product ID
$product_in_cart = false;
// Check if the target product is in the cart
foreach ( $package['contents'] as $item ) {
if ( $item['product_id'] == $target_product_id ) {
$product_in_cart = true;
break;
@braddalton
braddalton / WooCommerce-Brands.php
Last active May 8, 2025 19:51
Add Thumbnail Images for Brands to the Single Product Page in WooCommerce https://wpsites.net/wordpress-tutorials/display-all-brand-thumbnails-on-product-page/
add_action( 'woocommerce_single_product_summary', 'display_all_brand_thumbnails_on_product_page', 25 );
function display_all_brand_thumbnails_on_product_page() {
global $post;
$brands = wp_get_post_terms( $post->ID, 'product_brand' );
if ( ! empty( $brands ) && ! is_wp_error( $brands ) ) {
// Output container for brand logos
echo '<div class="product-brand-thumbnails">';
add_action( 'woocommerce_single_product_summary', function() {
global $product;
$gtin = get_post_meta( $product->get_id(), '_global_unique_id', true );
if ( $gtin ) {
echo '<p class="product-gtin">GTIN: ' . esc_html( $gtin ) . '</p>';
}
}, 25 );
function enqueue_modal_popup_script_on_checkout() {
if (is_checkout()) {
// Enqueue jQuery (if it's not already loaded)
wp_enqueue_script('jquery');
?>
<style type="text/css">
/* Modal Styling */
.modal {
display: none; /* Hidden by default */
@braddalton
braddalton / functions.php
Last active October 15, 2024 06:25
Add custom HTML to the WooCommerce order confirmation email and thank you page
// Add custom HTML to the WooCommerce thank you page
add_action('woocommerce_thankyou', 'custom_html_confirmation_page_1001');
function custom_html_confirmation_page_1001($order_id) {
$order = wc_get_order($order_id);
echo '<div class="custom-confirmation-message">';
echo '<h2>Thank you for your purchase!</h2>';
echo '<p>Your order number is: ' . $order->get_order_number() . '</p>';
echo '<p>We appreciate your business and will send you a confirmation email shortly.</p>';
echo '</div>';
}
@braddalton
braddalton / tiktok-pixel.php
Created October 13, 2024 04:57
PHP code to install the Tik Tok Pixel in WordPress child themes functions.php file
// Version 1.0
function add_tiktok_pixel() {
?>
<!-- TikTok Pixel Code -->
<script>
!function (w, d, t) {
w.TiktokAnalyticsObject = t;
var ttq = w[t] = w[t] || [];
ttq.methods = ["page", "track", "identify", "instances", "debug", "on", "off", "once", "ready", "alias", "group", "enableCookie", "disableCookie"];
ttq.setAndDefer = function (t, e) {
add_filter('woocommerce_package_rates', 'wpsites_remove_flat_rate_methods_over_150', 10, 2);
function wpsites_remove_flat_rate_methods_over_150($rates, $package) {
$cart_total = WC()->cart->get_cart_contents_total();
$threshold = 150;
// Define the shipping method IDs to remove if cart total exceeds $150
$methods_to_remove = array(
'flat_rate:12',
add_filter('woocommerce_package_rates', 'switch_flat_rate_based_on_total_v3', 10, 2);
function switch_flat_rate_based_on_total_v3($rates, $package) {
$cart_total = WC()->cart->get_cart_contents_total();
$flat_rate_5 = 'flat_rate:5'; // ID for flat rate 5
$flat_rate_7 = 'flat_rate:7'; // ID for flat rate 7
if ($cart_total > 150) {
if (isset($rates[$flat_rate_5])) {
unset($rates[$flat_rate_5]);
add_action('woocommerce_product_options_general_product_data', 'add_custom_fields');
function add_custom_fields() {
woocommerce_wp_text_input( array(
'id' => '_shipping_detail',
'label' => 'Shipping Detail',
'desc_tip' => 'true',
'description' => 'Enter the shipping details.',
));
woocommerce_wp_text_input( array(
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_addons', 10 );
function custom_product_addons() {
global $product;
$product_price = $product->get_price(); // Base price of the product.
?>
<div id="product-warranty-addons">
<p>Select Additional Warranty:</p>
<button type="button" class="warranty-addon" data-addon="15" data-price="<?php echo esc_attr( $product_price ); ?>">
Additional 4-Month Warranty (+15%)
</button>