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 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_filter( 'single_template', 'wpsites_single_post_type_template' );
function wpsites_single_post_type_template($single_template) {
global $post;
if ($post->post_type == 'post' ) {
$single_template = dirname( __FILE__ ) . '/single.php';
}
return $single_template;
}
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>
@braddalton
braddalton / Discount 6th order woocommerce.php
Last active August 14, 2024 11:38
Once the user completes 5 purchases, the code automatically applies a discount to their 6th purchase. After applying the discount, the order count resets.
add_action('woocommerce_order_status_completed', 'track_completed_orders_101', 10, 1);
function track_completed_orders_101($order_id) {
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
if ($user_id) {
$completed_orders = get_user_meta($user_id, '_completed_orders_count', true);
$completed_orders = $completed_orders ? $completed_orders + 1 : 1;
update_user_meta($user_id, '_completed_orders_count', $completed_orders);
}
@braddalton
braddalton / woocommerce-loop.php
Last active August 6, 2024 06:37
Custom Product Loop - WooCommerce https://wpsites.net/?p=109696
add_action( 'woocommerce_after_single_product', 'wpsitesdotnet_custom_wc_wp_query' );
function wpsitesdotnet_custom_wc_wp_query() {
echo '<ul class="products">';
$args = array(
'post_type' => 'product',
'posts_per_page' => 3
);
@braddalton
braddalton / Limit Category Link Product Page.php
Created August 4, 2024 06:51
How To Limit Category Link on Product Page in WooCommerce ( On Non Block Themes )