Jump to content

Accepting multiple values for features - core modification.


Spluf

Recommended Posts

Hi guys,

 

In case anyone needs to add multiple values for features for their products in prestashop here is how you can do it in 1.6x versions:

 

In classes/product.php on line 3295 you can find this code:

	public function addFeaturesToDB($id_feature, $id_value, $cust = 0)
	{
		if ($cust)
		{
			$row = array('id_feature' => (int)$id_feature, 'custom' => 1);
			Db::getInstance()->insert('feature_value', $row);
			$id_value = Db::getInstance()->Insert_ID();
		}
		$row = array('id_feature' => (int)$id_feature, 'id_product' => (int)$this->id, 'id_feature_value' => (int)$id_value);
		Db::getInstance()->insert('feature_product', $row);
		SpecificPriceRule::applyAllRules(array((int)$this->id));
		if ($id_value)
			return ($id_value);
	}

The only thing you need to do in order to accept multiple values is to change that code into this:

	public function addFeaturesToDB($id_feature, $id_value, $cust = 0)
	{
    // Default behavior.
    if ($cust)
		{
			$row = array('id_feature' => (int)$id_feature, 'custom' => 1);
			Db::getInstance()->insert('feature_value', $row);
			$id_value = Db::getInstance()->Insert_ID();
		}

    // For multi-value features, build array of rows and insert into db.
    $base =  array(
      'id_feature' => (int)$id_feature,
      'id_product' => (int)$this->id,
    );
    $rows = array();
    foreach ($id_value as $value) {
      if(!empty($value)) {
        $rows[] = $base + array('id_feature_value' => $value);
      }
    }
    $row = array('id_feature' => (int)$id_feature, 'id_product' => (int)$this->id, 'id_feature_value' => (int)$id_value);
		Db::getInstance()->insert('feature_product', $row);
		SpecificPriceRule::applyAllRules(array((int)$this->id));
		if ($id_value)
			return ($id_value);
	}

You can find attatched a picture with how it looks in the backend.post-828900-0-85678800-1412006248_thumb.jpg

 

One more thing in order for this to work, you'll have to drop the primary keys for id_feature and id_product in the ps_feature_product table in your database.

 

I home you find this usefull.

 

Have a nice day everyone :)

 

P.s. if someone tries and encounters some problems with this please get back to me, i did this some time ago and i hope i haven't forgot anything :).

Edited by Spluf (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Your modification looks similar to this thread,

 

 
but I have an issue with the default Layered Navigation Block
 
 

Hello!

This is truly great modification to a default Prestashop functionality - thanks a lot, that is exactly I was seaching for. And I would agree with others that this functionality should be default in Prestashop.

It works perfecty, but I have an issue with the default Layered Navigation Block.

Consider you have two products:
TShirt1 with feature "Color" set to "Red" and "Blue"
TShirt2 with feature "Color" set to "Red" and "Green"

Consider those two items are in the same category. We put "Color" as indexable and in Layered Nagigation configuration enable this to work for filtering.

Now in a Front Office you'll see feature "Color" with 3 options "Red", "Blue" and "Green" in the Layered Navigation Block. Suppose you select "Blue" checkbox (i.e. you want to see T-Shirts having only blue color), and Prestashop filters the product and shows you the product TShirt1 - all as expected.

Problem is that from the Layered Nagigation Block on left the "Green" checkbox doesn't disappear - but it should, because after application of filter "Blue" there are no products available with "Green" feature.

Does anybody has an idea how one should modify Layered Navigation module to make it to work as expected with multiple features?

P.S. I know that color is better to set as an attribute, but here it was chosen to be a feature for the sake of simplicity.

 
Link to comment
Share on other sites

  • 2 weeks later...

Thank you for sharing,

 

I try but not changed, don't know why, anyone can advise.... 

1. In classes/product.php to change the code

2. to drop the primary keys for id_feature and id_product in the ps_feature_product table in database.

 

Thank you!

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
  • 1 year later...

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...