Create plugin to add more field to restriction section of product

  • Posts: 168
  • Thank you received: 7
1 year 2 months ago #348422

hi
i need to add some restriction to purchase a product. for example if he do not complete some part of a course in other website out of joomla context, then he can not purchase this product. simply i need to add some additional dropdown field to render all part of each course and select multiple section of that course. i need to use php to user curl and api to ask from other site to verify this user complete or not some part of that course. can you advise me?
thank you.
if it is possible create some new field through hika Custom Field , it is enough for me, but i can not see can use php in new custom field and it seems only can use MySQL query tab in new field. also i need show my additional field in somewhere for admin or vendor for example RESTRICTIONS AND DIMENSIONS.
thank you again.

Please Log in or Create an account to join the conversation.

  • Posts: 81423
  • Thank you received: 13057
  • MODERATOR
1 year 2 months ago #348423

Hi,

I think the best would be to create a plugin of the group "hikashop" and implement two events:
- First, the event onAfterCheckCartQuantities(&$cart, &$parent_products, &$ret) is called when the cart is being loaded. In it, you can loop on the products array in $cart->products and set the cart_product_quantity to 0 for a product if you want to remove it from the cart. You can also add an entry to $cart->messages to set an error message to be displayed to the user like this:

$cart->messages[] = array('msg' => 'product not purchasable yet', 'product_id' => $product_id, 'type' => 'notice');
This will make sure that whenever a cart is loaded, the system will check that the user can purchase all the products in the cart and if not, remove it. It can be useful if you add a rule later on and users already have old carts with products in it they should not be allowed to purchase anymore.

- Second, the event onBeforeProductQuantityCheck(&$products, &$cart, &$options) is called when a product is being added to the cart. In the $products array you can set the 'qty' array entry to 0 for each sub array to cancel the add to cart for the corresponding product (several products can be added at once in some cases). And you can use the code below to set the error message:
$cartClass = hikashop_get('class.cart');
$cartClass->addMessage($cart, array('msg' => 'product not purchasable yet', 'product_id' => $product_id, 'type' => 'error'));
This will display a nice message to the customer when he tries to add something to the cart and he doesn't have access to it.

The following user(s) said Thank You: sadaf3d

Please Log in or Create an account to join the conversation.

  • Posts: 168
  • Thank you received: 7
1 year 2 months ago #348462

thank you for completely reply.
is it possible advise about how to create additional customized php calculator field in RESTRICTIONS AND DIMENSIONS.
i want give something via api from other website and show it in dropdown.my field need search feature like that you have in payment method plugin to ajax search in product list. and is there any sample plugin?

Last edit: 1 year 2 months ago by sadaf3d.

Please Log in or Create an account to join the conversation.

  • Posts: 81423
  • Thank you received: 13057
  • MODERATOR
1 year 2 months ago #348464

Hi,

You can do something like that to display a search field like that:

			$nameboxType = hikashop_get('type.namebox');
			if(!is_array($value))
				$value = explode(',', trim($value));
			echo $nameboxType->display(
				$map,
				$value,
				hikashopNameboxType::NAMEBOX_MULTIPLE,
				'rawlist',
				array(
					'delete' => true,
					'sort' => true,
					'default_text' => '<em>'.JText::_('HIKA_NONE').'</em>',
					'rawdata' => $values
				)
			);
$map contains the "name" used to send the selected elements in the POST when the form is submitted
$value contains the current value (several values separated by a comma
$values contains an array of values to be selected along with their keys (the keys will be saved in the database, the values will be displayed to the user)

You could add that in product / form for your backend template via the menu Display>Views. You'll want to have in $map something like that:
data[product][xxx]
where xxx is the column of the hikashop_product table where you want the data to be stored.

Please Log in or Create an account to join the conversation.

  • Posts: 168
  • Thank you received: 7
1 year 2 months ago #348558

nicolas wrote: Hi,



You could add that in product / form for your backend template via the menu Display>Views. You'll want to have in $map something like that:

data[product][xxx]

nicolas wrote: where xxx is the column of the hikashop_product table where you want the data to be stored.

thank you nicolas.
is it possible implement this piece of code in plugin structure?
and excuse me for more toil. i have some course that has some activity in that course. it meens nested structure. and figure out if i have more than 1000 course and each of them has some activity in that course, is it posibble load step by step means in the first step load course title and when click on the course title in second step load all activity in that course. by this way it is loaded less dom element in the first step. or can you say me it is better or not to create related drop down fields , when user select in the first field then the second field load activity of the selected course in the first field.

Last edit: 1 year 2 months ago by sadaf3d.

Please Log in or Create an account to join the conversation.

  • Posts: 81423
  • Thank you received: 13057
  • MODERATOR
1 year 2 months ago #348561

Hi,

Yes. To add that code to the product edit form from a plugin, you want to implement the event onProductFormDisplay(&$product, &$html) and you can add your HTML as a new entry in $html.
Note however that in that case, you can't choose the location of your HTML. It will be in the "Fields" section, after the custom product fields. That's why I was recommending a view override sine you wanted to place it elsewhere in the interface.

To load the search field dynamically, like we have in some places, it's possible, but much more complex.
First, you need to implement the event onNameboxTypesLoad(&$loaded_types) to add your own type of search field. That way, instead of "rawlist" you can use that type.
Here is an example I made for a custom development to get a search field to select users:

	function onNameboxTypesLoad(&$loaded_types)
	{
		$loaded_types['client_selector'] = array(
			'class' => 'class.user',
			'name' => 'user_id',
			'mode' => 'list',
			'displayFormat' => '{user_id} - {name}',
			'url' => 'xxx&task=getValues',
			'options' => array(
				'olist' => array(
					'table' => array('user_id' => 'ID', 'name' => 'HIKA_NAME', 'user_email' => 'HIKA_EMAIL'),
					'displayFormat' => '{user_id} - {name}',
				)
			)
		);
	}
where xxx is the folder name of the plugin.
Then, you also need to implement the event onHikashopPluginController($ctrl) in order to add your own controller to HikaShop, like so:
	public function onHikashopPluginController($ctrl)
	{
		if (!in_array($ctrl, array('xxx')))
			return;
		$prefix = 'ctrl';

		if (!hikashop_isClient('administrator'))
			return;

		// register the controller for the client selector
		return array(
			'type' => 'hikashop',
			'name' => 'xxx',
			'prefix' => $prefix,
		);
	}
And then you need a file xxx_ctrl.php in your plugin. In that file, you need code like this to define the controller and the task getValues:
<?php


/**
 *
 */
class  xxxController extends hikashopController {
	public $display = array('getValues');
	public $modify_views = array();
	public $add = array();
	public $modify = array();
	public $delete = array();

	public $pluginCtrl = array('hikashop', 'xxx');
	public $type = 'plg_xxx';

	/**
	 *
	 * @param array $config
	 * @param boolean $skip
	 */
	public function __construct($config = array(), $skip = false) {
		parent::__construct($config, $skip);
		if(!$skip)
			$this->registerDefaultTask('listing');
	}

	/**
	 *
	 */
	protected function getACLName($task) {
		return '';
	}
	/**
	 * user data feeder for the client selector
	 */
	public function getValues() {
		$displayFormat = hikaInput::get()->getVar('displayFormat', '');
		$search = hikaInput::get()->getVar('search', null);
		$start = hikaInput::get()->getInt('start', 0);

		$nameboxType = hikashop_get('type.namebox');
		$options = array(
			'start' => $start,
			'displayFormat' => $displayFormat
		);
		$ret = $nameboxType->getValues($search, 'user', $options);
		if(!empty($ret)) {
			echo json_encode($ret);
			exit;
		}
		echo '[]';
		exit;
	}
}
The line:
$ret = $nameboxType->getValues($search, 'user', $options);
will return the data of the users necessary from the getValues function of administrator/components/com_hikashop/types/namebox.php
This function actually uses the getNameboxData function of administrator/components/com_hikashop/classes/user.php
So you'll have to adapt all this to your needs. You're in for at least several hours of work.
I think it's quite complex for a marginal gain. I would recommend you to stick with the code I gave in my previous message. Loading a few thousand elements in a Javascript array is not a problem.

The following user(s) said Thank You: sadaf3d

Please Log in or Create an account to join the conversation.

  • Posts: 168
  • Thank you received: 7
11 months 6 days ago #351572

hi after almost 3 month
i create a plugin to add field in the restriction but i have two problem:
1- when i remove the options from our example field in the product restriction, it can not set a default option. for example i select some option from dropdown of that field after plugin install and the create manually a field with the name "moojla_courses" with my own type. how to resolve this problem? if you don't understand my mean please see the video:
app.screencast.com/E6HTJzVDbc0mT
2- how to use return in moojla_course_completed_restriction_class.php line:83 ?
we use echo in line70 . the return not work according that you, is it possible advise me?
please see and install the attached extension. thank you
this plugin want to restrict buy a product until he complete some course.

Attachments:
Last edit: 11 months 6 days ago by sadaf3d.

Please Log in or Create an account to join the conversation.

  • Posts: 81423
  • Thank you received: 13057
  • MODERATOR
11 months 6 days ago #351574

Hi,

1. I'm not sure why it does that and doesn't remove the last element selected in the field. However, I thing which could be problematic is the __construct function you have in your code. You don't call the parent class function. Because of that it's missing several pieces and the field won't work properly in some cases. So it might be linked. Please either remove the __construct function in your code or call the parent class __construct in it.

2. Change the "echo" on line 70 to "return" and that's it.

Last edit: 11 months 6 days ago by nicolas.

Please Log in or Create an account to join the conversation.

Time to create page: 0.072 seconds
Powered by Kunena Forum