Skip to content

Instantly share code, notes, and snippets.

@bekarice
bekarice / wc-add-order-item-meta-to-rest-response.php
Created December 28, 2016 20:18
Example: Add order item meta to WC REST API
<?php // only copy if needed
/**
* Example: Add order meta to the REST API
* WC 2.6+
*
* @param \WP_REST_Response $response The response object.
* @param \WP_Post $post Post object.
* @param \WP_REST_Request $request Request object.
* @return object updated response object
@bekarice
bekarice / wc-sku-generator-replace-variation-separator.php
Created December 27, 2016 18:48
2 Examples on how to change variation SKU separators w/ Product SKU Generator
<?php // only copy this line if needed
/**
* Example 1: Changes the separator between variations in a generated SKU
*
* @param string $separator the original separator
* @return string the new separator
*/
function sv_wc_replace_variation_sku_separator( $separator ) {
return '_';
@bekarice
bekarice / registration-order-link-for-woocommerce.php
Last active August 16, 2023 06:13
Automatically link previous orders to new WooCommerce customer accounts upon registration
@bekarice
bekarice / wc-custom-order-action-sample.php
Created October 24, 2016 06:55
Add a WooCommerce custom order action
<?php // only copy if needed
/**
* Add a custom action to order actions select box on edit order page
* Only added for paid orders that haven't fired this action yet
*
* @param array $actions order actions array to display
* @return array - updated actions
*/
function sv_wc_add_order_meta_box_action( $actions ) {
@bekarice
bekarice / wc-hide-non-free-shipping.php
Created September 22, 2016 04:11
Show only free / $0 shipping methods when available
<?php // only copy if needed
/**
* Hides any non-free shipping methods if free shipping is available
*
* @param array $rates array of \WC_Shipping_Rate objects that apply to the cart
* @return array - the updated available rates
*/
function sww_wc_hide_non_free_shipping( $rates ) {
<?php
// add custom column headers
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
'column_1' => 'Column 1',
'column_2' => 'Column 2',
// add other column headers here in the format column_key => Column Name
);
@bekarice
bekarice / wc-prevent-checkout-for-cart-with-specific-category.php
Last active June 29, 2023 20:32
Prevents checkout if the WooCommerce cart only contains items from a specific category
<?php // only copy this line if needed
/**
* Renders a notice and prevents checkout if the cart
* only contains products in a specific category
*/
function sv_wc_prevent_checkout_for_category() {
// set the slug of the category for which we disallow checkout
$category = 'clothing';
@bekarice
bekarice / sv-wc-random-product-sorting.php
Created July 28, 2016 18:02
WooCommerce: Add random product sorting option to shop / archives
<?php // only copy if needed
/**
* Adds randomized product sorting option to WooCommerce stores (WC 2.3+)
*
* @param array $sortby the orderby options for product sorting
* @return array - updated sorting options
*/
function sv_random_woocommerce_catalog_orderby( $sortby ) {
$sortby['rand'] = __( 'Sort by: random order', 'my-textdomain' );
@bekarice
bekarice / sv-wc-get-active-memberships.php
Created July 27, 2016 17:28
Get active memberships for a user with WC Memberships
<?php
/**
* Helper function to get all active memberships for a user
*
* @param int|\WP_User $user_id Optional, defaults to current user
* @return array - empty array|array of active memberships for the user
*/
function sv_wc_memberships_get_active_memberships( $user_id = null ) {