License Manager for WooCommerce

Easily sell and manage software license keys through your shop

lmfwc_update_license_meta()

function lmfwc_update_license_meta(int $licenseId, string $metaKey, mixed $metaValue, mixed $previousValue = null)

Description

Updates a license meta field based on the given license ID. Use the $prev_value parameter to differentiate between meta fields with the same key and license ID. If the meta field for the license does not exist, it will be added and its value returned. Can be used in place of lmfwc_add_license_meta().

Parameters

$licenseId

(int) (Required) License ID.

$metaKey

(string) (Required) Metadata name.

$metaValue

(mixed) (Required) Metadata value. Must be serializable if non-scalar.

$previousValue

(mixed) (Optional) Previous value to check before updating.

Default value: null

Return value

(bool) True on success, false on failure.

Source

File: /includes/functions/lmfwc-meta-functions.php

/**
 * Updates existing license meta entries.
 *
 * @param int    $licenseId
 * @param string $metaKey
 * @param mixed  $metaValue
 * @param mixed  $previousValue
 *
 * @return bool
 */
function lmfwc_update_license_meta($licenseId, $metaKey, $metaValue, $previousValue = null)
{
    $license = LicenseResourceRepository::instance()->find($licenseId);

    if (!$license) {
        return false;
    }

    $selectQuery = array(
        'license_id' => $licenseId,
        'meta_key'   => $metaKey
    );
    $updateQueryCondition = array(
        'license_id' => $licenseId,
        'meta_key'   => $metaKey
    );
    $updateQueryData = array(
        'license_id' => $licenseId,
        'meta_key'   => $metaKey,
        'meta_value' => maybe_serialize($metaValue)
    );

    if ($previousValue !== null) {
        $selectQuery['meta_value'] = $previousValue;
        $updateQueryCondition['meta_value'] = $previousValue;
    }

    $metaLicense = LicenseMetaResourceRepository::instance()->findBy($selectQuery);

    if (!$metaLicense) {
        return false;
    }

    $updateCount = LicenseMetaResourceRepository::instance()->updateBy($updateQueryCondition, $updateQueryData);

    if (!$updateCount) {
        return false;
    }

    return true;
}