Skip to content

Instantly share code, notes, and snippets.

@bernattorras
bernattorras / save_trial_end_meta.php
Created July 5, 2018 17:11
Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
<?php
/**
* Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
**/
add_action('woocommerce_subscription_pre_update_status', 'save_trial_end_meta', 10, 3);
function save_trial_end_meta($old_status, $new_status, $sub){
if(in_array($new_status, array('cancelled', 'pending-cancel', 'switched', 'expired'))){
$sub_id = $sub->get_id();
$trial_end_date = $sub->get_date('trial_end');
@bernattorras
bernattorras / woocommerce_subscription_pre_update_status.php
Created July 5, 2018 17:12
Function to run some code when (before) the status of a subscription changes
<?php
/**
* Function to run some code when (before) the status of a subscription changes
**/
add_action('woocommerce_subscription_pre_update_status', 'order_satus_changed', 100, 3);
function order_satus_changed($from, $to, $order ){
// Run any code here
}
@bernattorras
bernattorras / custom_add_product_to_cart.php
Created July 5, 2018 17:14
Function to add a product to clear the cart and add a specific product when a certain page is loaded
<?php
/**
* Function to add a product to clear the cart and add a specific product when a certain page is loaded
**/
add_action( 'template_redirect', 'custom_add_product_to_cart' );
function custom_add_product_to_cart() {
if(is_page(124)){
$product_id = 1098;
WC()->cart->empty_cart();
@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(
@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 / 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 / remove_switch_link.php
Created November 21, 2018 10:05
Remove the subscription switch link if the subscription contains a specific product
@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 ) );
}
<?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 / 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 );