[英]Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3
I am using woocommerce and I have the following issue:我正在使用 woocommerce,但遇到以下问题:
An important thing is to allow customers to be able to read the description of the product before adding it to cart.重要的是让客户能够在将产品添加到购物车之前阅读产品描述。
Is there a way to replace add to cart button with read more in order to redirect from homepage to each product's page where the add to cart button will appear?有没有办法用阅读更多替换添加到购物车按钮,以便从主页重定向到每个产品页面,添加到购物车按钮将出现在那里?
Replacing the button add to cart by a link to the product in Shop and archives pages for woocommerce 3+: 更换按钮添加到购物车的商店和档案页面中的产品链接为woocommerce 3+:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
This code is tested on WooCommerce 3+ and works. 此代码在WooCommerce 3+上进行测试并且有效。 You can customize the text of button and you will get something like:
您可以自定义按钮的文本,您将获得以下内容:
Add these to your functions.php file in the theme folder 将这些添加到theme文件夹中的functions.php文件中
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
In case it helps, this worked for me to apply this solution only to simple products, since variable products don't "add to cart" on archive pages anyway. 如果它有帮助,这对我来说只能将这个解决方案应用于简单的产品,因为变量产品无论如何都不会在存档页面上“添加到购物车”。 This way it can be clearer that a product has more options (if it is variable).
这样可以更清楚地看到产品有更多选项(如果它是可变的)。 I also changed the text of "select options" to "more options" in the example below (since not all products in my case would be purchasable even after viewing the single product page's url, which is another non-topical idea for this thread):
我还在下面的示例中将“选择选项”的文本更改为“更多选项”(因为即使在查看单个产品页面的网址后,我的情况下并非所有产品都可以购买,这是该主题的另一个非主题创意) :
// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
$text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );
/**
* remove add to cart buttons on shop archive page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->is_type( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' .
$button_text . '</a>';
}
return $button;
}
I got the same problem i had a donation product, so it had a custom price ie you could make any amount of donation so in the shop page i replaced the add to cart button for that product to "Details" which would redirect it to product single page from where user could make any donation. 我有同样的问题,我有一个捐赠产品,所以它有一个自定义的价格,即你可以进行任何数量的捐赠,所以在商店页面我将该产品的添加到购物车按钮替换为“详细信息”,将其重定向到产品用户可以进行任何捐赠的单页。 I used this code.
我用过这段代码。 The code goes in your theme's or child theme's functions.php file
代码放在主题或子主题的functions.php文件中
function filter_woocommerce_loop_add_to_cart_link( $quantity,$product ) {
$product_id = $product->get_id();
$title = $product->get_title();
$sku = $product->get_sku();
if($product_id == get_option( 'woocommerce_donations_product_id' )){
//var_dump($title);
//var_dump($sku);
//var_dump($quantity);
$simpleURL = get_permalink();
//var_dump($simpleURL);
$quantity='<a href="'.$simpleURL.'" data-quantity="1" class="product_type_simple ajax_add_to_cart" data-product_id="'.$product_id.'" data-product_sku="'.$sku.'" aria-label="Read more about “'.$title.'”" rel="nofollow"><span class="filter-popup">Détails</span></a>';
//var_dump($quantity);
}
return $quantity;
//exit();
};
// add the filter
add_filter( 'woocommerce_loop_add_to_cart_link','filter_woocommerce_loop_add_to_cart_link', 10, 2 );
this code is working.. but at this place for my theme woocommerce/include/class-wc-product-simple.php thank you..这段代码正在工作..但在这个地方为我的主题woocommerce/include/class-wc-product-simple.php谢谢..
// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
$text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );
/**
* remove add to cart buttons on shop archive page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->is_type( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' .
$button_text . '</a>';
}
return $button;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.