An open API service indexing awesome lists of open source software.

https://github.com/solidusio-contrib/solidus_related_products

🍴 Related products extension for Solidus
https://github.com/solidusio-contrib/solidus_related_products

coupons discount ecommerce ecommerce-platform solidus

Last synced: 5 months ago
JSON representation

🍴 Related products extension for Solidus

Awesome Lists containing this project

README

          

# Solidus Related Products

[![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_related_products.svg?style=shield)](https://circleci.com/gh/solidusio-contrib/solidus_related_products)
[![codecov](https://codecov.io/gh/solidusio-contrib/solidus_related_products/branch/master/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_related_products)

This extension provides a generic way for you to define different types of relationships between your products, by defining a RelationType for each type of relationship you'd like to maintain.

You can manage RelationTypes via the admin configuration menu, and you can maintain product relationships via __Related Products__ tab on the edit product UI or via __Related Products__ section on the edit variant UI.

## Possible uses

* Accessories
* Cross Sells
* Up Sells
* Compatible Products
* Replacement Products
* Warranty & Support Products

## Installation

Add solidus_related_products to your Gemfile:

```ruby
gem 'solidus_related_products'
```

To get the latest changes, we suggest using the Github version:
```ruby
gem 'solidus_related_products', github: 'solidusio-contrib/solidus_related_products'
```

Bundle your dependencies and run the installation generator:

```shell
bin/rails generate solidus_related_products:install
```

## Usage

### Relation Types

You can create four different RelationTypes:

| Applies From | Applies To | Bi-Directional available |
| - | - | - |
| Spree::Product | Spree::Product | Yes |
| Spree::Product | Spree::Variant | No |
| Spree::Variant | Spree::Product | No |
| Spree::Variant | Spree::Variant | Yes |

**Bi-Directional**
You can optionally set the bi-directional flag (if available) to automatically create the inverse relation,
the flag can be set only on the type creation and can't be changed later, this is needed to avoid unpredictable behavior
in the case that the flag is changed.
Keep in mind that if you remove one side of the relation, also the other hand will be removed, the same way happens
for the description.
The discounts are disabled for Bi-Directional by setting the discount amount to be only zero, this is needed
because it's not clear how this feature should behave in this case.

The following examples use a `Spree::Product -> Spree::Product` relation type.

When you create a RelationType you can access that set of related products by referencing the relation_type name, see below for an example:
```ruby
rt = Spree::RelationType.create(name: 'Accessories', applies_from: 'Spree::Product', applies_to: 'Spree::Product')
=> #
product = Spree::Product.last
=> #
product.accessories
=> []
```

Since respond_to? will not work in this case, you can test whether a relation_type method exists with has_related_products?(method):

```ruby
product.has_related_products?('accessories')
# => true

if product.has_related_products?('accessories')
# Display an accessories box..
end
```

You can access all related products regardless of RelationType by:
```ruby
product.relations
=> []
```

**Discounts**
You can optionally specify a discount amount to be applied if a customer purchases both products.

Note: In order for the coupon to be automatically applied, you must create a promotion leaving the __code__ value empty, and adding an Action of type : __RelatedProductDiscount__ (blank codes are required for coupons to be automatically applied).

## Development

### Testing the extension

First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy
app if it does not exist, then it will run specs. The dummy app can be regenerated by using
`bin/rake extension:test_app`.

```shell
bin/rake
```

To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run

```shell
bundle exec rubocop
```

When testing your application's integration with this extension you may use its factories.
Simply add this require statement to your spec_helper:

```ruby
require 'solidus_related_products/factories'
```

### Running the sandbox

To run this extension in a sandboxed Solidus application, you can run `bin/sandbox`. The path for
the sandbox app is `./sandbox` and `bin/rails` will forward any Rails commands to
`sandbox/bin/rails`.

Here's an example:

```
$ bin/rails server
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop
```

### Updating the changelog

Before and after releases the changelog should be updated to reflect the up-to-date status of
the project:

```shell
bin/rake changelog
git add CHANGELOG.md
git commit -m "Update the changelog"
```

### Releasing new versions

Please refer to the dedicated [page](https://github.com/solidusio/solidus/wiki/How-to-release-extensions) on Solidus wiki.

## License

Copyright (c) 2010-2015 [Brian Quinn][1] and [contributors][2], released under the [New BSD License][3]

[1]: https://github.com/BDQ
[2]: https://github.com/solidusio-contrib/solidus_related_products/graphs/contributors
[3]: https://github.com/solidusio-contrib/solidus_related_products/blob/master/LICENSE.md