Skip to content

Instantly share code, notes, and snippets.

View woogists's full-sized avatar

WooGists woogists

View GitHub Profile
@woogists
woogists / wc_gc_set_message_limit.php
Created February 27, 2025 20:46
Limit the number of characters that are accepted in the Gift Card Message field
<?php
/*
By default, customers are allowed to input as many characters as they’d like in the “Message” field of a Gift Card form.
To limit the number of characters that are accepted in this field, you can use this snippet.
Note: This snippet sets a limit to 300 characters. To set a different limit, it is necessary to modify the value of the "maxlength" attribute.
*/
add_action( 'init', 'sw_gc_set_message_limit' );
function sw_gc_set_message_limit() {
@woogists
woogists / wc-gc-add-custom-field.php
Created February 27, 2025 20:34
an example snippet of how to add a custom field to the Gift Cards by WooCommerce extension
<?php
/*
Below, is an example snippet of how to add a "Recipient Name" field to the Gift Cards by WooCommerce extension
Adding a new field in this form is a technically demanding task, as it must:
- be displayed in the single product page,
- be validated when the Add to Cart button is clicked,
- be included in the Gift Cards Importer/Exporter and;
- be included as a placeholder in WooCommerce > Settings > Emails > Gift Card received.
@woogists
woogists / wc_pb_enable_zoom_bundled_items_images.php
Created February 25, 2025 13:03
Allow customers to zoom into bundled item images on hover
<?php
add_filter( 'woocommerce_bundle_front_end_params', 'sw_pb_enable_zoom_bundled_items_images' );
function sw_pb_enable_zoom_bundled_items_images( $frontend_params ) {
$frontend_params[ 'zoom_enabled' ] = 'yes';
return $frontend_params;
}
@woogists
woogists / woo-composite-products-disable-empty-filters.php
Last active February 24, 2025 17:22
Hide attributes which match no products when option filtering is enabled on a component of a composite product
<?php
/*
When you enable the Options Filtering option in a component, a field shows up where you can select some product attributes.
The selected attributes and their terms are shown on the front-end to be used as filters.
Composite Products does not perform any checks to make sure that at least one product exists for each term.
This is because enforcing these checks would have a significant performance impact on the page.
Use this snippet to automatically remove any terms that do not have any matching products.
*/
add_filter( 'woocommerce_composite_component_filters', 'sw_cp_disable_empty_filters', 10, 3 );
@woogists
woogists / wc-composite-products-default-component-quality.php
Created February 24, 2025 16:21
Use this snippet to set a default quantity for a component of a composite product.
<?php
/*
Use this snippet to set a default quantity for a component of a composite product.
Before using this snippet, replace 1234 with the ID of the component for which you’d like to set a default quantity.
You can find the ID of each component by: navigating to Product Data > Components and hovering over a specific component
*/
add_filter( 'woocommerce_composited_product_quantity', 'sw_wc_cp_set_default_quantity', 10, 6 );
@woogists
woogists / wc-brands-add-to-product-loop.php
Last active December 26, 2024 21:14
[WooCommerce Brands] Add Brands to Product Loop
<?php
if ( is_plugin_active( 'woocommerce-brands/woocommerce-brands.php' ) ) {
add_action( 'woocommerce_shop_loop_item_title', 'add_brands_to_product_loop' );
// Add brands to product loop.
function add_brands_to_product_loop() {
$terms = get_the_terms( get_the_ID(), 'product_brand' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$term = join( ', ', wp_list_pluck( $terms, 'name' ) );
@woogists
woogists / woocommerce-subscriptions-download-access-after-cancelled.php
Last active December 2, 2024 18:16
[WooCommerce Subscriptions]: Give access to downloadable files even if Subscription is cancelled
<?php
/*
* Access to downloadable files associated with a subscription will, by default, expire
* when the subscription is no longer "active" or "pending-cancel".
* https://woocommerce.com/document/subscriptions/faq/#section-39
* This snippet overrides that behavior to allow access as per the Download Expiry settng
* when the subscription status is "cancelled"
*/

Brands REST API

The Brands REST API allows you to create, view, update, and delete individual, or a batch, of brands. The endpoint is /wp-json/wc/v1/products/brands which basically mimics /wp-json/wc/v1/products/categories. You can refer to the same documentation of product categories REST API.

In addition to /products/brands endpoints, the /products endpoints also updated to display brands in the response and check the JSON request for brands.

Examples

  • Retrieve all product brands:
@woogists
woogists / phpcs.xml
Last active October 31, 2024 22:49
PHP_CodeSniffer ruleset for marketplace products
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards">
<description>WooCommerce extension PHP_CodeSniffer ruleset.</description>
<!-- Exclude paths -->
<exclude-pattern>tests/</exclude-pattern>
<exclude-pattern>woo-includes/woo-functions.php</exclude-pattern>
<exclude-pattern>woo-includes/class-wc-dependencies.php</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
@woogists
woogists / wc-disable-default-stylesheet.php
Last active October 8, 2024 22:20
[Theming Snippets] Disable the default stylesheet
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );