在单个产品页面中的“添加到购物车"按钮之后添加自定义按钮 [英] Adding a custom button after add to cart button in single product pages
本文介绍了在单个产品页面中的“添加到购物车"按钮之后添加自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发基于WooCommerce的电子商务商店.
I am developing an ecommerce store based on WooCommerce.
我想在添加到购物车"按钮下方添加一个查看购物车.如果仅在成功添加购物车中至少添加了1个项目后才显示它,那就更好了:
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:
//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
}
我该如何实现?
谢谢.
推荐答案
您可以使用use WC_Cart is_empty()
方法和
You can use use WC_Cart is_empty()
method and wc_get_page_permalink()
function:
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>';
}
代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
此代码已经过测试并且可以正常工作.
This code is tested and works.
这篇关于在单个产品页面中的“添加到购物车"按钮之后添加自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文