wordpress 将 Woocommerce 添加到购物车按钮添加到相关产品和产品列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15036157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:10:16  来源:igfitidea点击:

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

wordpresswordpress-plugincartwoocommerce

提问by user1685141

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.

我在向 WooCommerce 添加其他内容时遇到了一些困难,因为我还是个新手。我正在尝试向相关产品和产品列表添加“添加到购物车”按钮。

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.

希望有人可以指导我如何添加按钮。提前致谢。

回答by Robert Lee

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

解释每个 do_action 在 woocommerce-hooks.php 中并指向 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' );

回答by Azmi Kamis

Search for woocommerce_template_loop_add_to_cartrecursively in your wordpress folder.

woocommerce_template_loop_add_to_cart在您的 wordpress 文件夹中递归搜索。

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

默认情况下,WooCommerce 将其挂接到woocommerce_after_shop_loop_itemwp-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

我安装的主题Mystile在 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.

一旦我将其注释掉,就会出现“添加到购物车”按钮。

回答by Alain Tiemblo

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'); ?>

回答by Matt Royal

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 );

回答by lise

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

在 ElegantTheme wordpress 主题 (Divi) 上使用 WooCommerce,我将此行添加到 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 )

它在标题和价格之后添加了一个“添加到购物车”按钮(即每个产品一个“添加到购物车”按钮)