dynamic/silverstripe-foxy-discounts

Offer discounts on purchase conditions for your Foxy products.

Installs: 557

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 2

Open Issues: 2

Type:silverstripe-vendormodule

1.2.3 2021-03-22 02:18 UTC

This package is auto-updated.

Last update: 2024-11-30 01:56:30 UTC


README

Offer discounts on purchase conditions for your Foxy products.

Build Status Scrutinizer Code Quality Build Status codecov

Latest Stable Version Total Downloads Latest Unstable Version License

Requirements

  • SilverStripe ^4.0
  • SilverStripe Foxy ^1.0

Installation

composer require dynamic/silverstripe-foxy-discounts

License

See License

Example configuration

Add the following extensions to your product classes:

Dynamic\Products\Page\Product:
  extensions:
    - Dynamic\Foxy\Discounts\Extension\ProductDataExtension

PageController:
  extensions:
    - Dynamic\Foxy\Discounts\Extension\PageControllerExtension
  

Advanced Usage

You can limit discounts based on Product restrictions. The example below would add a "Discount only these products" and "These products should be excluded from the discount" type of logic:

note: This example has additional requirements such as GridFieldExtension be installed

<?

namespace {
    use SilverStripe\ORM\DataExtension;
    use Dynamic\Products\Page\Product;
    
	class DiscountDataExtension extends DataExtension
	{
	    /**
	     * @var array
	     */
	    private static $many_many = [
	        'Products' => Product::class,
	        'ExcludeProducts' => Product::class,
	    ];
	
	    /**
	     * @param FieldList $fields
	     */
	    public function updateCMSFields(FieldList $fields)
	    {
	        if ($this->owner->ID) {
	            // Products
	            $field = $fields->dataFieldByName('Products');
	            $fields->removeByName('Products');
	            $fields->addFieldToTab('Root.Included', $field);
	            $field->setDescription('Limit the discount to these products. If no products specified, all products will receive the discount');
	            $config = $field->getConfig();
	            $config
	                ->removeComponentsByType([
	                    GridFieldAddExistingAutocompleter::class,
	                    GridFieldAddNewButton::class,
	                    GridFieldArchiveAction::class,
	                ])
	                ->addComponents([
	                    new GridFieldAddExistingSearchButton(),
	                ]);
	
	            $exclusions = $fields->dataFieldByName('ExcludeProducts');
	            $fields->removeByName('ExcludeProducts');
	            $fields->addFieldToTab('Root.Excluded', $exclusions);
	            $exclusions->setDescription('Products in this list will ALWAYS be excluded from the discount, even if added to the "Included" tab.');
	            $excludeConfig = $exclusions->getConfig();
	            $excludeConfig
	                ->removeComponentsByType([
	                    GridFieldAddExistingAutocompleter::class,
	                    GridFieldAddNewButton::class,
	                    GridFieldArchiveAction::class,
	                ])
	                ->addComponents([
	                    new GridFieldAddExistingSearchButton(),
	                ]);
	        }
	    }
	
	    /**
	     * @return array
	     */
	    public function getRestrictions()
	    {
	        if ($this->owner->Products()->count() == 0) {
	            $products = Product::get()->column();
	        } else {
	            $products = $this->owner->Products()->column();
	        }
	
	        foreach ($this->owner->ExcludeProducts()->column() as $id) {
	            if (in_array($id, $products)) {
	                $key = array_search($id, $products);
	                unset($products[$key]);
	            }
	        }
	
	        return $products;
	    }
	}
}       

And apply to Discount in foxy.yml:

Dynamic\Foxy\Discounts\Model\Discount:
  extensions:
    - DiscountDataExtension

Maintainers

Bugtracker

Bugs are tracked in the issues section of this repository. Before submitting an issue please read over existing issues to ensure yours is unique.

If the issue does look like a new bug:

  • Create a new issue
  • Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots and screencasts can help here.
  • Describe your environment as detailed as possible: SilverStripe version, Browser, PHP version, Operating System, any installed SilverStripe modules.

Please report security issues to the module maintainers directly. Please don't file security issues in the bugtracker.

Development and contribution

If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.