Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
'US' => array(
'default_fee' => 100,
'over_threshold_fee' => 5,
'under_threshold_fee' => 10
),
@braddalton
braddalton / woocommerce-country-codes
Created May 8, 2024 14:12
List of Country Codes for WooCommerce PHP Coding
AX Åland Islands
AF Afghanistan
AL Albania
DZ Algeria
AD Andorra
AO Angola
AI Anguilla
AQ Antarctica
AG Antigua and Barbuda
AR Argentina
@braddalton
braddalton / wc-country-code-fees.php
Created May 8, 2024 13:41
Extra Fee Based on Billing Country At Checkout in WooCommerce https://wpsites.net/?p=115829
// Define the targeted countries and the corresponding fee
$target_countries = array(
'US' => 10, // Add $10 fee for customers from the United States
'CA' => 15, // Add $15 fee for customers from Canada
// Add more countries and fees as needed
);
@braddalton
braddalton / Woocommerce Email Hooks.php
Created February 1, 2024 06:20
Woocommerce Email Hooks
add_action( 'woocommerce_email_header', array( $object, 'email_header' ) );
add_action( 'woocommerce_email_footer', array( $object, 'email_footer' ) );
add_action( 'woocommerce_email_order_details', array( $object, 'order_details' ), 10, 4 );
add_action( 'woocommerce_email_order_details', array( $object, 'order_schema_markup' ), 20, 4 );
add_action( 'woocommerce_email_order_meta', array( $object, 'order_meta' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $object, 'customer_details' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $object, 'email_addresses' ), 20, 3 );
add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 );
add_action( 'woocommerce_email_order_details', array( $this, 'output_email_structured_data' ), 30, 3 );
@braddalton
braddalton / Show How Many Products Left in Stock
Last active December 19, 2023 06:35
WooCommerce Show How Many Products Left in Stock - Full Tutorial & Support https://wpsites.net/?p=114708
/**
* @author Brad Dalton
* @link https://wpsites.net/?p=114708
* @copyright https://wpsites.net/disclosure-privacy/
*/
// Hook to display stock quantity on the product page
add_action( 'woocommerce_before_add_to_cart_button', 'display_stock_quantity', 10 );
// Function to display stock quantity for each product
@braddalton
braddalton / functions.php
Created December 16, 2023 17:54
Load jQuery
function enqueue_custom_scripts() {
wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
@braddalton
braddalton / custom-script.js
Last active December 16, 2023 17:54
How to add a class to submenu items when they are clicked https://wpsites.net/?p=114703
jQuery(document).ready(function($) {
// Add click event to submenu items
$('.sub-menu li').on('click', function() {
// Remove 'active' class from all submenu items
$('.sub-menu li').removeClass('active');
// Add 'active' class to the clicked submenu item
$(this).addClass('active');
});
});
@braddalton
braddalton / WC Exclude On Sale Products Shop Page.php
Last active December 14, 2023 13:04
WooCommerce Exclude On Sale Products from Shop Page https://wpsites.net/?p=114677
add_action('pre_get_posts', 'wc_exclude_sale_items_from_shop_page');
function wc_exclude_sale_items_from_shop_page($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Check if it's the main shop page
if ( is_shop() ) {
$query->set('meta_query', array(
@braddalton
braddalton / Custom WC Settings Tab
Created December 13, 2023 13:35
Add Custom Settings Tab for Number Field to WooCommerce https://wpsites.net/?p=114654
add_action('woocommerce_settings_tabs_exchange_rates', 'add_exchange_rate_usd_field');
add_action('woocommerce_admin_field_number', 'add_exchange_rate_usd_field');
// Add custom exchange rate field to WooCommerce settings under a new tab
function add_exchange_rate_usd_field() {
woocommerce_admin_fields(array(
array(
'type' => 'title',
'id' => 'exchange_rates_tab_title',
'title' => __('Exchange Rates', 'woocommerce'),
@braddalton
braddalton / wc-manual-order-currency-change.php
Created December 13, 2023 13:13
Change Currency for Manual Orders in WooCommerce https://wpsites.net/?p=114652
add_action('woocommerce_new_order', 'set_custom_currency_for_manual_orders', 10, 1);
function set_custom_currency_for_manual_orders($order_id) {
// Check if the order is manual (admin-created) by looking at the order status
$order = wc_get_order($order_id);
$order_status = $order->get_status();
// Specify the custom currency code you want to set for manual orders
$custom_currency_code = 'EUR';
// Check if the order is manual (e.g., status is 'on-hold' for admin-created orders)