The only cart permission is 'access cart', and this controls access to the /cart page, but not access to instances of the cart block, and not visibility of the Add to Cart button of the variations widget.
Todo: check for this permission before displaying the Add to Cart widget button and the cart block.

Comments

finne created an issue. See original summary.

waspper’s picture

Status: Active » Needs review
StatusFileSize
new1.04 KB

Added a small patch.

bojanz’s picture

Title: Use 'access cart' permission to show Add to Cart button » The 'access cart' permission is not respected by the add to cart form / cart block
Category: Task » Bug report
Status: Needs review » Needs work

Good catch. We also need to make the cart block respect the same permission.

waspper’s picture

Status: Needs work » Needs review
StatusFileSize
new2.19 KB

Added asked improvements to the patch.

Anonymous’s picture

Works for me. I am able to disable the add to cart button for anonymous users

bojanz’s picture

Issue tags: +Needs tests

This needs a test.

bojanz’s picture

Status: Needs review » Needs work
+    if ($form_state->get('hide_form') || !$this->currentUser->hasPermission('access cart')) {
       $form['#access'] = FALSE;
     }

I don't like this because it hides the form after it has already been built. A lot of work has happened for nothing (loading variations, rendering attributes, checking availability). We need to move the permission check up the stack, to either AddToCartFormatter or ProductLazyBuilders. This will depend on how we account for cachability, we need to ensure that the user.permissions cache context is added.

EDIT: mglaman confirms it, we want to modify AddToCartFormatter.

bojanz’s picture

Title: The 'access cart' permission is not respected by the add to cart form / cart block » Remove the 'access cart' permission because it is unused and not granular enough
Issue tags: -Needs tests

rszrama and mglaman feel that having a single permission is not enough, and that the existing permission is misnamed. They suggest having one for the cart page, one for the cart block, and one for the add to cart form.

Since that's is out of scope for a bug report, mglaman will open a feature request for the community to give feedback on, and I'll remove the unused/wrong permission here.

mglaman’s picture

Linking to issue where we can discuss a full-fledged implementation: #2955039: Provide granular permissions for Cart functionality

  • bojanz committed 0054dd4 on 8.x-2.x
    Issue #2897813 by waspper, bojanz: Remove the 'access cart' permission...
bojanz’s picture

Status: Needs work » Fixed

Done.

Status: Fixed » Closed (fixed)

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