在单个产品页面中添加到购物车按钮后添加自定义按钮

时间:2022-10-25 10:14:58

I am developing an ecommerce store based on WooCommerce.

我正在开发一个基于WooCommerce的电子商务商店。

I would like to add an View Cart Below the Add To Cart Button. It would be even better if it could only shown after adding at least 1 item in the cart successfully:

我想在添加到购物车按钮下方添加一个查看购物车。如果它只能在购物车中成功添加至少1个商品后显示,那就更好了:

//add view cart button after add to cart button

add_action('woocommerce_after_add_to_cart_button','view_cart_store');
function view_cart_store() { ?>
    <a class="button wc-forward" href="https://example.xxx/cart/"><?php _e( 'View Shopping Cart', 'woocommerce' ) ?></a>
    <?php
}

How can I achieve this?

我怎样才能做到这一点?

Thanks.

谢谢。

2 个解决方案

#1


2  

You can use use WC_Cart is_empty() method and wc_get_page_permalink() function:

您可以使用WC_Cart is_empty()方法和wc_get_page_permalink()函数:

add_action('woocommerce_after_add_to_cart_button','view_cart_store');
function view_cart_store() {
    if ( ! WC()->cart->is_empty() )
        echo '<a class="button wc-forward" href="'.wc_get_page_permalink( 'cart' ).'">'. __( 'View Shopping Cart', 'woocommerce' ) .'</a>';
}

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 and works.

此代码经过测试并可以使用。

#2


0  

The code you've posted should already be displaying the cart link beneath the add to cart button.

您发布的代码应该已经显示添加到购物车按钮下方的购物车链接。

In the example below, I'm checking the cart item count to determine whether to display a link.

在下面的示例中,我正在检查购物车项目计数以确定是否显示链接。

Another minor update I've made is to use the WC function for retrieving the cart URL.

我做的另一个小更新是使用WC功能来检索购物车URL。

function wpse_view_cart_store() {
    if ( WC()->cart->get_cart_contents_count() ) : ?>

        <a class="button wc-forward" href="<?php echo esc_url( wc_get_cart_url() ); ?>"><?php _e( 'View Shopping Cart', 'woocommerce' ) ?></a>

    <?php endif;
}
add_action( 'woocommerce_after_add_to_cart_button', 'wpse_view_cart_store' );

#1


2  

You can use use WC_Cart is_empty() method and wc_get_page_permalink() function:

您可以使用WC_Cart is_empty()方法和wc_get_page_permalink()函数:

add_action('woocommerce_after_add_to_cart_button','view_cart_store');
function view_cart_store() {
    if ( ! WC()->cart->is_empty() )
        echo '<a class="button wc-forward" href="'.wc_get_page_permalink( 'cart' ).'">'. __( 'View Shopping Cart', 'woocommerce' ) .'</a>';
}

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 and works.

此代码经过测试并可以使用。

#2


0  

The code you've posted should already be displaying the cart link beneath the add to cart button.

您发布的代码应该已经显示添加到购物车按钮下方的购物车链接。

In the example below, I'm checking the cart item count to determine whether to display a link.

在下面的示例中,我正在检查购物车项目计数以确定是否显示链接。

Another minor update I've made is to use the WC function for retrieving the cart URL.

我做的另一个小更新是使用WC功能来检索购物车URL。

function wpse_view_cart_store() {
    if ( WC()->cart->get_cart_contents_count() ) : ?>

        <a class="button wc-forward" href="<?php echo esc_url( wc_get_cart_url() ); ?>"><?php _e( 'View Shopping Cart', 'woocommerce' ) ?></a>

    <?php endif;
}
add_action( 'woocommerce_after_add_to_cart_button', 'wpse_view_cart_store' );