This topic contains a post which is marked as Best Answer. Press here if you would like to see it.
*

Tango

  • ****
  • 214 posts
Struggling with some plugin code
« on: December 08, 2021, 04:45:10 PM »
Hello,

I'm struggling to modify a plugin that adds an alternative price field to the item-post / item-edit pages.
In it's current form, the plugin always shows the field regardless of the category selected, but I want to change it to be category-dependant.

By looking at the plugin dev structure here, I've managed to hook the plugin to categories (t_plugin_category table), but I can't make the field show/hide based on the category selection...

Here is the plugin code that handles the field:
Code: [Select]
function osc_set_mrp_number() {
    $detail = '';
    if (osc_is_publish_page()) {
        if (Session::newInstance()->_getForm('mrp') != '') {
            $detail = Session::newInstance()->_getForm('mrp');
        }
    } else if (osc_is_edit_page()) {
        if (Session::newInstance()->_getForm('mrp') != '') {
            $detail = Session::newInstance()->_getForm('mrp');
        } else {
            $value = Modeldiscount::newInstance()->t_check_value(osc_item_id());
            if (!empty($value)) {
                $detail = $value['s_mrp'];
            }
        }
    }
    ?>
    <div class="form-group js-price">
        <label class="col-sm-3 control-label" for="price"><?php _e('Market Price''mrp'); ?></label>
        <div class="col-sm-3">
            <input id="mrp" type="text" value="<?php echo osc_esc_html($detail); ?>" name="mrp"></input>
        </div>
        <div class="col-sm-3">
            <?php ItemForm::currency_select(); ?>
        </div>
    </div>
    <?php
}

function 
pre_post_store_value() {
    
Session::newInstance()->_setForm('mrp'Params::getParam("mrp"));
    
Session::newInstance()->_keepForm('mrp');
}

osc_add_hook('pre_item_post''pre_post_store_value');

The plugin's slug is mrp_price, so I tried to add a category check, but this hides the field completely and doesn't show it when selecting the correct category:
Code: [Select]
if (osc_is_this_category('mrp_price', osc_item_category_id())) {
    if ( Session::newInstance()->_getForm('mrp') != '' ) {
        $detail['mrp'] = Session::newInstance()->_getForm('mrp');
    }
}

What else could I try, as this is driving me crazy? :(

Thanks!

*

MB Themes

Re: Struggling with some plugin code
« Reply #1 on: December 08, 2021, 05:05:46 PM »
Do you show that field via item_form hook?
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Tango

  • ****
  • 214 posts
Re: Struggling with some plugin code
« Reply #2 on: December 08, 2021, 05:11:02 PM »
I'm showing it in forms, using this function:
Code: [Select]
<?php if(function_exists('osc_set_mrp_number')) { osc_set_mrp_number(); } ?>
« Last Edit: December 08, 2021, 07:09:10 PM by Tango »

*

MB Themes

Re: Struggling with some plugin code
« Reply #3 on: December 09, 2021, 09:43:36 AM »
@Tango
But if it is not loaded via hooks, then if you change category, nothing will happen, as only hooked functions will get refreshed.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

Marked as best answer by frosticek on December 09, 2021, 03:16:34 PM
*

Tango

  • ****
  • 214 posts
Re: Struggling with some plugin code
« Reply #4 on: December 09, 2021, 02:52:29 PM »
Yeah, found a hackier but better way to do it.

I've attached my plugin to the Osclass Pay, with:
Code: [Select]
if(function_exists('osp_item_sell_form')) {
  $sellers = explode(',', osp_param('seller_users'));
  if(function_exists('osc_set_mrp_number') && osp_param('selling_allow') == 1 && in_array(osc_logged_user_id(), $sellers) && osc_is_web_user_logged_in()) {
     osc_set_mrp_number();
  }
}

So my field will show only to Sellers! ;D

Thanks for the support!
« Last Edit: December 09, 2021, 03:00:02 PM by Tango »

*

MB Themes

Re: Struggling with some plugin code
« Reply #5 on: December 09, 2021, 03:16:29 PM »
 :-*
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots