Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Some meta data should not be copied to subscriptions or renewal orders.
*/
namespace UniversalYums\Subscriptions;
class PreventMetaCopy {
/**
* The single instance of the class.
*/
protected static $instance;
/**
* Prevents these meta keys from copying to subscriptions.
* Ordinarily these would copy over from the parent order.
*
* The Metorik meta does not need to copy to the subscription,
* it should only apply to the parent order.
*/
protected static $subscription_meta = array(
'_metorik_source_type',
'_metorik_referer',
'_metorik_utm_source',
'_metorik_utm_medium',
'_metorik_session_entry',
'_metorik_session_start_time',
'_metorik_session_pages',
'_metorik_session_count',
'_metorik_cart_token',
'_metorik_utm_campaign',
'_metorik_utm_content',
'_metorik_utm_term',
);
/**
* Prevents these meta keys from copying to renewal orders.
* Ordinarily these would copy over from the subscription order.
*
* User agent and IP address are just artifacts from the original order.
* We're keeping them on the subscription order for now, but may remove later.
*/
protected static $order_meta = array(
'_customer_user_agent',
'_customer_ip_address',
'gift_message',
'gift_message_to',
'gift_message_from',
'shareasale-wc-tracker-triggered',
);
/**
* Meta to remove when order gets status competed.
*/
protected static $completed_order_meta = array(
'shareasale-wc-tracker-triggered',
);
/**
* Main PreventMetaCopy Instance.
*
* Ensures only one instance of the PreventMetaCopy is loaded or can be loaded.
* To load the instance: UniversalYums\Subscriptions\PreventMetaCopy::instance();
*
* @return PreventMetaCopy - Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
// This removes the meta from the subscription.
add_filter( 'wcs_subscription_meta', array( $this, 'prevent_subscription_meta_copy' ) );
// This removes the meta from renewal orders.
add_filter( 'wcs_renewal_order_meta', array( $this, 'prevent_renewal_meta_copy' ) );
// When order status is completed, remove some meta.
add_action( 'woocommerce_order_status_completed', array( $this, 'remove_meta_on_order_status_completed' ) );
}
/**
* Delete post meta from the order once the order is completed.
*
* @param $order_id
*/
public function remove_meta_on_order_status_completed( $order_id ) {
foreach ( self::$completed_order_meta as $meta_key ) {
delete_post_meta( $order_id, $meta_key );
}
}
/**
* Prevent meta data from copying to subscriptions.
*
* @param $meta
*
* @return mixed
*/
public function prevent_subscription_meta_copy( $meta ) {
return self::filter_meta( $meta, self::$subscription_meta );
}
/**
* Prevent meta data from copying to renewals.
*
* @param $meta
*
* @return mixed
*/
public function prevent_renewal_meta_copy( $meta ) {
return self::filter_meta( $meta, self::$order_meta );
}
/**
* Remove meta with given meta keys from the array.
*
* @param $meta
* @param $meta_to_remove
*
* @return mixed
*/
public static function filter_meta( $meta, $meta_to_remove ) {
foreach ( $meta_to_remove as $meta_key_to_remove ) {
$key = array_search( $meta_key_to_remove, wp_list_pluck( $meta, 'meta_key' ), true );
if ( false !== $key ) {
unset( $meta[ $key ] );
}
}
return $meta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment