It's a common request having the ability to export a list of coupons to a CSV file to be handed over to partners or to be sent to customers via email.

For exporting, it's useful to give filter coupons by the batch where they were created. I've filed another issue for this:

https://www.drupal.org/project/commerce/issues/3146585

CommentFileSizeAuthor
#11 3146586-11.patch20.14 KBjsacksick

Issue fork commerce-3146586

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

recidive created an issue. See original summary.

flocondetoile’s picture

Not sure that a CSV export should be in the scope of Drupal Commerce. Have you tried Entity Export CSV module ?

jsacksick’s picture

I'm not against adding this directly into Commerce, we probably can steal some code from Commerce pricelist (referring to the "Export prices" form.

jsacksick’s picture

I've been investigating this task today and it's probably not so straightforward to provide this from Commerce directly.

We basically have 2 options:

  1. Provide a separate "Export coupons" form, similar to Commerce pricelist and its "export prices" form, but I think this option isn't the best in terms of user experience, as it doesn't allow you to preselect the coupons you want to export and trigger the export straight from the list.
  2. Replace the coupons list by a view, so that we can allow pre-filtering the list of coupons before the export (for example, by date of generation). The problem with this approach is that it is definitely more work... And possibly not entirely feasible from within Commerce itself.

I'm personally in favor of option 2) but since Commerce doesn't depend on Views bulk operations, we can't easily provide this OOB ourselves. Perhaps Commerce could simply replace the coupons listing by a view, and people could then update that view to attach a display built using Views Data export.

Note that if we do with option 2), we need to write 2 custom formatters for the coupon usage. (One for the global usage limit and the other one for the customer usage limit). There's currently logic for that in the coupon list builder itself.

Perhaps it's also a good idea to resurrect https://www.drupal.org/project/commerce_backoffice? Commerce backoffice could then depend on Views bulk operations since Commerce doesn't?

Thoughts?

I'm wondering if we shouldn't write the custom formatters regardless of the solution we pick, so we could allow people to easily do this using custom views...

So perhaps Commerce could provide the formatters... And eventually, the Action plugin?

jsacksick’s picture

Perhaps, as suggested by the comment in #2, this isn't really a core concern as this can be easily done using a contrib module... This can either be done using Views data export... Or using the VBO Export module for example...

But note once again that this would still require Commerce to provide a better views integration by providing field formatters for the usage fields.

rszrama’s picture

Title: Allow exporting coupons to a CSV file » Convert the Coupon list builder to a View

Retitling the issue, because once this is accomplished, generating a CSV export is resolved via Views Data Export. The scope for core will just be the View conversion, which requires adding the related Views field integrations we're missing.

jsacksick’s picture

Some requirements about the view:

  • Machine name: commerce_promotion_coupons
  • Url:/promotion/%/coupons
  • Permission: "update commerce_promotion" as it's the one currently being used. (See commerrce_promotion.routing.yml which takes precedence over the route provider).

The view needs a contextual filter on the parent promotion, configured using the following configuration: "Provide default value: Raw value from url, using "2" as the path component).

Instead of displaying all coupons, let's add a pager to the view to output only 50 coupons at once.

The view needs to use the formatter introduced in #3216713: Create a field formatter for the usage_limit and usage_limit_customer fields.

We also need a sort criteria on the coupon ID (DESC).

The view needs to be provided on install, and we also need an update hook to import the view.

Also, the "entity.commerce_promotion_coupon.collection" route should be removed from the routing.yml.

marchuk.vitaliy made their first commit to this issue’s fork.

jsacksick’s picture

Status: Active » Needs review
FileSize
20.14 KB

I investigated/fixed some of the tests failures.

I also updated the view, changed the permission to "administer commerce_promotion", added a coupon code filter, and put the empty text in the table.

jsacksick’s picture

Status: Needs review » Fixed

So the coupons list is now a view, it doesn't provide any logic for the CSV export but this can easily be added as pointed out by @rszrama.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.