• Resolved yueprofile

    (@yueprofile)


    Hi there

    Im trying to update productId by code after the license was created, from my own plugin.

    I tried the follow method but no luck:

    ====================================================

    use LicenseManagerForWooCommerce\Repositories\Resources\License as LicenseResourceRepository;

    defined(‘ABSPATH’) || exit;

    function example()
    {
    $product_id = 11;
    $license_id = 89;
    $updateData = array(“productId” => $product_id);
    $updatedLicense = LicenseResourceRepository::instance()->update($license_id, $updateData);
    }
    ====================================================

    Any hint? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @yueprofile

    thank you for your message and for using my plugin.

    try the following:

    $updateData = array("product_id" => $product_id);

    the array keys need to correspond with the database column names.

    Thread Starter yueprofile

    (@yueprofile)

    Hi Drazen, it works! Thanks so much.

    While working with this I found the filter for rest api is a bit clunky if you want to add multiple filters to it.

    In license-manager-for-woocommerce/includes/api/Setup.php

    public function preResponse( $method, $route, $data)
    {
    return $data;
    }

    The result of the first round of execution will be passed into the first argument of the next execution of filter. So in the next execution, the $data from the last execution were pass into the $method variable, resulting next execution have 2 $data argument, and the original value of $method is lost.

    Not sure if its perfect, but my solution is to exchange position of the arguments
    so that the first argument is data, and other arguments will be preserved correctly in multiple filter executions

    public function preResponse( $data, $route, $method)
    {
    return $data;
    }

    Then in license-manager-for-woocommerce/includes/abstracts/RestController.php
    protected function response($success, $data, $code = 200, $route)
    {
    return new WP_REST_Response(
    array(
    ‘success’ => $success,
    ‘data’ => apply_filters(‘lmfwc_rest_api_pre_response’, $data, $route, $_SERVER[‘REQUEST_METHOD’])
    ),
    $code
    );
    }

    But since I’m not super familiar with WordPress, you might need to investigate a bit more to see if there is unintended bugs.

    Thread Starter yueprofile

    (@yueprofile)

    I have another question regarding the programmatically update a product to sell license keys

    I figure I will have to update post meta of
    lmfwc_licensed_product
    lmfwc_licensed_product_delivered_quantity
    lmfwc_licensed_product_assigned_generator
    lmfwc_licensed_product_use_generator
    lmfwc_licensed_product_use_stock

    Is there any other things need to be updated?
    And if there is a small code example would be perfect, thanks!

    @yueprofile

    You’re right about the filter, I’ll have to check on that.

    And the meta keys you have listed are spot on, those are used by the plugin.

    Hello @yueprofile

    Do you still need help with this or were you able to figure it out?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Update License property by code’ is closed to new replies.