![](/img/trans.png)
[英]How to hide add to cart button specific role and product category Woocommerce
[英]How to hide / remove add to cart button in Woocommerce after adding the product to the cart
再会,
在此之前,我在这里搜索了查询的解决方案,但失败了。
将产品添加到购物车后,我需要隐藏/删除 Woocommerce 中的添加到购物车按钮,而不影响价格、数量和产品类型(简单、变量、分组等)。 一般来说,想法是在单个产品页面中添加结帐表格。
您可以从这里访问该站点进行测试
我使用以下主题和插件:
我进行了以下更改:
1 - 在商店页面重定向添加到购物车
我将“添加到购物车”按钮的文本更改为“查看产品”并将此按钮重定向到单个产品页面,这是使用的代码
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;
}
2 - 更改单个产品页面中的添加到购物车文本
我在单个产品页面中将文本“添加到购物车”更改为“立即购买”,这是使用的代码
// Code Start
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' ); // Replace "Buy Now" text with your own text
}
// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' ); // Replace "By Now" text with your own text
}
// Code End
3 - 默认情况下 woocommerce 的简短描述
按下立即购买按钮后,我已将结帐页面添加到产品页面,这是使用的代码
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text="[woocommerce_checkout]";
return $description.$text;
}
4 - 如果用户到达主页(在我的情况下为商店页面),则清空购物车
我在进入主页时添加了一个代码来清空购物车(在我的情况下,主页是商店页面),这是使用的代码
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}
现在我想做的是,在按下立即购买按钮(以前,添加到购物车)并出现结帐页面表单字段后,立即购买按钮被隐藏或删除(以前,添加到购物车)而不影响产品类型或还要别的吗。
我希望能得到您的帮助或任何推荐的建议。 最好的问候
好的,有多种方法可以做到这一点。 首先,您必须检查产品是否在购物车中。 为此,您可以循环购物车项目并将它们与产品 ID 进行比较。 像这样创建一个 function。
function is_product_in_cart( $product_id ) {
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
return $in_cart;
}
所以第一种方法是你必须在你的活动主题中覆盖 woocommerce 模板。 为了从这里复制文件
wp-content\plugins\woocommerce\templates\single-product\add-to-cart.php
并粘贴在这里
wp-content\themes\your-active-theme-name\woocommerce\single-product\add-to-cart.php
您将在 add-to-cart.php 中看到代码,如下所示。
<?php
/**
* Simple product add to cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/simple.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files, and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
global $product;
if ( ! $product->is_purchasable() ) {
return;
}
echo wc_get_stock_html( $product ); // WPCS: XSS ok.
if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?>
现在使用 function is_product_in_cart
修改代码,如下所示
<?php
/**
* Simple product add to cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/simple.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files, and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
global $product;
if ( ! $product->is_purchasable() ) {
return;
}
echo wc_get_stock_html( $product ); // WPCS: XSS ok.
if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
if( !is_product_in_cart( $product->get_id() ) ){ ?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?>
and that's it now once you have added a product into the cart add to cart button will hide.
对于变量、分组和外部类型等其他类型,您必须遵循相同的原则。 您将在我上面提到的同一文件夹中找到所有这些文件,并且您必须按照与上述相同的步骤进行操作。
现在是第二种方式。
在页面加载时,您可以使用 CSS 隐藏按钮并使用相同的 function。 检查下面的代码。 这将在您的活动主题functions.php文件中编码go。
function hide_add_to_cart_button(){
if( is_product() ){
if( is_product_in_cart( get_the_ID() ) ){
?>
<style type="text/css">
.single_add_to_cart_button{display: none;}
</style>
<?php
}
}
}
add_action('wp_head','hide_add_to_cart_button');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.