Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Acephalia / wcrltd.php
Created April 7, 2023 04:50
Woocommerce Rename ‘Length’ to ‘Depth’
add_filter( 'gettext', 'rename_length_to_depth', 20, 3 );
function rename_length_to_depth( $translated_text, $untranslated_text, $domain ) {
if ( $untranslated_text == 'Length' && $domain == 'woocommerce' ) {
$translated_text = 'Depth';
}
return $translated_text;
}
@Acephalia
Acephalia / wcas.php
Last active April 10, 2023 10:35
Attribute Shortcode
// Register the shortcode [dimensiondisplay] Replace 'dimensiondisplay' below with preferred shortcode name.
add_shortcode( 'dimensiondisplay', 'dimension_display_shortcode' );
// Shortcode callback function
function dimension_display_shortcode() {
global $product;
// Get the dimensions attribute value. Replace 'dimensions' with relevant attribute name.
$dimensions = $product->get_attribute( 'dimensions' );
@Acephalia
Acephalia / wcnypcocc.php
Created April 10, 2023 21:32
WC Name Your Price: Add Price Change To Cart & Checkout
// Add Name Your Price Field on Cart page
add_filter( 'woocommerce_cart_item_price', 'add_name_your_price_field_to_cart', 10, 3 );
function add_name_your_price_field_to_cart( $product_price, $cart_item, $cart_item_key ) {
global $woocommerce;
// Check if the Name Your Price plugin is active and if the current product supports it
if ( class_exists( 'WC_Name_Your_Price' ) && WC_Name_Your_Price_Helpers::product_supports_name_your_price( $cart_item['data'] ) ) {
// Add Name Your Price Field
$name_your_price_input = woocommerce_name_your_price_input( $cart_item['product_id'], '', '', false );
@Acephalia
Acephalia / wcdco.php
Last active April 11, 2023 13:51
Woocommerce Direct Checkout
//Skip Cart & Redirect To Checkout
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
function custom_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
@Acephalia
Acephalia / wconoce.php
Last active April 17, 2023 04:42
Woocomerce Add Order Note With Specific Words To Order Completed Notification Email
//This code snippet will search through the order notes for the words 'Loren ipsum' and then display that note in the Order Completed Email sent to a customer.
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'customer_completed_order' ) {
// Get order id
$order_id = $order->get_id();
// Get order notes
$order_notes = wc_get_order_notes( array(
@Acephalia
Acephalia / Readme.txt
Last active April 17, 2023 13:38
Woocommerce change order status to complete after payment clears.
The snippets below adds an action hook that listens for incoming webhook requests. When a webhook request is received, the code retrieves the order ID from the metadata sent with the webhook. It then checks if the order exists and the payment has not already been marked as completed. If these conditions are met, the code updates the order status to "completed" using the update_status() method of the order object.
@Acephalia
Acephalia / wcdmsrfsc.php
Created April 20, 2023 00:55
Woocommerce Display Monthly Revenue For A Specific Country
// Add a new menu item to the WordPress admin menu
function add_australian_sales_menu_item() {
add_menu_page(
'Australian Sales', // Page title
'Australian Sales', // Menu title
'manage_options', // Capability required to access the page
'australian-sales', // Menu slug
'display_australian_sales_page' // Callback function that displays the page
);
}
@Acephalia
Acephalia / wcafcl.php
Created April 20, 2023 23:56
Stop checkout and log order to text file.
//This code will fail the checkout process with a custom message. The order time and products that were attemtpted to be purchased will be logged to a text file in wp/content and the log will be displayed under Checkout Log in the wordpress Dashboard.
// Add custom admin page
add_action('admin_menu', 'auto_fail_custom_admin_page');
function auto_fail_custom_admin_page() {
add_menu_page(
'Checkout Log',
'Checkout Log',
'manage_options',
'checkout-log',
@Acephalia
Acephalia / wcjbv.js
Last active April 21, 2023 13:26
Woocommerce Jump Between Variations Without Reset
<script>
// This code allows users to switch between the first variation of a product regardless of whether the second variation selection is availaible for that combination. This works by displaying all combinations for the first variation. If a particular combination is not availaible for the first selected variation a message will be displayed and the second variation will be reset. Credit to user : https://stackoverflow.com/users/10755574/intelligent-web-solution for the original solution.
jQuery(document).ready(function($){
if (jQuery('form.variations_form').length) {
let $form = jQuery('form.variations_form');
let $first_attr_select = $form.find( '.variations select' ).eq(0);
let first_attr_val = $first_attr_select.val() || '';
$first_attr_select.on('change', function(e){
@Acephalia
Acephalia / wchecfnlug.php
Last active April 26, 2023 22:47
Hide empty categories from 'customer' usergroup & non logged in users and 404 direct access
//Hide empty categories from 'customer' usergroup & non logged in users. Display to everyone else.
add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_categories_for_logged_in_users', 999 );
function show_categories_for_logged_in_users( $hide_empty ) {
//change'customer' to match needed usergroup
if ( is_user_logged_in() && ! current_user_can( 'customer' ) ) {
$hide_empty = false;
}
return $hide_empty;
}