Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
jorpdesigns / replace-default-product-tabs.php
Created July 8, 2021 17:40
Snippet to remove default product tabs and add custom tabs on WooCommerce product page
<?php
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 20 );
function remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
@jorpdesigns
jorpdesigns / customer-membership-class.php
Created July 11, 2021 17:16
Snippet to add customer WooCommerce Membership plan as body class
<?php
add_filter( 'body_class', 'membership_body_class' );
function membership_body_class( $classes ) {
if ( ! function_exists( 'wc_memberships' ) ) {
return $classes;
}
$user_id = get_current_user_id();
$args = array(
@jorpdesigns
jorpdesigns / download-purchase-check.php
Created July 11, 2021 17:18
Function to check if a customer has already purchased a downloadable WooCommerce product
<?php
function customer_has_download( $product_id ) {
$has_download = false;
$user_id = get_current_user_id();
if ( $user_id ) {
$downloads = wc_get_customer_available_downloads( $user_id );
if ( ! empty($downloads) ) {
foreach ($downloads as $download) {
@jorpdesigns
jorpdesigns / membership-product-upsells.php
Created July 11, 2021 17:25
Function that returns upsells of products that grants access to a user's WooCommerce Membership plan
<?php
function get_year_group_upsells() {
$user_id = get_current_user_id();
$args = array(
'status' => array( 'active', 'pending-cancellation' ), // Add or remove other statuses if you want
);
// Get user memberships
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
@jorpdesigns
jorpdesigns / product-ids-sort.php
Created July 11, 2021 17:45
Function to sort an array of WooCommerce product ids based on custom value
<?php
// This function sorts the product based on menu order. You can get other numerical conditions here: https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/#more-72711
function product_multiarray_sort( $productIDsArray ) {
$productMultiArray = array();
$sortedProductIDsArray = array();
foreach ( $productIDsArray as $productID ) {
$product = wc_get_product( $productID );
@jorpdesigns
jorpdesigns / replace-cart-button-with-download-single.php
Last active July 11, 2021 17:54
Snippet to replace "Add to Cart" with "Download" button on WooCommerce product page if logged-in customer has purchased downloadable product
<?php
// IMPORTANT: Add customer_has_download() function from here - https://gist.github.com/jorpdesigns/d155eaf0fe3260b0f59d514cca5004ed
add_action( 'template_redirect', 'product_single_download_button_check' );
function product_single_download_button_check() {
if ( customer_has_download( get_queried_object_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'product_single_download_button', 30 );
}
@jorpdesigns
jorpdesigns / replace-cart-button-with-download-archive.php
Created July 11, 2021 17:58
Snippet to replace "Add to Cart" with "Download" button on WooCommerce archive page if logged-in customer has purchased downloadable product
<?php
// IMPORTANT: Add customer_has_download() function from here - https://gist.github.com/jorpdesigns/d155eaf0fe3260b0f59d514cca5004ed
add_action( 'woocommerce_after_shop_loop_item', 'product_archive_download_button_check', 1 );
function product_archive_download_button_check() {
global $product;
if ( $product->is_downloadable() && customer_has_download( $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
@jorpdesigns
jorpdesigns / remove-downloadable-product-link.php
Created July 11, 2021 18:00
Snippet to remove WooCommerce product link in Downloads table and display product title only
@jorpdesigns
jorpdesigns / check-customer-subscription.php
Created July 11, 2021 18:05
Function to check if a customer has a WooCommerce Subscription
<?php
function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) {
if ( empty($the_user_id) ) {
$the_user_id = get_current_user_id();
}
if (wcs_user_has_subscription( $the_user_id, $the_product_id, $the_status)) {
return true;
}
}
@jorpdesigns
jorpdesigns / display-subcategories.php
Last active July 26, 2021 16:32
Shortcode to display subcategories under a WooCommerce parent category
<?php
// Shortcode example: [display_subcats id="55"]. Replace 55 with your parent category ID
add_shortcode('display_subcats', 'display_subcats');
function display_subcats( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'id' => ''