PATH:
home
/
lab2454c
/
equitablegold.com
/
wp-content
/
plugins
/
woocommerce
/
src
/
Blocks
/
BlockTypes
<?php namespace Automattic\WooCommerce\Blocks\BlockTypes; use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; /** * CatalogSorting class. */ class AddToCartForm extends AbstractBlock { /** * Block name. * * @var string */ protected $block_name = 'add-to-cart-form'; /** * Initializes the AddToCartForm block and hooks into the `wc_add_to_cart_message_html` filter * to prevent displaying the Cart Notice when the block is inside the Single Product block * and the Add to Cart button is clicked. * * It also hooks into the `woocommerce_add_to_cart_redirect` filter to prevent redirecting * to another page when the block is inside the Single Product block and the Add to Cart button * is clicked. * * @return void */ protected function initialize() { parent::initialize(); add_filter( 'wc_add_to_cart_message_html', array( $this, 'add_to_cart_message_html_filter' ), 10, 2 ); add_filter( 'woocommerce_add_to_cart_redirect', array( $this, 'add_to_cart_redirect_filter' ), 10, 1 ); } /** * Get the block's attributes. * * @param array $attributes Block attributes. Default empty array. * @return array Block attributes merged with defaults. */ private function parse_attributes( $attributes ) { // These should match what's set in JS `registerBlockType`. $defaults = array( 'isDescendentOfSingleProductBlock' => false, ); return wp_parse_args( $attributes, $defaults ); } /** * Render the block. * * @param array $attributes Block attributes. * @param string $content Block content. * @param WP_Block $block Block instance. * * @return string | void Rendered block output. */ protected function render( $attributes, $content, $block ) { global $product; $post_id = $block->context['postId']; if ( ! isset( $post_id ) ) { return ''; } $previous_product = $product; $product = wc_get_product( $post_id ); if ( ! $product instanceof \WC_Product ) { $product = $previous_product; return ''; } ob_start(); /** * Trigger the single product add to cart action for each product type. * * @since 9.7.0 */ do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' ); $product = ob_get_clean(); if ( ! $product ) { $product = $previous_product; return ''; } $parsed_attributes = $this->parse_attributes( $attributes ); $is_descendent_of_single_product_block = $parsed_attributes['isDescendentOfSingleProductBlock']; $product = $this->add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block ); $classname = $attributes['className'] ?? ''; $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes ); $product_classname = $is_descendent_of_single_product_block ? 'product' : ''; $form = sprintf( '<div class="wp-block-add-to-cart-form wc-block-add-to-cart-form %1$s %2$s %3$s" style="%4$s">%5$s</div>', esc_attr( $classes_and_styles['classes'] ), esc_attr( $classname ), esc_attr( $product_classname ), esc_attr( $classes_and_styles['styles'] ), $product ); $product = $previous_product; return $form; } /** * Add a hidden input to the Add to Cart form to indicate that it is a descendent of a Single Product block. * * @param string $product The Add to Cart Form HTML. * @param string $is_descendent_of_single_product_block Indicates if block is descendent of Single Product block. * * @return string The Add to Cart Form HTML with the hidden input. */ protected function add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block ) { $hidden_is_descendent_of_single_product_block_input = sprintf( '<input type="hidden" name="is-descendent-of-single-product-block" value="%1$s">', $is_descendent_of_single_product_block ? 'true' : 'false' ); $regex_pattern = '/<button\s+type="submit"[^>]*>.*?<\/button>/i'; preg_match( $regex_pattern, $product, $input_matches ); if ( ! empty( $input_matches ) ) { $product = preg_replace( $regex_pattern, $hidden_is_descendent_of_single_product_block_input . $input_matches[0], $product ); } return $product; } /** * Filter the add to cart message to prevent the Notice from being displayed when the Add to Cart form is a descendent of a Single Product block * and the Add to Cart button is clicked. * * @param string $message Message to be displayed when product is added to the cart. */ public function add_to_cart_message_html_filter( $message ) { // phpcs:ignore if ( isset( $_POST['is-descendent-of-single-product-block'] ) && 'true' === $_POST['is-descendent-of-single-product-block'] ) { return false; } return $message; } /** * Hooks into the `woocommerce_add_to_cart_redirect` filter to prevent redirecting * to another page when the block is inside the Single Product block and the Add to Cart button * is clicked. * * @param string $url The URL to redirect to after the product is added to the cart. * @return string The filtered redirect URL. */ public function add_to_cart_redirect_filter( $url ) { // phpcs:ignore if ( isset( $_POST['is-descendent-of-single-product-block'] ) && 'true' == $_POST['is-descendent-of-single-product-block'] ) { if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { return wc_get_cart_url(); } return wp_validate_redirect( wp_get_referer(), $url ); } return $url; } /** * Get the frontend script handle for this block type. * * @param string $key Data to get, or default to everything. */ protected function get_block_type_script( $key = null ) { return null; } /** * Get the frontend style handle for this block type. * * @return null */ protected function get_block_type_style() { return array_merge( parent::get_block_type_style(), [ 'wc-blocks-packages-style' ] ); } /** * It isn't necessary register block assets because it is a server side block. */ protected function register_block_type_assets() { return null; } }
[+]
..
[-] CheckoutContactInformationBlock.php
[edit]
[-] MiniCartProductsTableBlock.php
[edit]
[-] ProductFilterAttribute.php
[edit]
[-] CartItemsBlock.php
[edit]
[-] MiniCartShoppingButtonBlock.php
[edit]
[-] CartOrderSummaryTaxesBlock.php
[edit]
[-] AbstractInnerBlock.php
[edit]
[-] CheckoutOrderNoteBlock.php
[edit]
[-] AddToCartForm.php
[edit]
[-] ProductCategories.php
[edit]
[-] ProductFilterStockStatus.php
[edit]
[-] CheckoutExpressPaymentBlock.php
[edit]
[-] CartExpressPaymentBlock.php
[edit]
[-] CheckoutAdditionalInformationBlock.php
[edit]
[-] ProductPrice.php
[edit]
[-] CheckoutOrderSummaryCouponFormBlock.php
[edit]
[-] AtomicBlock.php
[edit]
[-] MiniCartItemsBlock.php
[edit]
[-] ProductSaleBadge.php
[edit]
[-] CartAcceptedPaymentMethodsBlock.php
[edit]
[-] CheckoutPickupOptionsBlock.php
[edit]
[-] ProductQuery.php
[edit]
[-] AbstractProductGrid.php
[edit]
[-] ProductSummary.php
[edit]
[-] CheckoutFieldsBlock.php
[edit]
[-] CustomerAccount.php
[edit]
[-] ReviewsByCategory.php
[edit]
[-] ProductFilter.php
[edit]
[-] ClassicTemplate.php
[edit]
[-] ProductFilterActive.php
[edit]
[-] AbstractDynamicBlock.php
[edit]
[-] ProductGalleryLargeImage.php
[edit]
[-] CartOrderSummarySubtotalBlock.php
[edit]
[-] CartLineItemsBlock.php
[edit]
[-] ProductRating.php
[edit]
[-] ProductFilterPrice.php
[edit]
[-] MiniCartCheckoutButtonBlock.php
[edit]
[-] PriceFilter.php
[edit]
[-] ProductTitle.php
[edit]
[-] PageContentWrapper.php
[edit]
[-] CartCrossSellsBlock.php
[edit]
[-] ProductDetails.php
[edit]
[-] ProductGalleryThumbnails.php
[edit]
[-] ProductButton.php
[edit]
[-] RelatedProducts.php
[edit]
[-] FeaturedCategory.php
[edit]
[-] CheckoutOrderSummaryFeeBlock.php
[edit]
[-] FilledCartBlock.php
[edit]
[-] ProductReviews.php
[edit]
[-] CatalogSorting.php
[edit]
[-] MiniCartContents.php
[edit]
[-] EmptyCartBlock.php
[edit]
[-] ProductCollection.php
[edit]
[-] CheckoutBillingAddressBlock.php
[edit]
[-] CartOrderSummaryHeadingBlock.php
[edit]
[-] SingleProduct.php
[edit]
[-] ProceedToCheckoutBlock.php
[edit]
[-] CheckoutPaymentBlock.php
[edit]
[-] CheckoutTermsBlock.php
[edit]
[-] ProductGallery.php
[edit]
[-] EmptyMiniCartContentsBlock.php
[edit]
[-] ProductCollectionNoResults.php
[edit]
[-] ProductTag.php
[edit]
[-] CheckoutActionsBlock.php
[edit]
[-] CheckoutShippingMethodsBlock.php
[edit]
[-] ProductResultsCount.php
[edit]
[-] MiniCartTitleBlock.php
[edit]
[-] FilledMiniCartContentsBlock.php
[edit]
[-] AttributeFilter.php
[edit]
[-] RatingFilter.php
[edit]
[-] CartOrderSummaryDiscountBlock.php
[edit]
[-] CheckoutOrderSummarySubtotalBlock.php
[edit]
[-] HandpickedProducts.php
[edit]
[-] ProductGalleryPager.php
[edit]
[-] CheckoutOrderSummaryDiscountBlock.php
[edit]
[-] ProductImageGallery.php
[edit]
[-] CartOrderSummaryShippingBlock.php
[edit]
[-] ProductAddToCart.php
[edit]
[-] StockFilter.php
[edit]
[-] StoreNotices.php
[edit]
[-] ReviewsByProduct.php
[edit]
[-] CheckoutShippingAddressBlock.php
[edit]
[-] CartOrderSummaryBlock.php
[edit]
[-] ProductFilterRating.php
[edit]
[-] MiniCartTitleLabelBlock.php
[edit]
[+]
OrderConfirmation
[-] ProductSKU.php
[edit]
[-] ProductStockIndicator.php
[edit]
[-] ActiveFilters.php
[edit]
[-] ProductRatingCounter.php
[edit]
[-] ProductsByAttribute.php
[edit]
[-] MiniCartCartButtonBlock.php
[edit]
[-] FeaturedItem.php
[edit]
[-] MiniCartFooterBlock.php
[edit]
[-] FilterWrapper.php
[edit]
[-] ProductNew.php
[edit]
[-] CheckoutOrderSummaryTaxesBlock.php
[edit]
[-] AllReviews.php
[edit]
[-] CartCrossSellsProductsBlock.php
[edit]
[-] ProductTemplate.php
[edit]
[-] CartOrderSummaryFeeBlock.php
[edit]
[-] MiniCart.php
[edit]
[-] ProductTopRated.php
[edit]
[-] ProductGalleryLargeImageNextPrevious.php
[edit]
[-] Checkout.php
[edit]
[-] CheckoutTotalsBlock.php
[edit]
[-] Breadcrumbs.php
[edit]
[-] ProductImage.php
[edit]
[-] Cart.php
[edit]
[-] AbstractBlock.php
[edit]
[-] ProductSearch.php
[edit]
[-] CartTotalsBlock.php
[edit]
[-] AllProducts.php
[edit]
[-] MiniCartTitleItemsCounterBlock.php
[edit]
[-] CheckoutOrderSummaryBlock.php
[edit]
[-] ClassicShortcode.php
[edit]
[-] ProductOnSale.php
[edit]
[-] CheckoutOrderSummaryShippingBlock.php
[edit]
[-] ProductRatingStars.php
[edit]
[-] ProductBestSellers.php
[edit]
[-] CartOrderSummaryCouponFormBlock.php
[edit]
[-] ProductCategory.php
[edit]
[-] CheckoutOrderSummaryCartItemsBlock.php
[edit]
[-] ProductAverageRating.php
[edit]
[-] FeaturedProduct.php
[edit]
[-] CheckoutShippingMethodBlock.php
[edit]