( html_entity_decode( $value ) ) ); }, $sort_array ); asort( $array_without_accents ); return array_replace( $array_without_accents, $sort_array ); } /** * Retrieves formatted shipping zones from WooCommerce. * * @return array An array of formatted shipping zones. */ public static function get_shipping_zones() { $shipping_zones = \WC_Shipping_Zones::get_zones(); $formatted_shipping_zones = array_reduce( $shipping_zones, function ( $acc, $zone ) { $acc[] = array( 'id' => $zone['id'], 'title' => $zone['zone_name'], 'description' => $zone['formatted_zone_location'], ); return $acc; }, array() ); $formatted_shipping_zones[] = array( 'id' => 0, 'title' => __( 'International', 'woocommerce' ), 'description' => __( 'Locations outside all other zones', 'woocommerce' ), ); return $formatted_shipping_zones; } /** * Recursively search the checkout block to find the express checkout block and * get the button style attributes using the parse_blocks function. * * @param array $blocks Blocks to search. * @param string $cart_or_checkout The block type to check. * * @return array Block attributes. */ public static function find_express_checkout_attributes_in_parsed_blocks( $blocks, $cart_or_checkout ) { $express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block'; foreach ( $blocks as $block ) { if ( ! empty( $block['blockName'] ) && $express_block_name === $block['blockName'] && ! empty( $block['attrs'] ) ) { return $block['attrs']; } if ( ! empty( $block['innerBlocks'] ) ) { $answer = self::find_express_checkout_attributes_in_parsed_blocks( $block['innerBlocks'], $cart_or_checkout ); if ( $answer ) { return $answer; } } } } /** * Recursively search the checkout block to find the express checkout block and * get the button style attributes * * @param string|array $post_content The post content. * @param string $cart_or_checkout The block type to check. * * @return array|null Block attributes, if present and valid, otherwise `null`. */ public static function find_express_checkout_attributes( $post_content, $cart_or_checkout ) { if ( is_array( $post_content ) ) { // If an array is passed, assume it's already been parsed with parse_blocks, // use the old method, and show a deprecation warning. wc_deprecated_argument( 'post_content', '10.3.0', 'Passing parsed blocks as an array in $post_content is deprecated. Please pass the post content as a string.' ); return self::find_express_checkout_attributes_in_parsed_blocks( $post_content, $cart_or_checkout ); } $express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block'; $scanner = Block_Scanner::create( $post_content ); while ( $scanner->next_delimiter() ) { if ( $scanner->opens_block( $express_block_name ) ) { return $scanner->allocate_and_return_parsed_attributes(); } } return null; } /** * Given an array of blocks, find the express payment block and update its attributes. * * @param array $blocks Blocks to search. * @param string $cart_or_checkout The block type to check. * @param array $updated_attrs The new attributes to set. */ public static function update_blocks_with_new_attrs( &$blocks, $cart_or_checkout, $updated_attrs ) { $express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block'; foreach ( $blocks as $key => &$block ) { if ( ! empty( $block['blockName'] ) && $express_block_name === $block['blockName'] ) { $blocks[ $key ]['attrs'] = $updated_attrs; } if ( ! empty( $block['innerBlocks'] ) ) { self::update_blocks_with_new_attrs( $block['innerBlocks'], $cart_or_checkout, $updated_attrs ); } } } /** * Check if the cart page is defined. * * @return bool True if the cart page is defined, false otherwise. */ public static function has_cart_page() { return wc_get_page_permalink( 'cart', -1 ) !== -1; } /** * Get product IDs from a user's persistent cart. * * This method retrieves product IDs stored in the user's persistent cart meta. * It can be used for abandoned cart emails, cart-based product collections, * and other scenarios where cart products need to be retrieved for a user. * * @param int|null $user_id The user ID. If not provided, will attempt to look up by email. * @param string|null $user_email The user email. Used to lookup user if ID not provided. * @return array Array of product IDs from the user's cart, or empty array if none found. */ public static function get_cart_product_ids_for_user( ?int $user_id, ?string $user_email ) { if ( empty( $user_id ) && ! empty( $user_email ) ) { $user = get_user_by( 'email', $user_email ); if ( $user ) { $user_id = $user->ID; } } if ( empty( $user_id ) ) { return array(); } $cart_meta = get_user_meta( $user_id, '_woocommerce_persistent_cart_' . get_current_blog_id(), true ); if ( empty( $cart_meta ) || ! is_array( $cart_meta ) || empty( $cart_meta['cart'] ) ) { return array(); } return array_values( array_unique( array_filter( array_map( function ( $cart_item ) { return isset( $cart_item['product_id'] ) ? intval( $cart_item['product_id'] ) : 0; }, $cart_meta['cart'] ) ) ) ); } }