1
0
mirror of synced 2025-03-26 10:03:53 +03:00

Add filters for custom fields

This commit is contained in:
Dima Uryvskiy 2022-03-22 15:29:39 +03:00 committed by GitHub
parent 81dc5071dd
commit 96fc850a40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -147,9 +147,14 @@ if (!class_exists('WC_Retailcrm_History')) :
if ($wcCustomer instanceof WC_Customer) { if ($wcCustomer instanceof WC_Customer) {
$wcCustomer->save(); $wcCustomer->save();
$customerCustomFields = $this->getCustomData('customer'); $customFields = apply_filters(
'retailcrm_process_customer_custom_fields',
$this->getCustomData('customer'),
$crmCustomer,
$wcCustomer
);
$this->updateMetaData($customerCustomFields, $crmCustomer, $wcCustomer->get_id()); $this->updateMetaData($customFields, $crmCustomer, $wcCustomer);
} }
WC_Retailcrm_Logger::debug(__METHOD__, array('Updated WC_Customer:', $wcCustomer)); WC_Retailcrm_Logger::debug(__METHOD__, array('Updated WC_Customer:', $wcCustomer));
@ -227,13 +232,19 @@ if (!class_exists('WC_Retailcrm_History')) :
$wcOrderId = $this->orderCreate($order, $options); $wcOrderId = $this->orderCreate($order, $options);
} }
$wcOrder = wc_get_order($wcOrderId); $wcOrder = wc_get_order($wcOrderId);
$orderCustomFields = $this->getCustomData('order');
$this->updateMetaData($orderCustomFields, $order, $wcOrderId, 'order');
if ($wcOrder instanceof WC_Order) { if ($wcOrder instanceof WC_Order) {
$wcOrder->calculate_totals(); $wcOrder->calculate_totals();
$customFields = apply_filters(
'retailcrm_process_order_custom_fields',
$this->getCustomData('order'),
$order,
$wcOrder
);
$this->updateMetaData($customFields, $order, $wcOrder);
} }
$wcOrderNumber = $wcOrder->get_order_number(); $wcOrderNumber = $wcOrder->get_order_number();
@ -1106,23 +1117,26 @@ if (!class_exists('WC_Retailcrm_History')) :
* Update meta data in CMS. * Update meta data in CMS.
* *
* @param array $customFields Custom fields witch settings mapping. * @param array $customFields Custom fields witch settings mapping.
* @param array $data Data witch CRM. * @param array $crmData Data witch CRM.
* @param int $idPost ID post in which we update the metadata. * @param object $wcObject Object witch WC.
* @param string $dataType Data type which to update the metadata.
* *
* @return void * @return void
*/ */
private function updateMetaData($customFields, $data, $idPost, $dataType = 'customer') private function updateMetaData($customFields, $crmData, $wcObject)
{ {
if (!empty($customFields)) { if (empty($customFields)) {
foreach ($customFields as $metaKey => $customKey) { return;
if (!empty($data['customFields'][$customKey])) { }
if ($dataType === 'order') {
update_post_meta($idPost, $metaKey, $data['customFields'][$customKey]); foreach ($customFields as $metaKey => $customKey) {
} else { if (empty($crmData['customFields'][$customKey])) {
update_user_meta($idPost, $metaKey, $data['customFields'][$customKey]); continue;
} }
}
if ($wcObject instanceof WC_Order) {
update_post_meta($wcObject->get_id(), $metaKey, $crmData['customFields'][$customKey]);
} else {
update_user_meta($wcObject->get_id(), $metaKey, $crmData['customFields'][$customKey]);
} }
} }
} }