Skip to content

Instantly share code, notes, and snippets.

View alexdeborba's full-sized avatar
:octocat:
DevOps

Alex de Borba alexdeborba

:octocat:
DevOps
View GitHub Profile
@alexdeborba
alexdeborba / gist:167a09d7e766cc1621d01b47b348139b
Last active June 8, 2023 11:36
Action to show on Single Product only Related Products to Product Archive
// Remove default related products function
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
// Add our custom related products function to the same hook
add_action( 'woocommerce_after_single_product_summary', 'custom_related_products', 20 );
function custom_related_products() {
// Use global $product object
global $product;
// Prepare the arguments for WP_Query
@alexdeborba
alexdeborba / gist:72e0de0db7312d848432eeb396504539
Created June 8, 2023 11:42
Function to Disable Free Orders Completed Email
/**
* This function checks if an order has a total of zero (free), and if so, stops the "Completed order" email from being sent.
*
* @param bool $is_enabled - Original state of the email (enabled/disabled)
* @param WC_Order $order - The order object for which the email is being sent
* @return bool - Modified state of the email (enabled/disabled)
*/
function disable_free_order_completed_email( $is_enabled, $order ) {
// Check if the order total is zero
if ( 0 == $order->get_total() ) {
@alexdeborba
alexdeborba / gist:14389d06fd060a8d53f0af861a8e4cdf
Created June 8, 2023 12:53
WooCommerce: Action to limit User Role to a number items per Order
// Add an action on cart evaluation
add_action( 'woocommerce_check_cart_items', 'limit_cart_items_for_specific_role' );
function limit_cart_items_for_specific_role() {
// The user role to check against
$user_role_to_check = 'subscriber';
// The item limit
$item_limit = 3;
// Get the current user
@alexdeborba
alexdeborba / gist:d7a24b1ea9bb5ee16dde5392bec6e722
Created June 14, 2023 18:49
WooCommerce: Hide Price if Value is 0
// Add this code to your child theme's functions.php file
add_filter( 'woocommerce_get_price_html', 'hide_zero_price', 10, 2 );
/**
* Hide price if it is zero
*
* @param string $price The price (HTML formatted).
* @param WC_Product $product The product object.
* @return string
@alexdeborba
alexdeborba / gist:1dca47ee02bac23a20f8c2afe0409348
Created July 25, 2023 22:37
Disable Jetpack modules except WooCommerce App
add_filter( 'jetpack_get_default_modules', 'disable_all_jetpack_modules_except_woocommerce' );
function disable_all_jetpack_modules_except_woocommerce( $modules ) {
// List of Jetpack modules needed for WooCommerce App.
$required_modules = array(
'json-api', // Required for the WooCommerce Mobile App.
// Add other modules as needed.
);
// Disable all modules except those in the list above.
// Allow wholesale customers to view all products
function allow_wholesale_customer_view_all_products($query) {
// Check if the user has the "wholesale_customer" role and if it's the main query
if (current_user_can('wholesale_customer') && $query->is_main_query()) {
// Set the query to retrieve products only
$query->set('post_type', 'product');
// Set the post status to include both published and private products
$query->set('post_status', ['publish', 'private']);
// Ignore sticky posts
$query->set('ignore_sticky_posts', true);
@alexdeborba
alexdeborba / gist:2feaa5c8dac619db939d75c4134ca7a9
Created June 3, 2024 23:23
Prevent shipping to PO box addresses in WooCommerce
/**
* Prevent shipping to PO box addresses in WooCommerce
*/
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode($posted) {
// Determine if we are checking the shipping or billing address
$address = isset($posted['shipping_address_1']) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = isset($posted['shipping_postcode']) ? $posted['shipping_postcode'] : $posted['billing_postcode'];