Skip to content

Instantly share code, notes, and snippets.

View DeveloperWil's full-sized avatar

Wil Brown DeveloperWil

View GitHub Profile
@DeveloperWil
DeveloperWil / woocommerce-order-data-object-fields
Last active April 21, 2021 03:18
WooCommerce: Order data object fields
<?php
/**
* See https://woocommerce.github.io/code-reference/classes/WC-Order.html#property_data for all data items in the WC_Order
*/
/**
* This line is not needed within the filter as $order is passed as a parameter
*/
$order_data = $order->get_data();
@DeveloperWil
DeveloperWil / woocommerce-stripe-product-customer-metadata
Created March 30, 2021 22:36
WooCommerce: Stripe product and customer metadata
/**
* Add Stripe metadata along with WooCommerce purchase
*
* @param $metadata
* @param $order
* @param $source
* @return mixed
*/
function wbdc_filter_wc_stripe_payment_metadata( $metadata, $order, $source ) {
@DeveloperWil
DeveloperWil / woocommerce-global-redirect-after-payment.php
Created March 29, 2021 22:34
WooCommerce: Global redirect after payment
/* Redirect user after check out */
function zpd_wc_global_redirect_after_payment() {
global $wp;
$redirect_url = 'https://yourdomain.com/this-page/';
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( $redirect_url );
exit;
}
@DeveloperWil
DeveloperWil / sql-view-wordpress-autoloaded-data.sql
Created March 24, 2021 23:39
WordPress: View Database Autoloaded Data
SELECT 'autoloaded data in KiB' as name, ROUND(SUM(LENGTH(option_value))/ 1024) as value FROM wp_options WHERE autoload='yes'
UNION
SELECT 'autoloaded data count', count(*) FROM wp_options WHERE autoload='yes'
UNION
(SELECT option_name, length(option_value) FROM wp_options WHERE autoload='yes' ORDER BY length(option_value) DESC LIMIT 10);
@DeveloperWil
DeveloperWil / woocommerce-set-minimum-checkout-order-amount.php
Created March 9, 2021 00:30
WooCommerce: Set a minimum amount at checkout
/**
* Set a minimum amount for checkout
*/
function zpd_wc_minimum_order_amount(): void {
/**
* $minimum is the minimum value you want to set the checkout total order amount.
*/
$minimum = 50;
/**
@DeveloperWil
DeveloperWil / woocommerce-change-shop-page-order-by
Last active March 8, 2021 23:48
WooCommerce: Set default order on shop page to by price, title or date
/**
* Change the default shop page 'catalogue' order of how products are displayed
*
* You can use the following parameters:
* 'menu_order' – by the custom order first, then by product name (Default)
* 'popularity' – by the number of sales
* 'rating' – by the average rating
* 'date' – recently added products will be displayed first
* 'price' – cheapest products will be displayed first
* 'price-desc' – the most expensive first
@DeveloperWil
DeveloperWil / woocommerce-add-extra-email-recipient-completed-order
Created March 8, 2021 22:48
WooCommerce: Add an extra email recipient to the email sent when an order has been completed
/**
* Add extra emails to the WC email sent when an order has been completed.
*
* Make sure you separate multiple emails with a comma.
*
* @param $recipient
* @param $object
* @return string
*/
function zpd_wc_extra_email_recipient( $recipient, $object ): string {
@DeveloperWil
DeveloperWil / wordpress-remove-all-comments-support.php
Created February 2, 2021 03:01
WordPress: Remove All Comments Support
/**
* Removes commenting from WordPress
*
* Redirects edit comment URL to admin URL
* Removes comment meta box
* Removes comments and trackback support for all post types
*/
function zpd_remove_commenting () {
// Redirect any user trying to access comments page
global $pagenow;
@DeveloperWil
DeveloperWil / woocommerce-disable-out-of-stock-variations.js
Last active July 31, 2024 02:35
WooCommerce: Disable out-of-stock product variations from showing in the drop-down front-end UI add "Sold Out" label
/**
* Finds product varant option dropdown with " - sold out" and adds a "sold-out" class to the option.
* You can add the following CSS to grey-out the sold out option
* option.sold-out{ color:lightgray;}
*/
jQuery(window).load(function(){
jQuery('body.single-product .product.type-product table.variations td.value select option').each(function( index ){
var optionText = jQuery(this).text();
if( optionText.indexOf(" - sold out") >= 0 ){
jQuery(this).addClass( 'sold-out' );
@DeveloperWil
DeveloperWil / woocommerce-custom-order-number-based-on-billing-initials-and-random-number.php
Last active November 20, 2021 06:33
WooCommerce: Custom Order Number Based On Billing Customers Initials And Random Number between 10,000 and 99,999
/**
* Generate an order ID based on the billing customers initials and a random number between 10000 and 99999
*
* @author Wil Brown zeropointdevelopment.com
* @param $order_id
*
* @return string
* @throws Exception
*/
function zpd_change_woocommerce_order_number( $order_id ) {