php - 如果购物车有特定产品,则更改结帐 "Place Order"文本

标签 php wordpress woocommerce checkout hook-woocommerce

在 WooCommerce 中,如果购物车在结帐页面上有特定的产品(ID),我正在寻找一种功能来更改“下订单”文本。

这对于吸引销售产品并同时提供不同服务(例如成员(member)资格)的商店很有用。这将使下订单文本作为号召性用语按钮更能描述产品。

我创建了根据特定产品 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;
}

全局更改下单文本;

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' ); 

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

我正在寻找如何使它们适应结帐页面

谢谢。

最佳答案

如果我很好地理解了您的问题,您将在下面看到一个自定义函数,当购物车中有特定产品时,该函数将在结帐提交按钮上显示自定义文本:

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
}

代码位于事件子主题(事件主题或任何插件文件)的 function.php 文件中。

此代码已经过测试并且可以工作。


相似的答案(多个产品 ID): WooCommerce - Check if item's are already in cart

关于php - 如果购物车有特定产品,则更改结帐 "Place Order"文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337290/

相关文章:

php - 如何在codeigniter中将包含html标签的csv文件导入MYSQL

php - 显示 WooCommerce 中的总客户评论和平均评分

php - Woocommerce/Wordpress - 添加按钮到管理面板上的订单/产品页面

php - Woocommerce 手动添加订单 : How to filter products in ajax call

php - 尝试导入sql文件,但遇到此错误

php - 为什么我的 Drupal 间歇性变慢? Stracing 显示大量重复的 sendto 调用

php - 在 WordPress 中为标签创建一个复选框

wordpress - Woocommerce 单一产品税收状况胜过 WooTax 和 Tax Cloud

php - Laravel 5 重复 ID

php - 将产品数据添加到 WooCommerce 管理订单预览