Problem/Motivation

The cart block, if present on the /cart page, and the product remove button as obtained from #2910740: Ajaxify the Remove from cart button has been used, then the cart block should also be ajaxifically updated.

Although this feature is not necessary, but it would be good for UX.

Proposed resolution

Not yet decided. But looks like I have to use dc_ajax_add_cart.refresh_page_elements_helper service.

Help required. See #6

Remaining tasks

Not yet decided.

User interface changes

The cart block should be updated ajaxifically.

API changes

Not yet decided.

Data model changes

N/A

Comments

subhojit777 created an issue. See original summary.

subhojit777’s picture

Title: The cart block should be updated (ajax) when item removed from cart » Cart block should be updated (ajax) when item removed from cart
subhojit777’s picture

subhojit777’s picture

subhojit777’s picture

Issue summary: View changes
subhojit777’s picture

I spent a few hours on this issue. I added submit callback in Drupal\dc_ajax_add_cart_views\Plugin\views\field\RemoveButton, this was removing the order item, and was returning an ajax response that would refresh the cart block. I used dc_ajax_add_cart.refresh_page_elements_helper service. But it throws an exception:

LogicException: A stray renderRoot() invocation is causing bubbling of attached assets to break.

Looks like the views submit callback is not the right place to return the ajax response. (unlike form submit callback)


I also tried doing the above using a custom ajax callback, but it breaks the ajax functionality of the order item removal.


I need help on this issue. It would be great if someone proposes an approach to achieve this, or may be point out what I am doing wrong.

subhojit777’s picture

Issue summary: View changes
subhojit777’s picture

Not sure but we can make use of the code that's is #2910744: Add a popup confirmation

  public function onResponse(FilterResponseEvent $event) {
    $response = $event->getResponse();

    // We only care if this happened after an entity was added to the cart.
    if (!$this->purchasedEntity) {
      return;
    }

    // We only care about AJAX responses.
    if (!$response instanceof AjaxResponse) {
      return;
    }

    // Re-render the cart block here.
  }
jefuri’s picture

Status: Active » Needs review
StatusFileSize
new6.13 KB

Same issue with quantities I guess. The ajax response does not update the view in the cart.
Added a eventsubcriber to update the cart block on order item update, remove and add to make sure this is triggerd.
And not only from the product form.

imclean’s picture

Status: Needs review » Needs work

Result:

PHP Fatal error: Trait 'Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait' not found in /var/www/html/modules/contrib/dc_ajax_add_cart/tests/src/Functional/AjaxAddCartTestBase.php on line 13>

For a solution, see: #3045863: Trait 'Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait' not found

Also some coding standard issues.

vasyok’s picture

StatusFileSize
new97.9 KB

For me #9 does not work. After push Remove button - nothing happened.
Here is my view:
cartblock

durum’s picture

#9 almost works:

Scenario 1: There is 1 item in the cart. Successfully removes the item & refreshes the block.

Scenario 2: There are 2 or more items in the cart. You remove the last one. Nothing happens. Refresh the page. Item gone.

Scenario 3: There are 2 or more items in the cart. You remove any of them except the last one. Nothing happens. Refresh the page. The item you removed and all the following items are gone.

Same results with Ajax enabled and disabled in the view settings.