Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix Mini Cart doesn't update data (#5741)
Browse files Browse the repository at this point in the history
* Remove api hydration for the Mini Cart Block #5729

Remove api hydration for the Mini Cart Block

* fix PHP error

* send event when the button is clicked

* fix import order
  • Loading branch information
gigitux committed Feb 10, 2022
1 parent 72c3234 commit a9dc0e1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { __ } from '@wordpress/i18n';
import triggerFetch from '@wordpress/api-fetch';
import { useEffect, useCallback, useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { triggerAddedToCartEvent } from '@woocommerce/base-utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -107,6 +108,7 @@ const FormSubmit = () => {
} else {
receiveCart( response );
}
triggerAddedToCartEvent( { preserveCartData: true } );
dispatchActions.setAfterProcessing( response );
setIsSubmitting( false );
} );
Expand Down
8 changes: 8 additions & 0 deletions src/BlockTypes/AllProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ protected function enqueue_data( array $attributes = [] ) {
$this->asset_data_registry->add( 'min_rows', wc_get_theme_support( 'product_blocks::min_rows', 1 ), true );
$this->asset_data_registry->add( 'max_rows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true );
$this->asset_data_registry->add( 'default_rows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true );
$this->hydrate_from_api();
}

/**
* Hydrate the All Product block with data from the API.
*/
protected function hydrate_from_api() {
$this->asset_data_registry->hydrate_api_request( '/wc/store/cart' );
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ protected function enqueue_data( array $attributes = [] ) {

// Hydrate the following data depending on admin or frontend context.
if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
$this->hydrate_from_api();

$label_info = $this->get_tax_label();

$this->tax_label = $label_info['tax_label'];
Expand Down Expand Up @@ -209,13 +207,6 @@ function_exists( 'wp_is_block_theme' ) &&
do_action( 'woocommerce_blocks_cart_enqueue_data' );
}

/**
* Hydrate the cart block with data from the API.
*/
protected function hydrate_from_api() {
$this->asset_data_registry->hydrate_api_request( '/wc/store/cart' );
}

/**
* Returns the script data given its handle.
*
Expand Down
3 changes: 2 additions & 1 deletion src/BlockTypes/SingleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ protected function get_block_type_editor_script( $key = null ) {
}

/**
* Hydrate the cart block with data from the API.
* Hydrate the Single Product block with data from the API.
*
* @param int $product_id ID of the product.
*/
protected function hydrate_from_api( int $product_id ) {
$this->asset_data_registry->hydrate_api_request( "/wc/store/products/$product_id" );
$this->asset_data_registry->hydrate_api_request( '/wc/store/cart' );
}
}

0 comments on commit a9dc0e1

Please sign in to comment.