如果产品在WooCommerce上的购物车中,请更改添加到购物车文本

时间:2019-05-25 作者:its4yougr

在wooCommerce中,当产品在购物车中时,我正在更改“添加到购物车”按钮文本,代码如下(添加到functions.php “我的孩子”主题)这只适用于简单的产品类型:

add_filter( \'woocommerce_product_single_add_to_cart_text\', \'woocommerce_custom_add_cart_button_single_product\' );
function woocommerce_custom_add_cart_button_single_product( $label ) {
   foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
      $product = $values[\'data\'];
      if( get_the_ID() == $product->get_id() ) {
         $label = __(\'Already in Cart. Add again?\', \'woocommerce\');
      }
   }
   return $label;
}
// Edit Loop Pages Add to Cart
add_filter( \'woocommerce_product_add_to_cart_text\', \'woocommerce_custom_add_cart_button_loop\', 99, 2 );
function woocommerce_custom_add_cart_button_loop( $label, $product ) {
   if ( $product->is_purchasable() && $product->is_in_stock() ) {
      foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
         $_product = $values[\'data\'];
         if( get_the_ID() == $_product->get_id() ) {
            $label = __(\'Already in Cart. Add again?\', \'woocommerce\');
         }
      }
   }
   return $label;
}
如何使其也适用于可变产品类型(及其产品变体)?

1 个回复
最合适的回答,由SO网友:LoicTheAztec 整理而成

以下内容适用于简单产品和具有产品变体的可变产品。

它需要jQuery根据所选的可变产品的变化来更改“添加到购物车”按钮文本。

我已经完全重新访问了您的初始代码。

// Conditional function that checks if a product is in cart and return the correct button text
function change_button_text( $product_id, $button_text ) {
    foreach( WC()->cart->get_cart() as $item ) {
        if( $product_id === $item[\'product_id\'] ) {
            return __(\'Already in Cart. Add again?\', \'woocommerce\');
        }
    }
    return $button_text;
}

// Archive pages: For simple products (ajax add to cart button)
add_filter( \'woocommerce_product_add_to_cart_text\', \'change_ajax_add_to_cart_button_text\', 10, 2 );
function change_ajax_add_to_cart_button_text( $button_text, $product ) {
    if ( $product->is_type(\'simple\') ) {
        $button_text = change_button_text( $product->get_id(), $button_text );
    }
    return $button_text;
}

// Single product pages: Simple and external products
add_filter( \'woocommerce_product_single_add_to_cart_text\', \'change_single_add_to_cart_button_text\', 10, 2 );
function change_single_add_to_cart_button_text( $button_text, $product ) {
    if (  ! $product->is_type(\'variable\') ) {
        $button_text = change_button_text( $product->get_id(), $button_text );
    }
    return $button_text;
}

// Single product pages: Variable product and its variations
add_action( \'woocommerce_after_variations_form\', \'action_after_variations_form_callback\' );
function action_after_variations_form_callback() {
    global $product;

    // Get the produc variation Ids for the variable product
    $children_ids = $product->get_visible_children();

    $ids_in_cart  = [];

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $item ) {
        if( in_array( $item[\'variation_id\'], $children_ids ) ) {
            $ids_in_cart[] = $item[\'variation_id\'];
        }
    }
    ?>
    <script type="text/javascript">
    jQuery(function($){
        var b = \'button.single_add_to_cart_button\',
            t = \'<?php echo $product->single_add_to_cart_text(); ?>\';

        $(\'form.variations_form\').on(\'show_variation hide_variation found_variation\', function(){
            $.each(<?php echo json_encode($ids_in_cart); ?>, function(j, v){
                var i = $(\'input[name="variation_id"]\').val();
                if(v == i && i != 0 ) {
                    $(b).html(\'<?php _e(\'Already in Cart. Add again?\', \'woocommerce\'); ?>\');
                    return false;
                } else {
                    $(b).html(t);
                }
            });
        });
    });
    </script>
    <?php
}
代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。

选择变量时,对变量产品执行以下操作:

如果variation is not in cart (文本不变)

enter image description here

<如果variation is in cart (文本更改)

enter image description here