在WooCommerce使用下面的代码在简单的产品价格和变量的产品后添加自定义标签:可变产品选择:获取实时选择的值
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){
// Set HERE your custom labels names
$per_dozen = ' '. __('per dozen', 'woocommerce');
$per_case = ' '. __('per case (20 dozens)', 'woocommerce');
// 1) Variable products
if ($product->product_type != 'simple' && $product->variation_id) {
// Getting the array of existing attributes values for a variation
$variation_attribute_value = $product->variation_data;
// Here we keep only the last value in this array
$last_variation_attribute_slug_value = ' ' . end($variation_attribute_value);
// Finding the word 'case' in the attribute value slug
$has_case = strstr($last_variation_attribute_slug_value, 'case');
// Setting the right displayed label depending on attribute value slug
if($has_case)
$attribute_quantity_name_value = $per_case;
else
$attribute_quantity_name_value = $per_dozen;
// Here the output price + custom label
$price = '<ins class="highlight">'.woocommerce_price($product- >regular_price).$attribute_quantity_name_value.'</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;
}
但在变型产品,我与问题在现场展示的价格中附加了自定义标签。我使用的代码只在现场价格“每打”后显示。
我将需要获得定制的“量”的选择所选择的值后的价格加入正确的标签:
- 如果选择的值是“打”我需要后,现场的价格,以显示“每打(012打)“,
- 如果选定的值是”大小写(20打)“,我需要在每个案例(20打)后以实时价格显示。
这张截图是我实际上是所有情况:
检查这个问题上我的网站specific product page
所以我需要得到属性“量“选定的值将正确的标签附加到现场价格。
任何帮助?我该怎么做才能做到这一点?
我已经尝试了很多代码,我无法得到它的工作。
无法打开链接! – Smit
刚刚打开没有问题..你得到什么错误?再试一次。 – Dora