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_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 / 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 )
@braddalton
braddalton / Child Categories on Parent Archive.php
Last active July 29, 2024 20:39
WooCommerce Child Categories on Parent Category Archive Page
// Hook into the WooCommerce before shop loop to display subcategories
add_action('woocommerce_before_shop_loop', 'display_woo_subcategories', 15);
function display_woo_subcategories() {
if (!is_product_category()) {
return;
}
$term_id = get_queried_object_id();
$taxonomy_name = 'product_cat';
$termchildren = get_term_children($term_id, $taxonomy_name);
@braddalton
braddalton / buy-x-get-number-free.php
Last active July 24, 2024 15:30
Buy x Amount Get x Number Free WooCommerce Full tutorial and support @ https://wpsites.net/?p=116705
add_action('woocommerce_cart_calculate_fees', 'buy_ten_get_two_free', 10, 1 );
function buy_ten_get_two_free( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Set HERE your targeted variable products IDs
$targeted_product_ids = array( 185 );
$each_n_items = 10; // Number of items required to get a free one
$free_items_count = 2; // Number of free items to give
$discount = 0; // Initializing