Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
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_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'),
]);
}
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) {
@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)
);
@braddalton
braddalton / WC User Role Backorder Allowed
Last active June 26, 2024 08:22
Restrict users to backorder specific products
add_action('init', 'add_backorder_allowed_role');
function add_backorder_allowed_role() {
add_role(
'backorder_allowed',
__('Backorder Allowed', 'textdomain'),
array(
'read' => true, // This allows the user to read the content
// Add other capabilities as needed
)
);
@braddalton
braddalton / functions.php
Created June 10, 2024 04:55
Automatically add an extra product to the cart when a specific product is added in WooCommmerce
add_action('woocommerce_before_calculate_totals', 'ensure_two_products_in_cart');
function ensure_two_products_in_cart($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
// Replace '123' with the ID of the product you want to ensure is sold in twos
$product_id = 123;
$quantity_to_add = 2;
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == $product_id) {