2017-02-19 64 views
2

我有一個小問題,我還不能解決。 我有這個WooCommerce web site可變產品,目前的價格都以這種方式顯示:根據產品的類型添加自定義文本標籤到產品價格

$每打5.50 - $ 100.00每打

我用這個CSS規則,每一個價格後,「每打」補充說,但在目前的情況下沒有意義。

.price .amount:after { 
content: " per dozen"; 
} 

我想顯示在這個變型產品的價格是這樣的:

$ 5.50 每打 - $ 100.00 每箱(數量數)

在此先感謝您的幫助。

+0

會發生什麼事和你想怎麼發生的呢? –

+0

你在我的問題中檢查了鏈接嗎?目前有 $每打5.50 - 每打 $ 100.00,並希望像顯示每打 $ 5.50 - $ 100.00每箱 瞭解,價格爲一塊,並批量價格。 – Dora

+2

我無法檢查外部鏈接。你需要提供一個[mcve] –

回答

5

在這裏你將可以添加自定義標籤,就像你想使用woocommerce_price_htmlwoocommerce_variation_price_html過濾器鉤子鉤住(適用於簡單和變量的產品定製功能。

對於最小/最大價格變量的產品,我們需要在woocommerce_variation_sale_price_html過濾鉤子勾住了分離功能

更新:因爲我的代碼現在也會在單個產品上處理「每打」,您必須刪除您的自定義CSS規則.price .amount:after { content: " per dozen";}

這將避免有重複「每打」無處不在。

但它不是可以設置不同的標籤上根據所選擇的屬性值,實時價格。對於使用使用Javascript/jQuery的,因爲這是在客戶端的實時事件的唯一途徑...

UPDATE2
這裏是工作和測試代碼(見截圖年底)

add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2); 
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2); 
function prices_custom_labels($price, $product){ 

    // Custom label name 
    $per_dozen = ' '. __('per dozen', 'woocommerce'); 

    // Set HERE your "quantity" attribute slug 
    $attribute_qty_slug = 'pa_quantity'; 

    $attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug; 
    $append_label = ''; 

    // 1) Variable products 
    if ($product->product_type != 'simple' && $product->variation_id) { 

     // Getting the attribute "quantity" value 
     $attribute_qty_is_set = $product->variation_data[$attribute_qty_slug_key]; 

     // if "quantity" not set we display " per dozen" 
     if(! $attribute_qty_is_set) 
      $append_label = $per_dozen; 


     // Outputed price + custom label 
     $price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$append_label.'</ins>'; 
    } 
    // 2) Simple products 
    else 
    { 
     // Here the output price + custom default label 
     $price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$per_dozen.'</ins>'; 
    } 
    return $price; 
} 

add_filter('woocommerce_variable_price_html', 'prices_custom_labels_min_max', 20, 2); 
function prices_custom_labels_min_max($price, $product) { 

    // Custom label name 
    $per_dozen = ' '. __('per dozen', 'woocommerce'); 
    $per_case = ' '. __('per case', 'woocommerce'); 

    // Set HERE your quantity attribute slug 
    $attribute_qty_slug = 'pa_quantity'; 


    // Getting the min and max variations prices 
    $variation_min_reg_price = $product->get_variation_regular_price('min', true); 
    $variation_max_reg_price = $product->get_variation_regular_price('max', true); 
    $variation_reg_price = $product->get_variation_regular_price(); 


    if($variation_min_reg_price == $variation_max_reg_price) 
    { 
     $price = '<ins class="highlight">'.woocommerce_price($variation_reg_price) . $per_dozen . '</ins>'; 
    } 
    else 
    { 
     if(!in_array($attribute_qty_slug, array_keys($product->get_attributes()))) 
     { 
      $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_dozen . '</ins>'; 
     } 
     else 
     { 
      $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . '</ins>'; 
     } 
    } 
    return $price; 
} 

代碼放在您的活動子主題(或主題的function.php文件),或者也可以任何插件文件


這裏是我的測試服務器真實截圖:

enter image description here

此代碼測試,真正的作品。


相關答案:

+0

你這麼多@loictheaztec。但在我的網站上遇到問題。檢查圖像: http://i.imgur.com/Mk3FRZ7.png?1 – Dora

+0

@Dora你是否已經刪除了自定義相關的CSS規則? – LoicTheAztec

+0

啊哈,讓我刪除然後我的自定義CSS規則。 – Dora

0

此代碼解決的問題之一:

add_filter('woocommerce_variable_price_html', 'prices_custom_labels_min_max', 20, 2); 
function prices_custom_labels_min_max($price, $product) { 

// Set HERE your custom labels names 
$per_dozen = ' '. __('per dozen', 'woocommerce'); 
$per_case = ' '. __('per case', 'woocommerce'); 

// Getting the min and max variations prices 
$variation_min_reg_price = $product->get_variation_regular_price('min', true); 
$variation_max_reg_price = $product->get_variation_regular_price('max', true); 

if($variation_min_reg_price == $variation_max_reg_price){ 
    $price = '<ins class="highlight">'.woocommerce_price($variation_min_reg_price) . $per_dozen . '</ins>'; 
} 
else 
{ 
    $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . '</ins>'; 
} 
return $price; 
} 
相關問題