vanilophp/framework

How do you get a products properties?

Closed this issue · 3 comments

https://vanilo.io/docs/3.x/properties has examples of how to retrieve a property and it's values, but nothing about how to do it from a product.

As in

$product = \Vanilo\Product\Models\Product::where('slug', $slug)->first();

But then what? I tried $product->propertyValues but that didn't work

The base product model doesn't have properties. You have two opportunities:

  1. If you're using the entire framework, then it contains an extended Product Model, that (among others) uses the HasPropertyValues trait.
  2. If you don't use the entire framework only modules from it, then you need to extend the product model in your application, eg. declare a class in app\Models\Product.php that uses the HasPropertyValues trait. Mind that you need to tell the system that now this is your product model via registering it with concord:

app/Providers/AppServiceProvider.php:

<?php

namespace App\Providers;

use Vanilo\Product\Contracts\Cart as ProductContract;
use App\Models\Product;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->get('concord')->registerModel(ProductContract::class, Product::class);
    }
}

See Also

Side note: to get a product by slug, you can use the Product::findBySlug('slug') method, reference

I am using the entire framework, so using Vanilo\Foundation\Models\Product was the part I was missing, thanks for the link to the Models section, I didn't realise I needed to use a different model than was specified in the products section (it might be worth having a note in the docs specifying if you are using the entire framework use Vanilo\Foundation\Models\Product instead of Vanilo\Product\Models\Product.

I ended up just dropping my options as json into the excerpt field as I realised delving into it a bit more that what I was actually after was more like a MasterProduct that isn't available yet.

So I just added:

{
  "options": [
    {
      "name": "3 - 4 week delivery",
      "key": "3%20-%204%20week%20delivery",
      "additional": 0
    },
    {
      "name": "2 week delivery",
      "key": "2%20week%20delivery",
      "additional": 30
    },
    {
      "name": "3 day delivery",
      "key": "3%20day%20delivery",
      "additional": 60
    }
  ],
  "key_features": [
    "Custom subdomain - <span class=\"text-gray-400\">https://<span class=\"text-purple-800\">name</span>.lovingmemory.link</span>",
    "Downloadable QR code that links to the site",
    "Site created and managed for you"
  ]
}

Then I pull it out in the controller and pass it to the view.