Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
@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
@braddalton
braddalton / functions.php
Created July 15, 2024 04:18
Enable Google Pay and Apple Pay for event product type https://wpsites.net/
function enable_google_apple_pay_for_event_tickets($available_gateways) {
if (is_admin()) return $available_gateways;
global $product;
if (!$product) return $available_gateways;
// Check if the product is an event ticket
$is_event_ticket = has_term('event', 'product_cat', $product->get_id());
if ($is_event_ticket) {
add_action('woocommerce_product_options_general_product_data', 'custom_woocommerce_product_fields');
function custom_woocommerce_product_fields() {
woocommerce_wp_text_input([
'id' => '_custom_field',
'label' => __('Custom Field', 'woocommerce'),
'desc_tip' => 'true',
'description' => __('Enter the value for the custom field.', 'woocommerce'),
]);
}
@braddalton
braddalton / functions.php
Created July 9, 2024 14:49
WooCommerce Online Stock and Instore Stock
// Step 1: Add a meta box for store stock field
function custom_store_stock_meta_box() {
add_meta_box(
'custom_store_stock_meta_box', // Meta box ID
'Store Stock', // Title
'render_custom_store_stock_meta_box', // Callback function to render the meta box content
'product', // Post type (product for WooCommerce products)
'side', // Context (side, normal, advanced)
'default' // Priority (high, core, default, low)
);