Skip to content

Instantly share code, notes, and snippets.

@bernattorras
bernattorras / extend_sub_intervals.php
Created November 12, 2019 09:52
Extend the subscription period intervals to a custom number
<?php
// Extend the subscription period intervals to a custom number
function eg_extend_subscription_period_intervals( $intervals ) {
$max_intervals = 30; // The maximum number of intervals you want to display
$intervals = array( 1 => _x( 'every', 'period interval (eg "$10 _every_ 2 weeks")', 'woocommerce-subscriptions' ) );
foreach ( range( 2, $max_intervals ) as $i ) {
$intervals[ $i ] = sprintf( _x( 'every %s', 'period interval with ordinal number (e.g. "every 2nd"', 'woocommerce-subscriptions' ), WC_Subscriptions::append_numeral_suffix( $i ) );
}
return $intervals;
}
@bernattorras
bernattorras / log_sub_changes.php
Last active November 22, 2021 01:49
A custom function to log any subscription status changes.
<?php
// Log Subscription status changes
add_filter( 'woocommerce_subscription_status_updated', 'log_sub_status_changes', 10, 3 );
function log_sub_status_changes( $subscription, $new_status, $old_status ) {
$logger = new WC_Logger();
$subscription_id = $subscription->get_id();
$sub_user = $subscription->get_user_id();
switch ( $new_status ) {
@bernattorras
bernattorras / clear_cart_when_loading_page.php
Created July 17, 2019 12:51
Clears the cart when a specific page is loaded (it can also add a product to the cart automatically)
<?php
add_action( 'template_redirect', 'clear_cart_when_loading_page' );
function clear_cart_when_loading_page() {
$page_id = 124;
if ( is_page( $page_id ) ) {
WC()->cart->empty_cart();
// Uncomment the next lines if you want to add a product to the cart automatically
// $product_id = 1098;
// WC()->cart->add_to_cart( $product_id );
@bernattorras
bernattorras / enable_eu_vat_field_opc.php
Created May 15, 2019 09:29
Enable EU VAT field when the current page contains a OPC form and the cart is empty
<?php
// Make sure that EU VAT Field is displayed on OPC pages (when the cart is empty)
function enable_eu_vat_field_opc_defualt( $default, $option, $passed_default ) {
if ( is_wcopc_checkout() ) {
return 'yes';
}
return $default;
}
add_filter( 'default_option_woocommerce_eu_vat_number_b2b', 'enable_eu_vat_field_opc_defualt', 11, 3 );
<?php
// Check the "shipping to different address" checkbox on the Checkout page if the cart contains a gifted item
add_filter('woocommerce_ship_to_different_address_checked','check_shipping_if_gifting', 11, 1);
function check_shipping_if_gifting($val){
foreach ( WC()->cart->cart_contents as $key => $item ) {
if ( '' !== WCSG_Cart::get_recipient_from_cart_item( $item ) ) {
return true;
}
}
return $val;
@bernattorras
bernattorras / add_extra_period_intervals.php
Created November 29, 2018 12:54
Add extra intervals to the subscription periods (WCS and WCSATT)
<?php
add_filter('woocommerce_subscription_period_interval_strings', 'add_extra_period_intervals', 10, 1);
function add_extra_period_intervals($intevals){
$max_intervals = 10; // The maximum number of intervals you want to display
$intervals = array( 1 => _x( 'every', 'period interval (eg "$10 _every_ 2 weeks")', 'woocommerce-subscriptions' ) );
foreach ( range( 2, $max_intervals ) as $i ) {
$intervals[ $i ] = sprintf( _x( 'every %s', 'period interval with ordinal number (e.g. "every 2nd"', 'woocommerce-subscriptions' ), WC_Subscriptions::append_numeral_suffix( $i ) );
}
@bernattorras
bernattorras / remove_switch_link.php
Created November 21, 2018 10:05
Remove the subscription switch link if the subscription contains a specific product
@bernattorras
bernattorras / wcopc_custom_input_qtty.php
Created September 13, 2018 14:10
A functionality to be able to define the default product units of each product included in the "woocommerce_one_page_checkout" shortcode
<?php
/**
* Parse an adititonal "product_qtty" attribute provided in the "woocommerce_one_page_checkout" shortcode to specify the default number of units that should be used in each product quantity field
*
*
* Example: [woocommerce_one_page_checkout template="pricing-table" product_ids="451,10,70" product_qtty="1,2,3"]
* Instructions: Add an additional shortcode attribute named "product_qtty" with the default units that each product will display in its quantity input. The values must be separated by commas (",") and must match the order of the products specified in the "product_ids" attribute (the first number will be the number of units of the first product, and so on)
* Note: This attribute is used to specify the default value of each product quantity field. It DOESN'T add these products to the cart automatically. The customer must do it instead.
*/
@bernattorras
bernattorras / change_sat_option_strings.php
Last active March 23, 2021 05:46
Change the strings of the options generated by Subscribe All The Things plugin
<?php
// Customize the default "one time option" string
add_filter('wcsatt_single_product_one_time_option_description', 'change_sat_one_time_option_description', 10, 6);
function change_sat_one_time_option_description($none_string, $product){
// This line of code removes all modifications that Subscribe All The Things plugin makes on the product price string (like adding "or subscribe and get...")
WCS_ATT_Product_Price_Filters::remove( 'price_html' );
$price = $product->get_price_html();
// Return the default string + the price of the product
@bernattorras
bernattorras / wc_get_customer_renewal_orders_by_status.php
Last active August 2, 2018 10:01
Function to get all the renewal orders with a specific status of a customer
<?php
function wc_get_customer_renewal_orders_by_status($customer_id, $status) {
$args = array(
'post_type' => 'shop_order',
'post_status' => $status,
'order' => 'ASC',
'meta_query' => array(
array(