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

时间:2018-01-07 17:25:27

标签: wordpress woocommerce hook-woocommerce

我使用名为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美元而我没有想知道导致问题的是什么,我试过改变钩子但没有成功。如有任何帮助/提示,​​请提前感谢!

1 个答案:

答案 0 :(得分:0)

我已将最后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 );
相关问题