找到了一個工作解決方案。這可能有點臃腫,因爲我不是代碼大師,但我做到了!以下是一個基本版本,但這種靈活性將允許更多有用的功能,例如自動批量折扣具有特定屬性的產品的用戶角色的變化價格。
add_filter('woocommerce_get_price','change_price', 10, 2);
function change_price($price, $productd){
global $post, $woocommerce;
$post_id = $variation->ID;
$args = array(
'post_type' => 'product_variation',
'post_status' => array('private', 'publish'),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts($args);
//declare variables for later use
$demomp3 = '';
$basicmp3 = '';
$basicwav = '';
$premiummp3 = '';
$premiumwav = '';
$syncmp3 = '';
$syncwav = '';
foreach ($variations as $variation) {
$variation_id = absint($variation->ID);$variable_id = $this['variation_id'];
$variation_post_status = esc_attr($variation->post_status);
$variation_data = get_post_meta($variation_id);
$variation_data['variation_post_id'] = $variation_id;
//find attributes
$option1 = 'pa_license-options';
$option2 = 'pa_delivery-format';
$getmeta1 = get_post_meta($variation_id , 'attribute_'.$option1, true);
$getmeta2 = get_post_meta($variation_id , 'attribute_'.$option2, true);
$attribute1 = get_term_by('slug', $getmeta1, $option1);
$attribute2 = get_term_by('slug', $getmeta2, $option2);
$license = $attribute1->name;
$format = $attribute2->name;
//get the prices of each variation by the attribute combinations they have
if ($format === "mp3" && $license === "Demo License"){
//retrieve variation price and assign it to the previously created variable
$demomp3 = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "mp3" && $license === "Basic License"){
$basicmp3 = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "WAV" && $license === "Basic License"){
$basicwav = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "mp3" && $license === "Premium License"){
$premiummp3 = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "WAV" && $license === "Premium License"){
$premiumwav = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "mp3" && $license === "Sync License"){
$syncmp3 = get_post_meta($variation_id, '_regular_price', true);
}
if ($format === "WAV" && $license === "Sync License"){
$syncwav = get_post_meta($variation_id, '_regular_price', true);
}
}
//Simple product doesn't need to be modified
if($productd->product_type == 'simple'){
return $price;
}
//adjust prices of variable products
if ( $productd->product_type == 'variation' || $productd->product_type == 'variable') {
$regprice = get_post_meta($productd->variation_id, '_regular_price',true);
//target the current variation by matching its price to the one stored in the variable
if ($regprice == $demomp3) {
//apply price adjustment and return the new value
$price = $regprice * .5;
return $price;
}
else if ($regprice == $basicmp3) {
$price = $regprice * .5;
return $price;
}
else if ($regprice == $basicwav) {
$price = $regprice * .5;
return $price;
}
else if ($regprice == $premiummp3) {
$price = $regprice * .5;
return $price;
}
else if ($regprice == $premiumwav) {
$price = $regprice * .5;
return $price;
}
else if ($regprice == $syncmp3) {
$price = $regprice * .5;
return $price;
}
else if ($regprice == $syncwav) {
$price = $regprice * .5;
return $price;
}
else{
return $price;
}
}
}
來源
2016-11-21 09:51:20
isk
感謝您的回覆!不幸的是,你的建議是給我一個 _「致命錯誤:未捕獲的異常'異常'消息'沒有父變體#0的產品集'........」_消息。如果我將其格式化爲** $ product_variation = $ product ['variation_id']; **和** $ regular_price = $ product ['regular_price']; **,但是我可以管理從字符串中返回的所有內容是數字2,7和9,我不確定這些數字是從哪裏拉出來的。 – isk
代碼只是一個例子,你可以嘗試get_post_meta爲'_variation_id',然後'$ product_variation = new WC_Product($ product_id);和var_dump的結果 – Benoti
經過多一點調整,我想出了一個工作解決方案。感謝您的領先! – isk