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

Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3

我正在使用 woocommerce,但遇到以下问题:

重要的是让客户能够在将产品添加到购物车之前阅读产品描述。

有没有办法用阅读更多替换添加到购物车按钮,以便从主页重定向到每个产品的页面,添加到购物车按钮将出现?

将这些添加到主题文件夹中的 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');

将添加到购物车的按钮替换为 link 到 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;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已在 WooCommerce 3+ 上测试并有效。你可以自定义按钮的文字,你会得到这样的东西:

万一它有帮助,这对我来说只适用于简单产品,因为可变产品不会 "add to cart" 在存档页面上。这样可以更清楚地知道一个产品有更多的选择(如果它是可变的)。我还在下面的示例中将 "select options" 的文本更改为 "more options"(因为即使在查看单个产品页面的 url 之后,我的案例中并非所有产品都可以购买,这是另一个非此线程的主题想法):

// 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;
}

我遇到了同样的问题,我有一个捐赠产品,所以它有一个自定义价格,即你可以进行任何数量的捐赠,所以在商店页面我将该产品的添加到购物车按钮替换为 "Details" 这会将其重定向到产品单页,用户可以从中进行任何捐赠。我使用了这段代码。 代码进入您的主题或子主题的 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 );

此代码有效。但是在这个地方我的主题 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;
}