Skip to content

Instantly share code, notes, and snippets.

@tobias-kuendig
Last active October 12, 2019 09:44
Show Gist options
  • Save tobias-kuendig/49ec99fc080fc6ba3824024f965eeafc to your computer and use it in GitHub Desktop.
Save tobias-kuendig/49ec99fc080fc6ba3824024f965eeafc to your computer and use it in GitHub Desktop.
OFFLINE.Mall: Create custom virtual product files
<?php namespace OFFLINE\MallGiftCards;
use Event;
use OFFLINE\Mall\Models\Category;
use OFFLINE\Mall\Models\Currency;
use OFFLINE\Mall\Models\Discount;
use OFFLINE\Mall\Models\Price;
use OFFLINE\Mall\Models\Product;
use OFFLINE\Mall\Models\ProductFileGrant;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function boot()
{
/**
* Listen for created download grants.
* Create a discount code, generate a gift card PDF,
* then attach it to the grant.
*/
Event::listen('mall.product.file_grant.created', function (ProductFileGrant $grant, Product $product) {
// Make sure the event is triggered by a product you care about. Validate this using the slug, the
// assigned category or one of the other unique values (user_defined_id, gtin, mpn, etc).
// In this example we check if the product belongs to a category that has the
// "generate-gift-cards" code set as a custom code.
$isGiftCardCategory = $product->categories->contains(function (Category $category) {
return $category->code === 'generate-gift-cards';
});
// Exit here if this is not a product with a gift card category attached.
if ( ! $isGiftCardCategory) {
return;
}
// Create a new discount model.
$discount = Discount::create([
// Generate a descriptive name for the discount using the product's price information.
'name' => sprintf('%s Gift Card', $product->price()->string),
// Make sure it can only be used once.
'max_number_of_usages' => 1,
// Re-use the grant's expiry date.
'expires' => $grant->expires_at,
// The discount is triggered by a code. The code will be created automatically on save.
'trigger' => 'code',
// The discount is of a fixed amount. The amount will be defined below.
'type' => 'fixed_amount',
]);
// Set the amounts relation for each currency.
Currency::getAll()->each(function (Currency $currency) use ($discount, $product) {
$discount->amounts()->save(new Price([
'currency_id' => $currency->id,
'field' => 'amounts',
// Re-use the price information form the product itself.
'price' => $product->price($currency)->float,
]));
});
// Generate a gift card PDF (or image). We're creating a text file here for simplicity.
$path = storage_path(sprintf('app/gift_card_%s.txt', $grant->id));
file_put_contents($path, 'Your coupon code is: ' . $discount->code);
// Attach the created file to the ProductFileGrant. This makes sure your custom file
// will be downloaded and not the file that is attached to the $product itself.
$grant->file = $path;
// Set a custom display name for the grant.
$grant->display_name = $discount->name;
// Don't forget to save the changes!
$grant->save();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment