Skip to content

Commit

Permalink
fix: remove accepts marketing metadata fields on subsequent orders (#54)
Browse files Browse the repository at this point in the history
* build: add debug script

* fix: remove custom metadata fields on subsequent orders

fix: do not access id directly for non hpos

* fix: only remove accepts marketing field

* bump versioning

update changelog
  • Loading branch information
cshcsh3 authored Jul 19, 2024
1 parent 7b9642a commit 1f05b3e
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 489 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ wp-content/cache/
wp-content/backups/
sitemap.xml
sitemap.xml.gz
node_modules
node_modules
.vscode
24 changes: 22 additions & 2 deletions includes/class-wc-referralcandy-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,30 @@ public function render_accepts_marketing_field()
}
}

private function remove_accepts_marketing_metadata($order, $type)
{
$order_meta_data = $order->get_meta_data();
$meta_keys_to_remove = ['rc_accepts_marketing'];

foreach ($order_meta_data as $meta_data) {
if (in_array($meta_data->key, $meta_keys_to_remove)) {
if ($type == 'post') {
delete_post_meta($order->get_id(), $meta_data->key);
} else if ($type == 'order') {
$order->delete_meta_data($meta_data->key);
}
}
}
}

public function update_order_meta($order)
{
$checkout_fields = Package::container()->get( CheckoutFields::class );
$rc_accepts_marketing_field = $checkout_fields->get_field_from_object($this->accepts_marketing_field_id, $order);

if (OrderUtil::custom_orders_table_usage_is_enabled()) {
// If order in param contains accepts marketing metadata field, remove them, its presence indicates truthy
$this->remove_accepts_marketing_metadata($order, 'order');
if (!empty($rc_accepts_marketing_field)) {
$order->update_meta_data('rc_accepts_marketing', $rc_accepts_marketing_field);
}
Expand All @@ -194,10 +213,11 @@ public function update_order_meta($order)
}
}
$order->save();

} else {
// If order in param contains accepts marketing metadata field, remove them, its presence indicates truthy
$this->remove_accepts_marketing_metadata($order, 'post');
if (!empty($rc_accepts_marketing_field)) {
update_post_meta($order->id, 'rc_accepts_marketing', $rc_accepts_marketing_field);
update_post_meta($order->get_id(), 'rc_accepts_marketing', $rc_accepts_marketing_field);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"scripts": {
"start": "wp-env start",
"start:xdebug": "wp-env start --xdebug",
"stop": "wp-env stop",
"destroy": "wp-env destroy"
},
Expand Down
Loading

0 comments on commit 1f05b3e

Please sign in to comment.