Jelajahi Sumber

Add options relationship to Product model for flexible variant options

chengwl 3 minggu lalu
induk
melakukan
780353a443
1 mengubah file dengan 12 tambahan dan 0 penghapusan
  1. 12 0
      packages/Webkul/Product/src/Models/Product.php

+ 12 - 0
packages/Webkul/Product/src/Models/Product.php

@@ -20,6 +20,7 @@ use Webkul\Category\Models\CategoryProxy;
 use Webkul\Core\Models\ChannelProxy;
 use Webkul\Inventory\Models\InventorySourceProxy;
 use Webkul\Product\Contracts\Product as ProductContract;
+use Longyi\Core\Models\ProductOption;
 use Webkul\Product\Database\Factories\ProductFactory;
 use Webkul\Product\Type\AbstractType;
 
@@ -292,6 +293,17 @@ class Product extends Model implements ProductContract
         return $this->belongsToMany(ChannelProxy::modelClass(), 'product_channels', 'product_id', 'channel_id');
     }
 
+    /**
+     * The flexible variant options that belong to the product.
+     */
+    public function options(): BelongsToMany
+    {
+        return $this->belongsToMany(ProductOption::class, 'product_product_options', 'product_id', 'product_option_id')
+            ->withPivot(['position', 'is_required', 'meta'])
+            ->withTimestamps()
+            ->orderBy('product_product_options.position');
+    }
+
     /**
      * Is saleable.
      *