WooCommerce Memberships
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
class WOE_Memberships { var $fields = array("plan","status","since","expires"); function __construct() { add_filter('woe_get_order_fields', function ($fields) { foreach($this->fields as $f) { $fields['membership_'.$f] = array( 'label' => "Member $f", 'colname' => "Member $f", 'checked' => 1 ); } return $fields; }); add_filter('woe_settings_validate_defaults', function ($settings) { foreach($this->fields as $f) { add_filter('woe_get_order_value_membership_'.$f, function ($value,$order,$field) { return isset($this->data[$field]) ? $this->data[$field] : $value; },10,3); } return $settings; }); // rebuild for each order add_filter('woe_order_export_started', function ($order_id) { // each order can create many memberships! $this->data = array(); foreach($this->fields as $f) $this->data[$f] = array(); //gather details $order = wc_get_order($order_id); $user_id = $order->get_user_id(); $memberships = wc_memberships_get_user_active_memberships( $user_id ); foreach ($memberships as $m) { $this->data['plan'][] = $m->get_plan()->get_name(); $this->data['status'][] = $m->get_status(); $this->data['since'][] = $m->get_start_date(); $this->data['expires'][] = $m->get_end_date(); } // convert to multiline cells foreach($this->data as $f=>$v) $this->data['membership_'.$f] = join("\n", $v ); return $order_id; }); } } new WOE_Memberships(); |