Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
@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
@braddalton
braddalton / Pre Load Product Images.php
Created July 24, 2024 08:33
Pre Load Product Images WooCommerce
add_action('wp_head', 'rl_add_preload', 1);
function rl_add_preload() {
global $product;
if (is_product() && isset($product)) {
$image = wp_get_attachment_image_src($product->get_image_id(), "edit");
if ($image && is_array($image) && isset($image[0])) {
echo PHP_EOL . '<link rel="preload" as="image" href="' . esc_url($image[0]) . '">' . PHP_EOL;
function remove_quantity_field_for_sold_individually_products() {
if ( is_product() ) {
global $product;
if ( is_object( $product ) && $product->is_sold_individually() ) {
// Remove the quantity field
remove_action( 'woocommerce_before_add_to_cart_quantity', 'woocommerce_template_single_add_to_cart', 10 );
remove_action( 'woocommerce_after_add_to_cart_quantity', 'woocommerce_template_single_add_to_cart', 10 );
}
@braddalton
braddalton / Add Category Name To Order Details
Created July 17, 2024 07:03
How To Add Category Name To Order Details in WooCommerce
add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 );
function display_custom_data_in_emails( $item_id, $item, $order, $bool ) {
$terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
echo "<br><small>" . implode(', ', $terms) . "</small>";
}
add_action( 'woocommerce_after_order_itemmeta', 'custom_admin_order_itemmeta', 15, 3 );
add_action( 'woocommerce_single_product_summary', 'display_discount_table', 20 );
function display_discount_table() {
global $product;
if ( ! $product->is_type( 'simple' ) ) {
return;
}
$discounts = [
add_filter('woocommerce_package_rates', 'custom_woocommerce_available_shipping_methods', 100);
function custom_woocommerce_available_shipping_methods($rates) {
$free_shipping_minimum = 100; // Set your minimum cart total for free shipping here
$cart_total = WC()->cart->get_subtotal();
if ($cart_total >= $free_shipping_minimum) {
foreach ($rates as $rate_id => $rate) {
if ('free_shipping' === $rate->method_id) {
$free_shipping_rate = $rates[$rate_id];
$rates = array($rate_id => $free_shipping_rate);
// Add a flat rate fee for a specific product
add_action( 'woocommerce_cart_calculate_fees', 'add_flat_rate_fee_for_specific_product_v1', 10, 1 );
function add_flat_rate_fee_for_specific_product_v1( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$product_id = 123; // Replace with the ID of your specific product
$flat_rate_fee = 10; // Replace with your desired flat rate fee