doule6314 2016-12-26 23:33
浏览 53
已采纳

如果购物车有特定产品,请更改结帐“下订单”文本

In WooCommerce, I am looking for a function to change the "Place Order" text if cart has a specific product(ID) on checkout page.

This is useful for woo shops selling product and at the same time offering different services for example memberships. This will make the place order text more descriptive of the product as a Call to Action button.

I founded that function for change ‘add to cart' button text on single product page based on specific product id

add_filter( 'woocommerce_product_single_add_to_cart_text',
'woo_custom_cart_button_text' ); 

function woo_custom_cart_button_text( $text ) {
global $product;

if ( 123 === $product->id ) {
   $text = 'Product 123 text';
}
return $text;
}

And changing the place order text globally;

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' ); 

function woo_custom_order_button_text() {
    return __( 'Your new button text here', 'woocommerce' ); 
}

Im looking for how to adapt them for checkout page.

Thanks.

  • 写回答

1条回答 默认 最新

  • dtdr84101 2016-12-27 01:00
    关注

    If I have well understood your question, you got below a custom function that will display a custom text on Checkout submit button, when a specific product is in the cart:

    add_filter( 'woocommerce_order_button_text', 'custom_checkout_button_text' );
    function custom_checkout_button_text() {
    
        // Set HERE your specific product ID
        $specific_product_id = 37;
        $found = false;
    
        // Iterating trough each cart item
        foreach(WC()->cart->get_cart() as $cart_item)
            if($cart_item['product_id'] == $specific_product_id){
                $found = true; // product found in cart
                break; // we break the foreach loop
            }
    
        // If product is found in cart items we display the custom checkout button
        if($found)
            return __( 'Your new button text here', 'woocommerce' ); // custom text Here
        else
            return __( 'Place order', 'woocommerce' ); // Here the normal text
    }
    

    Code goes in function.php file of your active child theme (active theme or in any plugin file).

    This code is tested and works.


    Similar answers (multiple product IDs): WooCommerce - Check if item's are already in cart

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?