当前位置: 首页 > 知识库问答 >
问题:

将“添加到购物车”按钮文本更改为“当产品已在购物车中且产品标签为buynow时添加”

童宏富
2023-03-14

我想更改添加到购物车文本添加时,产品已经在购物车和更改添加到购物车按钮文本立即购买时,产品标签是buynow。

这是我的密码:

/**
 * Change the add to cart text on single product pages
 */
add_filter('woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text');
function woo_custom_cart_button_text() {
global $woocommerce;    
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( get_the_ID() == $_product->id ) {
            //return __('✓ Added', 'woocommerce');
            return __('Added', 'woocommerce');
        }
    }

    //return __('Add to cart', 'woocommerce');
    if ( has_term( 'buynow', 'product_tag', $_product->id ) ) :
            return __( 'Buy Now', 'woocommerce' );
        else:
            return __( 'Add to Cart', 'woocommerce' );
        endif;
}
/**
 * Change the add to cart text on product archives
 */
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );
function woo_archive_custom_cart_button_text() {
global $woocommerce;    
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( get_the_ID() == $_product->ID ) {

            //return __('✓ Added', 'woocommerce');
            return __('Added', 'woocommerce');
        }
    }

    //return __('Add to cart', 'woocommerce');
    if ( has_term( 'buynow', 'product_tag', $_product->ID ) ) :
            return __( 'Buy Now', 'woocommerce' );
        else:
            return __( 'Add to Cart', 'woocommerce' );
        endif;
}

我的代码适用于单个产品页面,但不适用于产品归档页面。

任何人对此有解决方案/建议,请分享。提前谢谢。

共有2个答案

江丰羽
2023-03-14

最后,它的工作

这是我放在woocommerce/loop/add-to-cart中的代码。php

global $product;

foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( get_the_ID() == $_product->id ) {
            $incart[] = $_product->id;
        }
    }
if (in_array($product->id, $incart, true)) {
    $output = 'Added';
}elseif(in_array($product->id, $incart, true) && has_term( 'buynow', 'product_tag', $_product->id )){
     $output = 'Added';
}elseif ( has_term( 'buynow', 'product_tag', $product->id ) ){
    $output = 'Buy Now';
}else{
$output = 'Add to cart';
}
刘翔宇
2023-03-14

对于对象$\u产品,使用id而不是id

尝试使用var\u dump()查看比较的值。您可能已经看到了$\u产品-

 类似资料:
  • 我想在单个产品页面中向“添加到购物车”按钮显示产品价值(版本) 我找到了必须编辑的文件:woocommerce\templates\single-product\add-to-cart\external.php 但现在我的“添加到购物车”按钮上什么也没有显示

  • 我经营一家Woocommerce商店,该商店也提供免费产品(_常规价格=0)。客户必须选择数量并将其添加到购物车中,然后下订单才能收到产品。但这并不是Woocommerce的工作原理,它隐藏了所有价格为0的产品的“添加到购物车”链接。并且不会在购物车页面中显示它们。有没有解决这个问题的办法?非常感谢。

  • 我是新来的。我想将产品添加到购物车并列出购物车的所有产品。我已经将Co-Cart插件添加到我的服务器,并且我正在使用Co-Cart API实现购物车相关功能。 我面临的问题是,我不能在我的反应本地应用程序中查看购物车产品。以下是我现在使用的API: 1.将产品添加到购物车: 方法:邮寄 URL:https://www.myhost.com/wp-json/wc/v2/cart/add?token=

  • 我使用“从Woocommerce中的functions.php文件中添加到购物车按钮上显示价格”的答案代码,在简单的产品中添加价格。 但我想改进这个功能,动态显示选择的变异价格内按钮。在变体产品页面上使用此代码,它只显示最低的价格。有什么解决办法吗?

  • 在Woocommerce中,我试图将添加到购物车的文本从“添加到购物车”更改为“缺货”,当产品缺货时,对于简单的产品和可变产品的产品变化。

  • 我试图隐藏的添加到购物车按钮在所有的产品,除了一个可变的产品,我有。我已经尝试了以下留下变量选择选项(这是我想要的),但它隐藏了添加到购物车按钮(我不想要)。 有办法做到这一点吗? 我所有的产品都是简单的产品,除了这个单一的变量产品,所以也许有一个功能可以隐藏除了变量之外的所有简单产品的购物车按钮?