2016-12-26 47 views
3

在WooCommerce中,如果購物車在結賬頁面上有特定產品(ID),我正在尋找更改「下訂單」文本的功能。如果購物車中有特定商品,請點擊「下訂單」

這對於銷售產品的佑店很有用,同時提供不同的服務,例如會員資格。這將使地方訂單文本更具描述性地將產品描述爲Call to Action按鈕。

我基於特定產品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'); 
} 

進出口尋找如何適應他們結帳頁面。

謝謝。

回答

1

如果我有很好的理解你的問題,你有以下的自定義功能,顯示在結帳自定義文本提交按鈕,當特定的產品在購物車:

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

+0

您好,感謝您的答覆。功能適用於特定產品。即,如果在購物車項目中找到特定產品,我們可以顯示自定義結賬按鈕。但是,對於非特定產品,我們無法在結帳按鈕上顯示任何文本。所以我們有其他產品的結帳按鈕文本。我該如何解決這個問題?再次感謝你。 –

+0

@OnurK。我已經更新了我的答案...現在按鈕不會是空的:) – LoicTheAztec

+0

非常感謝。功能workin現在完美:) –

相關問題