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

Commit

Permalink
Merge branch 'trunk' into 9300-improve-transform-classic-product-temp…
Browse files Browse the repository at this point in the history
…late-ux
  • Loading branch information
gigitux authored May 9, 2023
2 parents 7730ef0 + 62697ad commit 4737fdb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
defaultAddressFields,
ShippingAddress,
} from '@woocommerce/settings';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, dispatch } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { FieldValidationStatus } from '@woocommerce/types';

Expand Down Expand Up @@ -123,6 +123,23 @@ const AddressForm = ( {
} );
}, [ addressFormFields, onChange, values ] );

// Clear postcode validation error if postcode is not required.
useEffect( () => {
addressFormFields.forEach( ( field ) => {
if ( field.key === 'postcode' && field.required === false ) {
const store = dispatch( 'wc/store/validation' );

if ( type === 'shipping' ) {
store.clearValidationError( 'shipping_postcode' );
}

if ( type === 'billing' ) {
store.clearValidationError( 'billing_postcode' );
}
}
} );
}, [ addressFormFields, type, clearValidationError ] );

useEffect( () => {
if ( type === 'shipping' ) {
validateShippingCountry(
Expand Down Expand Up @@ -265,11 +282,13 @@ const AddressForm = ( {
} )
}
customValidation={ ( inputObject: HTMLInputElement ) =>
customValidationHandler(
inputObject,
field.key,
values
)
field.required || inputObject.value
? customValidationHandler(
inputObject,
field.key,
values
)
: true
}
errorMessage={ field.errorMessage }
required={ field.required }
Expand Down
10 changes: 6 additions & 4 deletions src/BlockTypes/RelatedProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function build_query( $query ) {
return $query;
}

$related_products_ids = $this->get_related_products_ids();
$related_products_ids = $this->get_related_products_ids( $query['posts_per_page'] );
if ( count( $related_products_ids ) < 1 ) {
return array();
}
Expand All @@ -113,11 +113,12 @@ public function render_block( string $content, array $block ) {
return $content;
}

// If there are no related products, render nothing.
$related_products_ids = $this->get_related_products_ids();

if ( count( $related_products_ids ) < 1 ) {
return '';
}

return $content;
}

Expand All @@ -142,14 +143,15 @@ private function is_related_products_block( $block ) {
* Get related products ids.
* The logic is copied from the core function woocommerce_related_products. https://github.com/woocommerce/woocommerce/blob/ca49caabcba84ce9f60a03c6d3534ec14b350b80/plugins/woocommerce/includes/wc-template-functions.php/#L2039-L2074
*
* @param number $product_per_page Products per page.
* @return array Products ids.
*/
private function get_related_products_ids() {
private function get_related_products_ids( $product_per_page = 5 ) {
global $post;

$product = wc_get_product( $post->ID );

$related_products = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), 5, $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
$related_products = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $product_per_page, $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
$related_products = wc_products_array_orderby( $related_products, 'rand', 'desc' );

$related_product_ids = array_map(
Expand Down

0 comments on commit 4737fdb

Please sign in to comment.