VirtueMart

WooCommerce

Others

Docs

Support

Demo

Blog

About

Forum
Welcome, Guest
Username: Password: Remember me

TOPIC: [SOLVED] Problem to create custom field

Problem to create custom field 26 May 2015 09:56 #1

Dear Reinhold,
I just made a custom field "Dangerous" and define products dangerous by this custom field.
Now the problem is that I want use "Dangerous" in my rules. But how can i get the value of my custom field "Dangerous" ?

I use your plugin extension to create this, I send you the code Dangerous.php:
<?php

defined ('_JEXEC') or die('Restricted access');

if (!class_exists ('VmPlugin')) {
	require(JPATH_VM_PLUGINS . DS . 'vmplugin.php');
}

class plgVmShipmentRulesDangerous extends VmPlugin {

	function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method, $cart_prices) {
		$cartvals['Dangerous'] = VALUE OF DANGEROUS;		
	}
	function onVmShippingRulesRegisterCustomFunctions() {}
}

My question is : How can I get the "VALUE OF DANGEROUS"?
Best regards.
The administrator has disabled public write access.

Problem to create custom field 26 May 2015 17:48 #2

Hi again,
I did a print_r($cart) and found a variable like this:
[customfields] -> Dangerous 1
How can I get this field? I already tried $cart->customfields, it doesn't works.

Somebody could help me?

Best regards.
The administrator has disabled public write access.

Problem to create custom field 28 May 2015 00:11 #3

Dear Mediafaune,
First of all, you need to be aware that the plugin calculates shipping costs based on CART characteristics. In particular, the onVmShippingRulesGetCartValues is passed the whole cart and the list of products to be considered (e.g. when there is a evaluate_for_categories(....) call, the onVmShippingRulesGetCartValues function will be called to get the cart variables for just the products that have the desired categories. The $cart will always be the full cart, though. So to get the calculations right, you really need to use the $products list rather than the $cart.

Second, each member of the $products list has an array of all customfields, so if you want to set the variable Dangerous to true (value 1), then you need to loop through all products, for each product loop through all customfields of that product and if the customfield is the "Dangerous" customfield, then check the value of the custom field. I would write the code like:
function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method, $cart_prices) {
  $cartvals['dangerous'] = false;
  foreach ($products as $p) {
    foreach ($p->customfields as $cf) {
      if ($cf->custom_title=="Dangerous" and $cf->customfield_value==1) {
        $cartvals['dangerous'] = true;
      }
    }
  }
}

This should give you an idea how you can adjust the code if you use other custom field values.

Best regards,
Reinhold
The administrator has disabled public write access.

Problem to create custom field 01 Jun 2015 11:42 #4

Dear Reinhold,

First of all, thanks you for helping.
I've tried your code but the field $p->customfields seems to be a field and NOT an array.
So I've just change the code like this
$cartvals['dangerous'] = false;
		foreach ($products as $p) {
			if(preg_match("#Dangerous#", $p->customfields)){
				$cartvals['dangerous'] = true;
			}
		}

And now it works well. But may be there is another solution to find thoses fields in customfields?
Best regards.
The administrator has disabled public write access.

[SOLVED] Problem to create custom field 02 Jun 2015 00:49 #5

Problem solved.
The administrator has disabled public write access.

[SOLVED] Problem to create custom field 03 Jun 2015 17:30 #6

Problem solved.
The administrator has disabled public write access.

VM - Shopping cart

 x 

Cart empty

Login Formular