Adding Woocommerce Add to Cart Button to related products and product listing

主宰稳场 提交于 2019-12-22 07:47:11

问题


I'm having some difficulty in adding additional stuffs to WooCommerce as I'm still new to it. I'm trying to add an 'add to cart' button to related products and product listing.

Was running through the codes and got stuck at the below.

<a href="<?php the_permalink(); ?>">

        <?php
            /**
             * woocommerce_before_shop_loop_item_title hook
             *
             * @hooked woocommerce_show_product_loop_sale_flash - 10
             * @hooked woocommerce_template_loop_product_thumbnail - 10
             */
            do_action( 'woocommerce_before_shop_loop_item_title' );
        ?>

        <h3><?php the_title(); ?></h3>

        <?php
            /**
             * woocommerce_after_shop_loop_item_title hook
             *
             * @hooked woocommerce_template_loop_price - 10
             */
            do_action( 'woocommerce_after_shop_loop_item_title' );
        ?>

    </a>

    <?php do_action( 'woocommerce_after_shop_loop_item' ); ?>  

Hope someone can guide me on how to add the button. Thanks in advance.


回答1:


To explain each do_action is inside the woocommerce-hooks.php and points to a Function inside of woocommerce-template.php

Creates thumbnail:

Function Name: woocommerce_template_loop_product_thumbnail()

do_action( 'woocommerce_before_shop_loop_item_title' );

Provides Price:

Function Name: woocommerce_template_loop_price()

do_action( 'woocommerce_after_shop_loop_item_title' );

Add to Cart Button:

Function Name: woocommerce_template_loop_add_to_cart()

do_action( 'woocommerce_after_shop_loop_item' );



回答2:


Search for woocommerce_template_loop_add_to_cart recursively in your wordpress folder.

By default WooCommerce hooks it to woocommerce_after_shop_loop_item in wp-content\plugins\woocommerce\woocommerce-hooks.php

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

My installed theme, Mystile, removed this hook in wp-content\themes\mystile\includes\theme-woocommerce.php

// Remove add to cart button on archives
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);

Once I commented it out, the "Add to cart" button appeared.




回答3:


To add "add to cart" button to the product listing page, I personally copied :

wp-content/plugins/woocommerce/templates/content-product.php

Into :

wp-content/themes/myChildTemplate/woocommerce/content-product.php

I then replaced:

        do_action( 'woocommerce_after_shop_loop_item_title' );
    ?>

</a>

By:

        do_action( 'woocommerce_after_shop_loop_item_title' );
    ?>

</a>
    <?php do_action('woocommerce_simple_add_to_cart'); ?>



回答4:


FYI for anyone who comes across this, you could also try using this hook to get it working...

// Add add to cart button on archive page products
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10 );



回答5:


Using WooCommerce on a ElegantTheme wordpress theme (Divi) , I added this line to functions.php :

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

It adds an "Add to Cart" button right after the title and price (i.e one 'Add to Cart' button per product )



来源:https://stackoverflow.com/questions/15036157/adding-woocommerce-add-to-cart-button-to-related-products-and-product-listing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!