Jump to content

features values - how to pick variable


SonnyBoyII

Recommended Posts

Hi,

 

Im editing classes/product.php file.

How Can I get a value stored in product feature called lets say xyz and value name abc??

 

I can use very easly 'depth' field :

$product['depth']

$product->depth

 

but how can I call a value stored in value "abc" form "xyz" feature?

Link to comment
Share on other sites

So you are trying to get a specific feature in classes/Product.php? I can only see a function there to get all features. You could use it and then use a loop to find the right feature. For example:

 

$features = self::getFrontFeatures($id_lang);
$featureValue = null;

foreach ($features as $feature)
   if ($feature['name'] == 'xyz')
       $featureValue = $feature['value'];

  • Like 1
Link to comment
Share on other sites

So you are trying to get a specific feature in classes/Product.php? I can only see a function there to get all features. You could use it and then use a loop to find the right feature. For example:

 

$features = self::getFrontFeatures($id_lang);
$featureValue = null;

foreach ($features as $feature)
   if ($feature.name == 'xyz')
       $featureValue = $feature.value;

 

briliant! So I need to add this funcion to classes/product.php somewhere and I will be able to use $featureValue to play with other functions??

Link to comment
Share on other sites

The getFrontFeatures() function should already exist in classes/Product.php. You can put the above code in a function to get a feature value, assuming the $id_lang variable exists. If it doesn't, you will need to add global $cookie; to the top of the function and then use (int)$cookie->id_lang instead of $id_lang.

  • Like 1
Link to comment
Share on other sites

The getFrontFeatures() function should already exist in classes/Product.php. You can put the above code in a function to get a feature value, assuming the $id_lang variable exists. If it doesn't, you will need to add global $cookie; to the top of the function and then use (int)$cookie->id_lang instead of $id_lang.

 

Thank you so much, but it doesnt work... im trying to override updateQuantity function and I need feature value to do this:

 

return $productObj->addStockMvt(-(int)($product['cart_quantity'])*($featureValue), (int)_STOCK_MOVEMENT_ORDER_REASON_, (int)$product['id_product_attribute'], (int)$id_order, NULL);

 

That is my whole function:

 

public static function updateQuantity($product, $id_order = NULL)
{

global $cookie;

$features = self::getFrontFeatures((int)$cookie->id_lang);
$featureValue = null;
foreach ($features as $feature) {
   if ($feature.name == 'xyz')
    $featureValue = $feature.value;
 }

	if (!is_array($product))
		die (Tools::displayError());

	if (!Configuration::get('PS_STOCK_MANAGEMENT'))
		return true;

	if (Pack::isPack((int)($product['id_product'])))
	{
		$products_pack = Pack::getItems((int)($product['id_product']), (int)(Configuration::get('PS_LANG_DEFAULT')));
		foreach($products_pack AS $product_pack)
		{
			$tab_product_pack['id_product'] = (int)($product_pack->id);
			$tab_product_pack['id_product_attribute'] = self::getDefaultAttribute($tab_product_pack['id_product'], 1);
			$tab_product_pack['cart_quantity'] = (int)($product_pack->pack_quantity * $product['cart_quantity']);
			self::updateQuantity($tab_product_pack);
		}
	}

	$productObj = new Product((int)$product['id_product'], false, (int)Configuration::get('PS_LANG_DEFAULT'));
	return $productObj->addStockMvt(-(int)($product['cart_quantity'])*($featureValue), (int)_STOCK_MOVEMENT_ORDER_REASON_, (int)$product['id_product_attribute'], (int)$id_order, NULL);
}

 

 

I have checked, the code and idea are right. I get what I wanted using $depth field to store integer but I might to need this field in the future. Thats why I decided to use feature field instead.. unfortunately it seems to be tricky ...

Link to comment
Share on other sites

Rocky, the loop stil doesnt work properly for some reason .. :(

 

public static function updateQuantity($product, $id_order = NULL)
{

global $cookie; 
$features = self::getFrontFeatures((int)$cookie->id_lang);
$featureValue = null;

foreach ($features as $feature) {
   if ($feature['name'] == 'xyz')
    $featureValue = $feature['value'];
}

	if (!is_array($product))
		die (Tools::displayError());

	if (!Configuration::get('PS_STOCK_MANAGEMENT'))
		return true;

	if (Pack::isPack((int)($product['id_product'])))
	{
		$products_pack = Pack::getItems((int)($product['id_product']), (int)(Configuration::get('PS_LANG_DEFAULT')));
		foreach($products_pack AS $product_pack)
		{
			$tab_product_pack['id_product'] = (int)($product_pack->id);
			$tab_product_pack['id_product_attribute'] = self::getDefaultAttribute($tab_product_pack['id_product'], 1);
			$tab_product_pack['cart_quantity'] = (int)($product_pack->pack_quantity * $product['cart_quantity']);
			self::updateQuantity($tab_product_pack);
		}
	}

	$productObj = new Product((int)$product['id_product'], false, (int)Configuration::get('PS_LANG_DEFAULT'));
	return $productObj->addStockMvt(-(int)($product['cart_quantity'])*($featureValue), (int)_STOCK_MOVEMENT_ORDER_REASON_, (int)$product['id_product_attribute'], (int)$id_order, NULL);
}

Link to comment
Share on other sites

Try:

 

public static function updateQuantity($product, $id_order = NULL)
{
global $cookie; 

if (!is_array($product))
 die (Tools::displayError());

if (!Configuration::get('PS_STOCK_MANAGEMENT'))
 return true;

if (Pack::isPack((int)($product['id_product'])))
{
 $products_pack = Pack::getItems((int)($product['id_product']), (int)(Configuration::get('PS_LANG_DEFAULT')));
 foreach($products_pack AS $product_pack)
 {
  $tab_product_pack['id_product'] = (int)($product_pack->id);
  $tab_product_pack['id_product_attribute'] = self::getDefaultAttribute($tab_product_pack['id_product'], 1);
  $tab_product_pack['cart_quantity'] = (int)($product_pack->pack_quantity * $product['cart_quantity']);
  self::updateQuantity($tab_product_pack);
 }
}

$productObj = new Product((int)$product['id_product'], false, (int)Configuration::get('PS_LANG_DEFAULT'));       

$features = $productObj->getFrontFeatures((int)$cookie->id_lang);
$featureValue = null;

foreach ($features as $feature)
 if ($feature['name'] == 'xyz')
  $featureValue = $feature['value'];

return $productObj->addStockMvt(-(int)($product['cart_quantity'])*($featureValue), (int)_STOCK_MOVEMENT_ORDER_REASON_, (int)$product['id_product_attribute'], (int)$id_order, NULL);
} 

 

 

 

 

Link to comment
Share on other sites

Its working !! Thanks Rocky!

 

Now, I have the same problem with another function from the same class which I trying override... I have tried to do exactly the same.

The modyfication is working if I use $depth field for instance.

 

If I refound a product in "order" section the loop will crash the page.

 

Here is my code:

 

public static function reinjectQuantities(&$orderDetail, $quantity)
{
	global $cookie;
	if (!Validate::isLoadedObject($orderDetail))
		die(Tools::displayError());

	if (Pack::isPack((int)($orderDetail->product_id)))
	{
		$products_pack = Pack::getItems((int)($orderDetail->product_id), (int)(Configuration::get('PS_LANG_DEFAULT')));
		foreach($products_pack AS $product_pack)
			if (!$product_pack->addStockMvt((int)($product_pack->pack_quantity * $quantity), _STOCK_MOVEMENT_ORDER_REASON_, (int)$product_pack->id_product_attribute, (int)$orderDetail->id_order, (int)$cookie->id_employee))
				return false;
	}

	$features = $productObj->getFrontFeatures((int)$cookie->id_lang);
	$featureValue = null;
		foreach ($features as $feature)
	if ($feature['name'] == 'multi')
	$featureValue = $feature['value'];

	$product = new Product((int)$orderDetail->product_id);

	if (!$product->addStockMvt((int)$quantity*($featureValue), _STOCK_MOVEMENT_ORDER_REASON_, (int)$orderDetail->product_attribute_id, (int)$orderDetail->id_order, (int)$cookie->id_employee))
		return false;

	$orderDetail->product_quantity_reinjected += (int)($quantity);
	return true;
}


Link to comment
Share on other sites

You're trying to use the $product object before it's created! Try:

 

public static function reinjectQuantities(&$orderDetail, $quantity)
{
global $cookie;
if (!Validate::isLoadedObject($orderDetail))
 die(Tools::displayError());

if (Pack::isPack((int)($orderDetail->product_id)))
{
 $products_pack = Pack::getItems((int)($orderDetail->product_id), (int)(Configuration::get('PS_LANG_DEFAULT')));                         
 foreach($products_pack AS $product_pack)
  if (!$product_pack->addStockMvt((int)($product_pack->pack_quantity * $quantity), _STOCK_MOVEMENT_ORDER_REASON_, (int)$product_pack->id_product_attribute, (int)$orderDetail->id_order, (int)$cookie->id_employee))
   return false;
}

$product = new Product((int)$orderDetail->product_id, (int)$cookie->id_lang);

$features = $product->getFrontFeatures((int)$cookie->id_lang);
$featureValue = null;
foreach ($features as $feature)
if ($feature['name'] == 'multi')
 $featureValue = $feature['value'];

if (!$product->addStockMvt((int)$quantity*($featureValue), _STOCK_MOVEMENT_ORDER_REASON_, (int)$orderDetail->product_attribute_id, (int)$orderDetail->id_order, (int)$cookie->id_employee))
 return false;
$orderDetail->product_quantity_reinjected += (int)($quantity);
return true;
}

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
  • 4 months later...

{assign var="features" value=$product.features}

{foreach from=$features item=feature}

{feature.name}

{/foreach}

 

This code works perfect in product.tpl

 

But how can I get features values in

shopping-cart.tpl

shopping-cart-product-line.tpl

blockcart.tpl

blockcart-json.tpl

 

When I try parse features array in this blocks nothing happen.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...