Skip to content

Instantly share code, notes, and snippets.

View braddalton's full-sized avatar

Brad Dalton braddalton

View GitHub Profile
@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) {
@braddalton
braddalton / functions.php
Created June 10, 2024 04:50
WooCommerce set % discount when 2 or more products are in shopping cart
function custom_cart_discount() {
$discount_percentage = 10; // Replace with your desired discount percentage
$cart = WC()->cart;
if ( $cart->get_cart_contents_count() >= 2 ) {
$discount = $cart->subtotal * ($discount_percentage / 100);
$cart->add_fee( 'Bulk Discount', -$discount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_discount' );
@braddalton
braddalton / content-product.php
Created June 7, 2024 14:35
WooCommerce Get the custom price per square meter
// Get the product object
global $product;
// Get the regular price
$price_per_tile = $product->get_price();
// Get the custom price per square meter (assuming it is stored as a custom field)
$price_per_m2 = get_post_meta( $product->get_id(), 'price_per_square_meter', true );
// Display the prices with descriptions
add_action('loop_start', 'list_woocommerce_shipping_zones');
function list_woocommerce_shipping_zones() {
if ( current_user_can( 'manage_woocommerce' ) ) {
$zones = WC_Shipping_Zones::get_zones();
foreach ($zones as $zone) {
echo 'Shipping Zone Name: ' . $zone['zone_name'] . ' | ID: ' . $zone['id'] . '<br>';
}
function conditionally_hide_local_pickup( $rates, $package ) {
$minimum_amount = 50; // Set your minimum order amount here
// Get the cart total
$cart_total = WC()->cart->get_displayed_subtotal();
// Check if cart total is less than the minimum amount
if ( $cart_total < $minimum_amount ) {
// Loop through the available shipping methods
foreach ( $rates as $rate_id => $rate ) {
// Add €40 deposit to the cart based on product ID
add_action('woocommerce_cart_calculate_fees', 'add_deposit_fee');
function add_deposit_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
$deposit_fee = 40; // Deposit amount
$product_ids = array(391); // Replace with your product IDs
$add_deposit = false;
add_action('init', 'custom_add_customer_groups');
function custom_add_customer_groups() {
add_role('group1', __('Group 1'), array());
add_role('group2', __('Group 2'), array());
add_role('group3', __('Group 3'), array());
}
@braddalton
braddalton / Products by Brand in WooCommerce.php
Last active May 24, 2024 03:32
Products by Brand in WooCommerce. Full Tutorial https://wpsites.net/?p=116042
add_action( 'woocommerce_after_single_product_summary', 'display_on_sale_products_by_brand', 15 );
function display_on_sale_products_by_brand() {
global $post;
// Get the brands of the current product
$brands = wp_get_post_terms( $post->ID, 'brand' );
if ( ! empty( $brands ) && ! is_wp_error( $brands ) ) {
foreach ( $brands as $brand ) {
@braddalton
braddalton / wc-product-functions.php
Last active May 24, 2024 07:47
Use woocommerce_placeholder_img_src filter hook. This is the original function containing the filter. Full Tutorial https://wpsites.net/?p=115999
function wc_placeholder_img_src( $size = 'woocommerce_thumbnail' ) {
$src = WC()->plugin_url() . '/assets/images/placeholder.png';
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( ! empty( $placeholder_image ) ) {
if ( is_numeric( $placeholder_image ) ) {
$image = wp_get_attachment_image_src( $placeholder_image, $size );
if ( ! empty( $image[0] ) ) {
$src = $image[0];