Skip to content

Instantly share code, notes, and snippets.

View bolderelements's full-sized avatar

Erica Dion bolderelements

View GitHub Profile
@bolderelements
bolderelements / wc-discount-shipping-qty-based.php
Created July 10, 2020 18:45
Discount shipping cost based on number of items being purchased
/**
* Discount shipping cost based on number of items being purchased
* Further narrowed down to only run on BE Table Rate options
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
add_filter( 'woocommerce_package_rates', 'add_shipping_percentage_discount', 10, 2 );
function add_shipping_percentage_discount( $rates, $package ) {
// get number of items
@bolderelements
bolderelements / wc-show-free-shipping-text.php
Last active July 9, 2020 19:04
Show the Word "FREE" When Shipping is $0 from Third Party Plugin
/**
* Show the word "FREE" when shipping is free from third party shipping plugins
* Code snippets should be added to the functions.php file of your child theme
* (Updated for WooCommerce 3.5)
*
* @return string
*/
add_filter( 'woocommerce_cart_shipping_method_full_label', function( $label, $method ) {
// only update for Free shipping methods
$has_cost = 0 < $method->cost;
@bolderelements
bolderelements / betrs-hide-option-based-on-other-option.php
Created June 25, 2020 18:38
Hide One Table Rate Option When Another is Available
/**
* Hide a specific Table Rate option when the given shipping option is available to choose
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function hide_shipping_when_option_is_available( $rates, $package ) {
// setup the option IDs
$option_needed = 'betrs_shipping:8-1';
@bolderelements
bolderelements / betrs-add-stock-status-condition.php
Last active April 10, 2020 15:32
Add new condition to Table Rate settings page Reviewing Stock Status
/**
* Add ability to find and compare the stock status of items in Table Rate shipping
* Intended for Use with Per Order setups and Method Conditions
* https://codecanyon.net/item/table-rate-shipping-for-woocommerce/3796656?ref=bolderelements
*
* Code should be added to theme's functions.php file
*/
function betrs_add_stock_status_cond( $conditions ) {
// add new option to list
$conditions['stock_status'] = 'Stock Status';
@bolderelements
bolderelements / betrs-add-longest-length-cost-type.php
Created October 28, 2019 14:00
Add Custom Condition 'Longest Length' to Cost Types
/**
* Add the longest length to Cost types in Table Rate shipping
* Longest Length is a custom condition added in tutorial:
* https://www.bolderelements.net/support/knowledgebase/adding-new-condition/
*
* Code should be added to theme's functions.php file
*/
function betrs_modify_cost_every( $cost_units_every ) {
// add new condition's value
@bolderelements
bolderelements / betrs-add-longest-length-condition-per-class.php
Last active October 15, 2019 21:11
Add Longest Length Condition's Calculation for Per Class Setups
function betrs_add_longest_length_calc_class( $data, $items ) {
$item_lengths = array();
// cycle through cart items
foreach( $items as $item_ar ) {
// only count the ones that apply to shipping
if( isset( $item_ar['data'] ) && $item_ar['data']->needs_shipping() ) {
// initialize necessary variables
$item = $item_ar['data'];
@bolderelements
bolderelements / betrs-modify-order-length.php
Created August 28, 2019 02:22
Modify the Order Length for Table Rate Shipping Settings
/*
* Recalculate the order's length to ignore multiple quantities.
*
* Requires Table Rate Shipping 4.2+
*/
function betrs_modify_calculated_length( $calculated_totals, $items ){
// ensure there are items in the cart
if( is_array( $items ) ) {
$length = 0;
@bolderelements
bolderelements / add-img-tag-wp_kses_data.php
Last active October 24, 2018 15:34
Add <img> tag to WordPress data elements
/*
* Adds the <img> tag to allowed tags in fields sanitized using the wp_kses_data function.
*
* This particularly helps with adding images to Compare Table Features for individual products
*/
add_filter( 'wp_kses_allowed_html', 'my_kses_allowed_html_hook', 23, 2 );
function my_kses_allowed_html_hook( $allowed_tags, $context = null ){
if ( 'post' == $context && ! isset( $allowed_tags['img'] ) ) {
$allowed_tags['img'] = array(
'alt' => true,
@bolderelements
bolderelements / betrs-adjust-product-qty.php
Created October 3, 2018 02:52
Change a Product's True Quantity for Calculating Shipping
/**
* Adjust the calculated quantity of items in the cart based on a specific product
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function adjust_item_qty_for_shipping( $data, $items ) {
// the Product ID of the product that needs to be adjusted
$item_to_change = 13;
@bolderelements
bolderelements / wc-hide-shipping-option-title-based.php
Last active August 31, 2018 02:40
Hide a Shipping Option When Another Option is Available (Title Based)
/**
* Hide a shipping option when the given shipping option is in the cart based on their titles
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function hide_shipping_when_option_is_available( $rates ) {
// shipping option titles to be removed and the requirement, respectively
$option_removed = 'Super Saver Shipping';
$option_needed = 'Free Super Saver Shipping';