You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP All Import调用常规售价函数实现促销价折扣计算咨询

实现方案

你需要给促销价函数传入和常规售价函数一致的CSV字段参数,拿到常规售价计算结果后再应用折扣即可,修改后代码如下:

<?php
// 原有全局变量保留
$GLOBALS['$commissione_paypal_lm'] = 4;
$GLOBALS['$spese_spedizione_moto_lm'] = 10;
$GLOBALS['$spese_spedizione_autocarro_lm'] = 10;
$GLOBALS['$spese_spedizione_varie_lm'] = 7;
$GLOBALS['$pfu_moto_lm'] = 1.50;
$GLOBALS['$pfu_autocarro_lm'] = 4.90;
$GLOBALS['$pfu_varie_lm'] = 2.90;
// 新增全局折扣变量,比如10%折扣设为0.9,可按需调整
$GLOBALS['$sconto_lm'] = 0.9;

// 原有prezzo_finale、prezzo_finale_lm函数逻辑保留,仅优化了重复判断逻辑
function prezzo_finale( $price = null, $pfu = null, $diametro = null ) {
    if ( !empty( $price ) ) {
        $price = preg_replace("/[^0-9,.]/", "", $price);
        $pfu = preg_replace("/[^0-9,.]/", "", $pfu);
        
        $price = str_replace(",",".",str_replace(".","",$price));
        $pfu = str_replace(",",".",str_replace(".","",$pfu));
        
        $percent = 0;
        if ($diametro != '') {
            $term = term_exists( $diametro, 'pa_diametro', 0 ); 
            if ( $term !== 0 && $term !== null ) { 
                $percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
            }
        }
        
        $prezzo_finale = $price;
        if (empty( $percent ) || $percent <= 0 ) {
            $prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
            $prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
        }else if ($percent > 0){
            $prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
            $prezzo_finale = ($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu + $prezzo_appoggio_finale);
        }
        return $prezzo_finale; 
    }
}
function prezzo_finale_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null ) {
    if ( !empty( $fifo_ponderato ) ) {
        $fifo_ponderato = preg_replace("/[^0-9,.]/", "", $fifo_ponderato);
        $fifo_ponderato = str_replace(",",".",str_replace(".","",$fifo_ponderato));
        
        $percent = 0;
        if ($diametro != '') {
            $term = term_exists( $diametro, 'pa_diametro', 0 ); 
            if ( $term !== 0 && $term !== null ) { 
                $percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
            }
        }
        
        $prezzo_finale_lm = ($fifo_ponderato);
        if ($settori_codice === 'MOTO' || $settori_codice === 'SCOOTER' || $settori_codice === 'CICLOMOTORI'){
            $spese_spedizione = $GLOBALS['$spese_spedizione_moto_lm'];
            $pfu = $GLOBALS['$pfu_moto_lm'];
        }else if ($settori_codice === 'AUTOCARRO'){
            $spese_spedizione = $GLOBALS['$spese_spedizione_autocarro_lm'];
            $pfu = $GLOBALS['$pfu_autocarro_lm'];
        }else{
            $spese_spedizione = $GLOBALS['$spese_spedizione_varie_lm'];
            $pfu = $GLOBALS['$pfu_varie_lm'];
        }       
        if (empty( $percent ) || $percent <= 0) {
            $prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
            $prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
        }else if ($percent > 0){            
            $prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
            $prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
        }
        return $prezzo_finale_lm_paypal; 
                
    }
}

// 重写促销价函数
function prezzo_offerta_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null, $sconto = null ) {
    // 调用常规售价函数获取基础定价
    $prezzo_regolare = prezzo_finale_lm($fifo_ponderato, $diametro, $settori_codice);
    if (empty($prezzo_regolare)) {
        return '';
    }
    // 优先使用传入的自定义折扣,无自定义则调用全局折扣
    $discount = !empty($sconto) ? (float)$sconto : $GLOBALS['$sconto_lm'];
    // 计算最终促销价保留2位小数
    $prezzo_scontato = round($prezzo_regolare * $discount, 2);
    return $prezzo_scontato;
}
?>
调用方法
  • 常规售价调用方式保持不变:[prezzo_finale_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
  • 促销价默认调用方式:[prezzo_offerta_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
  • 若需单独为单个商品设置折扣,可在CSV新增折扣列后调用:[prezzo_offerta_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]}, {自定义折扣列名[1]})]

内容的提问来源于stack exchange,提问作者Alessandro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:09:03