自定义产品字段以更改 wordpress 中的产品总价

Custom product field to change product total price in wordpress

我使用名为 WC Fields Factory 的 Wordpress 插件添加了一些自定义产品字段,并希望在字段从用户那里获得一些输入后更改 Woocommerce 产品的价格。 为此,我使用了以下代码:

// custom product field functions

function save_bracelet_engraving_front_or_back_fee($cart_item_data, $product_id) {
    if(isset($_POST['gravaj_bratara_fataspate_10_lei']) && !empty($_POST['gravaj_bratara_fataspate_10_lei'])){
        $cart_item_data['bracelet_front_or_back_engraving_fee'] = htmlspecialchars($_POST['gravaj_bratara_fataspate_10_lei']);
    }
    return $cart_item_data;
}

function save_bracelet_engraving_front_and_back_fee($cart_item_data, $product_id){
    if(isset($_POST['gravaj_bratara_fata_si_spate_20_lei']) && !empty($_POST['gravaj_bratara_fata_si_spate_20_lei'])){
        $cart_item_data['bracelet_front_and_back_engraving_fee'] = htmlspecialchars($_POST['gravaj_bratara_fata_si_spate_20_lei']);
    }
    return $cart_item_data;
}

function save_necklace_size_fee($cart_item_data, $product_id) {
    if(isset($_POST['marime_colier']) && !empty($_POST['marime_colier'])){
        $cart_item_data['necklace_size_fee'] = htmlspecialchars($_POST['marime_colier']);
    }
    return $cart_item_data;
}

// product hook to add custom product field data in global woocommerce product info object
add_filter('woocommerce_add_cart_item_data', 'save_bracelet_engraving_front_or_back_fee', 99, 2);
add_filter('woocommerce_add_cart_item_data', 'save_bracelet_engraving_front_and_back_fee', 99, 2);
add_filter('woocommerce_add_cart_item_data', 'save_necklace_size_fee', 99, 2);

// custom product field function to rewrite WC product price
function calculate_bracelet_engraving_front_or_back_fee($cart_object) {
    if(!WC()->session->__isset('reload_checkout')){
        // Adding bracelet engraving front or back additional price
        $additionalPrice = 10;
        foreach($cart_object->cart_contents as $key=>$value){
            if(isset($value['bracelet_front_or_back_engraving_fee'])){
                if(isset($value['data']->price)) {
                    /*Before vers 3.0 WC*/
                    $orgPrice = floatval($value['data']->price);
                    $value['data']->set_price($orgPrice + $additionalPrice);
                }else{
                    /*WC 3.0 +*/
                    $orgPrice = floatval($value['data']->get_price());
                    $value['data']->set_price($orgPrice + $additionalPrice);
                }
            }
        }

    }
}
function calculate_bracelet_engraving_front_and_back_fee($cart_object) {
    if(!WC()->session->__isset('reload_checkout')){
        // Adding bracelet engraving front or back additional price
        $additionalPrice = 20;
        foreach($cart_object->cart_contents as $key=>$value){
            if(isset($value['bracelet_front_and_back_engraving_fee'])){
                if(isset($value['data']->price)) {
                    /*Before vers 3.0 WC*/
                    $orgPrice = floatval($value['data']->price);
                    $value['data']->set_price($orgPrice + $additionalPrice);
                }else{
                    /*WC 3.0 +*/
                    $orgPrice = floatval($value['data']->get_price());
                    $value['data']->set_price($orgPrice + $additionalPrice);
                }
            }
        }

    }
}
function calculate_necklace_size_fee($cart_object) {
    if(!WC()->session->__isset('reload_checkout')){
        // Adding bracelet engraving front or back additional price
        $additionalPrice = 0;
        foreach($cart_object->cart_contents as $key=>$value){
            if(isset($value['necklace_size_fee']) && !empty($value['necklace_size_fee'])){
                switch($value['necklace_size_fee']) {
                    case 100:
                        $additionalPrice = 0;
                        break;
                    case 70:
                        $additionalPrice = 10;
                        break;
                    case 45:
                        $additionalPrice = 20;
                        break;
                    default:
                        $additionalPrice = 0;
                        break;
                }
                if(isset($value['data']->price)) {
                    /*Before vers 3.0 WC*/
                    $orgPrice = floatval($value['data']->price);
                    $value['data']->set_price($orgPrice - $additionalPrice);
                }else{
                    /*WC 3.0 +*/
                    $orgPrice = floatval($value['data']->get_price());
                    $value['data']->set_price($orgPrice - $additionalPrice);
                }
            }
        }

    }
}
// custom product hook to rewrite WC product price
add_action( 'woocommerce_before_calculate_totals', 'calculate_bracelet_engraving_front_or_back_fee', 99 );
add_action( 'woocommerce_before_calculate_totals', 'calculate_bracelet_engraving_front_and_back_fee', 99 );
add_action( 'woocommerce_before_calculate_totals', 'calculate_necklace_size_fee', 99 );

当我将产品添加到购物车中时,价格随着新费用的增加而变化,但如果我继续在结帐页面上再次添加费用。因此,如果我为 1 个雕刻文本额外添加 10 美元,它会在将产品添加到购物车时添加 10 美元,但如果我转到结帐页面,它会再添加 10 美元,我不知道是什么原因造成的问题,我试过更换挂钩但没有成功。感谢任何 help/hint,提前致谢!

我已将最后 3 个挂钩更改为:

// custom product hook to rewrite WC product price
add_action( 'woocommerce_after_calculate_totals', 'calculate_bracelet_engraving_front_or_back_fee', 99 );
add_action( 'woocommerce_after_calculate_totals', 'calculate_bracelet_engraving_front_and_back_fee', 99 );
add_action( 'woocommerce_after_calculate_totals', 'calculate_necklace_size_fee', 99 );