简体   繁体   English

将添加到购物车按钮替换为链接到 WooCommerce 3 商店页面上的产品页面的更多信息

[英]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,但遇到以下问题:

  • The products are displayed in the homepage with their price and add to cart button.产品与其价格一起显示在主页上并添加到购物车按钮。
  • Add to cart button redirects to cart page.添加到购物车按钮重定向到购物车页面。
  • The image of each product redirects to product page.每个产品的图像重定向到产品页面。

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.

相关问题 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category 用 WooCommerce 商店、购物车和结帐页面中的 SKU 替换产品标题 - Replace product title by the SKU in WooCommerce shop, cart and checkout pages 如何在自定义页面(不是商店/产品页面)上添加到购物车按钮 Woocommerce - How to make add to cart button on custom page (not shop/product page) Woocommerce Woocommerce - 将商店页面上的“添加到购物车”重定向到产品页面? - Woocommerce - Redirect Add-to-Cart on Shop page to Product Page? 仅在woocommerce商店/类别页面上隐藏“添加到购物车”按钮 - Hide 'add to cart' button ONLY on woocommerce shop/category pages Woocommerce商店页面(类别页面模板)添加到购物车按钮行为 - Woocommerce shop page (category page template) Add to cart button behavior 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page 在 Woocommerce 3 商店页面上的添加到购物车下添加额外按钮 - Add Extra button under Add to cart on shop page of Woocommerce 3 在WooCommerce商店页面中产品下方添加产品URL按钮 - Add product URL button below product in WooCommerce shop page 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM