skip to Main Content

I’m trying to figure out where these functions are called within the WooCommerce products loop:

function woocommerce_simple_add_to_cart()
function woocommerce_variable_add_to_cart()

These functions load the ‘single-product/add-to-cart/simple.php’ template and the ‘single-product/add-to-cart/variable.php’ template, respectively.

I thought the content-product.php template might call these functions, but this template goes directly from:

do_action( 'woocommerce_after_shop_loop_item_title' );

to

do_action( 'woocommerce_after_shop_loop_item' );

The template for the shop loop item itself must be generated somewhere else in WooCommerce.

2

Answers


  1. Chosen as BEST ANSWER

    The simple and variable add_to_cart functions that load the templates:

    'single-product/add-to-cart/simple.php'

    and

    'single-product/add-to-cart/variable.php'

    are called by the 'woocommerce_template_single_add_to_cart' function with a variable in the function name, like this:

    do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
    

    The 'woocommerce_template_single_add_to_cart' function is hooked to the 'woocommerce_single_product_summary' function which is called in the template file 'content-single-product.php' here:

    <div class="summary entry-summary">
        <?php
        /**
         * Hook: woocommerce_single_product_summary.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         * @hooked WC_Structured_Data::generate_product_data() - 60
         */
        do_action( 'woocommerce_single_product_summary' );
        ?>
    </div>
    

  2. For WooCommerce products loop, add to cart is called via woocommerce_after_shop_loop_item action hook on content-product.php template file (lines 59 to 65), you have:

        /**
         * Hook: woocommerce_after_shop_loop_item.
         *
         * @hooked woocommerce_template_loop_product_link_close - 5
         * @hooked woocommerce_template_loop_add_to_cart - 10
         */
        do_action( 'woocommerce_after_shop_loop_item' );
    

    So woocommerce_template_loop_add_to_cart() function is hooked in at priority 10, that is called via includes/wc-template-hooks.php file (line 96):

    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    

    Now we can take a look to woocommerce_template_loop_add_to_cart() function itself located on includes/wc-template-hooks.php file (from line 1339 to 1386):

    if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
    
        /**
         * Get the add to cart template for the loop.
         *
         * @param array $args Arguments.
         */
        function woocommerce_template_loop_add_to_cart( $args = array() ) {
            global $product;
    
            if ( $product ) {
                $defaults = array(
                    'quantity'   => 1,
                    'class'      => implode(
                        ' ',
                        array_filter(
                            array(
                                'button',
                                wc_wp_theme_get_element_class_name( 'button' ), // escaped in the template.
                                'product_type_' . $product->get_type(),
                                $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                                $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                            )
                        )
                    ),
                    'attributes' => array(
                        'data-product_id'  => $product->get_id(),
                        'data-product_sku' => $product->get_sku(),
                        'aria-label'       => $product->add_to_cart_description(),
                        'aria-describedby' => $product->add_to_cart_aria_describedby(),
                        'rel'              => 'nofollow',
                    ),
                );
    
                $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
    
                if ( ! empty( $args['attributes']['aria-describedby'] ) ) {
                    $args['attributes']['aria-describedby'] = wp_strip_all_tags( $args['attributes']['aria-describedby'] );
                }
    
                if ( isset( $args['attributes']['aria-label'] ) ) {
                    $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
                }
    
                wc_get_template( 'loop/add-to-cart.php', $args );
            }
        }
    }
    

    At the end of the function code, you can se that loop/add-to-cart.php template file is called…

    This should answer your question.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search