简体   繁体   English

如何在 WooCommerce 的单页上更改特定变量产品的添加到购物车按钮文本

[英]How to change add to cart button text for specific variable products on single page in WooCommerce

I have a few variable products on my website.我的网站上有一些可变产品。 Among those only on a few specific products, I have added two variations on those products one is "carbon fiber" and other is "unpainted" now what I want is when user clicks on 2nd variation which is "unpainted" the text of add to cart button should change from "add to cart" button to "made to order. I have updated my question to be more specific Below is the code First part code is jQuery to display the variations on my single product page and the second to change the name of variations for specific products:在那些仅针对少数特定产品的产品中,我在这些产品上添加了两个变体,一个是“碳纤维”,另一个是“未上漆”,现在我想要的是当用户单击“未上漆”的第二个变体时添加的文本购物车按钮应从“添加到购物车”按钮更改为“定制”按钮。我已将我的问题更新为更具体 下面是代码 第一部分代码是 jQuery,用于在我的单个产品页面上显示变体,第二部分用于更改特定产品的变体名称:

 window.addEventListener('load', (event) => {



var initialvalue=jQuery('#pa_choose-option').val();
if(initialvalue=="carbon-fibre")
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To 
Cart");

jQuery('#pa_choose-option').on('change', function() {
console.log("here");
var initialvalue=jQuery('#pa_choose-option').val();
if(initialvalue!="carbon-fibre")
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Made 
to Order");
 else
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To 
Cart");

});


});


add_filter( 'woocommerce_product_single_add_to_cart_text', 
'custom_addtocart_button_text', 10, 2 ); 
function custom_addtocart_button_text( $button_text, $product ) {
// 1st product
if ( $product->get_id() == 6577 ) {
    $button_text = __( 'jawad', 'woocommerce' );
} 
// 2nd product
elseif ( $product->get_id() == 6570 ) {
    $button_text = __( 'kiani', 'woocommerce' );
}
return $button_text;
}

You can use show_variation trigger.您可以使用show_variation触发器。 try the below code.试试下面的代码。

function change_add_to_cart_text_based_on_variation(){
    global $post;
    
    $specific_ids = array( 6594, 6577 );

    if( is_product() && in_array( $post->ID, $specific_ids ) ){
        ?>
        <script type="text/javascript">
            (function($){
                $( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
                    if( variation.attributes['attribute_pa_choose-option'] == 'unpainted' ){
                        jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Made to Order" );
                    }else{
                        jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Add To Cart" );
                    }
                } );

                $( document ).on('click', '.reset_variations', function(event) {
                    event.preventDefault();
                    jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To Cart");
                });

            })(jQuery);
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'change_add_to_cart_text_based_on_variation', 10, 1 );

Tested and works.测试和工作。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 Woocommerce 中特定产品的添加到购物车按钮文本 - Change Add to cart button text for specific products in Woocommerce 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page 如果用户在 WooCommerce 中购买了特定产品,则更改“添加到购物车”文本 - Change “add to cart” text if a user has bought specific products in WooCommerce WooCommerce - 更改 40 英镑或更多产品的“添加到购物车”按钮文本 - WooCommerce - Change Add to Cart button text for products £40 or more WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products 隐藏WooCommerce变量产品上的“添加到购物车”按钮 - Hiding 'Add to Cart' button on WooCommerce variable products Ajax 加入购物车 WooCommerce 单品上的简单和可变产品 - Ajax Add to cart for simple and variable products on WooCommerce single products woocommerce 添加到单个产品页面上的购物车按钮 - woocommerce add to cart button on single product page 在WooCommerce中为特定产品添加额外添加到购物车按钮 - Add extra add to cart button to specific products in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM