在 Woocommerce 单一产品页面上将产品添加到购物车后激活自定义按钮
Activate a custom button once product is added to cart on Woocommerce single product page
在 Woocommerce 单品页面中,我使用 woocommerce_after_add_to_cart
挂钩添加了一个自定义按钮。该按钮将在单击/按下时将客户重定向到结帐处。
但是为了避免客户在将产品添加到购物车之前点击按钮,我在函数中添加了:if ( WC()->cart->get_cart_contents_count() != 0 ) {
。
我的问题是:如何让此按钮始终可用但在将产品添加到购物车之前处于非活动状态?在购物车中有产品之前,如何使其成为 "grayed out"(不可点击)?
这是我的完整代码:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
感谢您对此的任何帮助。
以下将在添加到购物车按钮后显示自定义禁用按钮......当产品添加到购物车时,产品将被启用并链接到结帐页面:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
在 Woocommerce 单品页面中,我使用 woocommerce_after_add_to_cart
挂钩添加了一个自定义按钮。该按钮将在单击/按下时将客户重定向到结帐处。
但是为了避免客户在将产品添加到购物车之前点击按钮,我在函数中添加了:if ( WC()->cart->get_cart_contents_count() != 0 ) {
。
我的问题是:如何让此按钮始终可用但在将产品添加到购物车之前处于非活动状态?在购物车中有产品之前,如何使其成为 "grayed out"(不可点击)?
这是我的完整代码:
add_action('woocommerce_after_add_to_cart_button, 'instant_checkout');
function instant_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
if ( WC()->cart->get_cart_contents_count() != 0 ) {
echo '<a href="'.$checkout_url.'" class="single_add_to_cart_button button alt">Instant Checkout</a>'; } }
感谢您对此的任何帮助。
以下将在添加到购物车按钮后显示自定义禁用按钮......当产品添加到购物车时,产品将被启用并链接到结帐页面:
add_action('woocommerce_after_add_to_cart_button', 'get_instant_checkout_buttom_html');
function get_instant_checkout_buttom_html() {
global $product;
$href = ''; // Initializing
$class = ' disabled'; // Initializing
// Continue only if cart is not empty
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ) {
// When product is in cart
if( $item['product_id'] == $product->get_id() ){
$href = ' href="'.wc_get_checkout_url().'"'; // Add the link to the button
$class = ''; // Remove "disabled" class
break; // Stop the loop
}
}
}
// Button output
echo '<a'.$href.' class="single_add_to_cart_button button alt'.$class.'">'.__("Instant Checkout").'</a>';
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。