更改分期付款框中的默认文本吗?

时间:2018-06-20 18:45:46

标签: woocommerce

我在某些产品页面中设置了分期付款框。默认情况下,该框始终显示最大的分期付款数,即18。我需要它默认显示“是否需要分期付款”文本。有什么建议吗?

我已经在functions.php中找到了该函数,但是我什么也无法工作。

//========= Installments in product page ======
add_action( 'woocommerce_before_add_to_cart_form', 'add_installments_single_page' );

function add_installments_single_page() {

    global $product;
    $thePrice = $product->get_price();
    $categories = get_the_terms( $product->ID, 'product_cat' );
    $currentCategory =  array();
    foreach ( $categories as $category ) {
        array_push($currentCategory,$category->name);
    }
    $currentCategory = join(',', $currentCategory);
    $currentBrand = get_the_term_list( $product->ID, 'yith_product_brand', '', '' );
    $currentBrand = strip_tags( $currentBrand );
    $currentSKU = $product->get_sku();
    //$a = get_option( 'woocommerce_modirum_settings' );
    $query = new WP_Query(array(
        'post_type' => 'product-installment',
        'post_status' => 'publish',
        'posts_per_page' => -1
    ));

    $DATAarray = array(); 
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();


        if ( empty(preg_replace('/\s+/', '', get_post_meta( $post_id, 'installments', true )) ) ){ 
            $installments = "0";
        } 
        else {
            $installments =preg_replace('/\s+/', '', get_post_meta( $post_id, 'installments', true ));
        }

        if ( empty(preg_replace('/\s+/', '', get_post_meta( $post_id, 'installments_free_rate', true )) ) ){ 
            $installments_free_rate = "0";
        } 
        else {
            $installments_free_rate =preg_replace('/\s+/', '', get_post_meta( $post_id, 'installments_free_rate', true ));
        }


        if ( empty(preg_replace('/\s+/', '', get_post_meta( $post_id, 'starting_price', true )) ) ){ 
            $starting_price = 0;
        } 
        else {
            $starting_price =preg_replace('/\s+/', '', get_post_meta( $post_id, 'starting_price', true ));
        }

        if ( empty(preg_replace('/\s+/', '', get_post_meta( $post_id, 'end_price', true )) ) ){ 
            $end_price = 0;
        } 
        else {
            $end_price =preg_replace('/\s+/', '', get_post_meta( $post_id, 'end_price', true ));
        }


        $installment_rule = array(
            "installments" => $installments,
            "installments_free_rate" => $installments_free_rate,
            "starting_price" => $starting_price,
            "end_price" => $end_price,
            "included_skus" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'included_skus', true )),
            "included_categories" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'included_categories', true )),
            "included_manufacturers" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'included_manufacturers', true )),
            "excluded_skus" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'excluded_skus', true )),
            "excluded_categories" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'excluded_categories', true )),
            "excluded_manufacturers" => preg_replace('/\s+/', '', get_post_meta( $post_id, 'excluded_manufacturers', true ))
        );

        array_push($DATAarray, $installment_rule);
    }

    wp_reset_query();
    ?>
    <script>
    <?php
    $rulesSetArray = explode(",", get_option( 'wc_settings_tab_demo_installment_rates' ));  
    $i = 0;
    foreach ($rulesSetArray as $ruleset) {
        if($i ==0){
            echo 'var installmentsArray = [ ["0","1","Δόση"],[';
            $x = 0;
            $ruleset = explode("|", $ruleset);
            foreach($ruleset as $rule){
                if($x ==0){
                    echo '"'.$rule.'"';
                }
                else{
                    echo ','.'"'.$rule.'"';
                }
                $x++;
            }
            echo ']';
        }
        else {
            echo ',[';
            $x = 0;
            $ruleset = explode("|", $ruleset);
            foreach($ruleset as $rule){
                if($x ==0){
                    echo '"'.$rule.'"';
                }
                else{
                    echo ','.'"'.$rule.'"';
                }
                $x++;
            }
            echo ']';
        }
        $i++;
    }
    echo '];';
    ?>


    var product_installment = [];   
    var current_category = "<?php echo $currentCategory ?>"; 
    var current_brand = "<?php echo  $currentBrand ?>"; 
    var current_sku = "<?php echo  $currentSKU ?>"; 
    var current_price = <?php echo $thePrice ?>;
    var html = "";   
    var products = [];
    /*
        var installmentsArray = [
        [0,1],
        [1,1],
        [2,1],
        [3,1],
        [4,1],
        [5,1],
        [6,1],
        [7,1],
        [8,1],
        [9,1],
        [10,1.0341],
        [11,1.0365],
        [12,1.0389],
        [13,1.0413],
        [14,1.0437],
        [15,1.0461],
        [16,1.0485],
        [17,1.0509],
        [18,1.0533],
        [19,1.0558],
        [20,1.0582],
        [21,1.0606],
        [22,1.063],
        [23,1.0655],
        [24,1.0679],
        [25,1.0704],
        [26,1.0728],
        [27,1.0753],
        [28,1.0777],
        [29,1.0802],
        [30,1.0826],
        [31,1.0851],
        [32,1.0876],
        [33,1.09],
        [34,1.0925],
        [35,1.095],
        [36,1.0975],
        [37,1.0999],
        [38,1.1024],
        [39,1.1049],
        [40,1.1074],
        [41,1.1099],
        [42,1.1124],
        [43,1.1149],
        [44,1.1174],
        [45,1.12],
        [46,1.1225],
        [47,1.125],
        [48,1.1275]
    ];  
    */
        products = <?php echo json_encode($DATAarray)?>;

        for (var i = 0; i < products.length; i++) {  

            var exclude = false;  
            var include = false;  
            var starting_price = products[i].starting_price;  
            var end_price = products[i].end_price;  
            var includedP = products[i].included_skus.split(",");  
            var includedC = products[i].included_categories.split(",");  
            var includedCurrentCategory = false; 
            for (var j = 0; j < current_category.split(",").length; j++) { 
                if(includedC.indexOf(current_category.split(",")[j]) > -1) includedCurrentCategory = true; 
            } 
            var includedM = products[i].included_manufacturers.split(",");  
            var excludedP = products[i].excluded_skus.split(",");  
            var excludedC = products[i].excluded_categories.split(",");  
            var excludedM = products[i].excluded_manufacturers.split(",");  
            var excludedCurrentCategory = false;  
            for (var j = 0; j < current_category.split(",").length; j++) {  
                if(excludedC.indexOf(current_category.split(",")[j]) > -1) excludedCurrentCategory = true;  
            }  

            if (excludedP.indexOf(current_sku) > -1 || excludedM.indexOf(current_brand) > -1 || excludedCurrentCategory == true || parseFloat(current_price) < parseFloat(starting_price) && parseFloat(starting_price) != 0 || parseFloat(current_price) > parseFloat(end_price) && parseFloat(end_price) != 0) exclude = true;

            if (includedP == "" && includedM == "" && includedCurrentCategory == false && parseFloat(current_price) >= parseFloat(starting_price) && parseFloat(current_price) <= parseFloat(end_price) || includedP.indexOf(current_sku) > -1 || includedM.indexOf(current_brand) > -1 || includedCurrentCategory == true )include = true;

            if (!exclude && include) {
                product_installment.push([products[i].installments,products[i].installments_free_rate]);
            }  

        }  

        //max_installment = product_installment.sort(function(a, b){return b-a})[0];

        var max_installment = product_installment.sort(sortFunction);
        var max_installment_free_rate = max_installment[0][1];
        function sortFunction(a, b) {
            if (a[0] === b[0]) {
                return 0;
            }
            else {
                return (a[0] < b[0]) ? -1 : 1;
            }
        }

        if (max_installment[0][0] !== undefined) {

            html = "<p><select class=\'installments-box\'><option value=\'-1\'>ΤΟ ΘΕΛΕΙΣ ΜΕ ΔΟΣΕΙΣ;</option>";  
            for (x=2; x <= max_installment[0][0]; x++ ) {
                if( parseInt(x) <= parseInt(max_installment[0][1])){
                    html += "<option value=\'"+x+"\'>με "+x+" "+installmentsArray[1][2];
                    html += "</optiom>";
                }
                else{
                   html += "<option value=\'"+x+"\'>με "+x+" "+installmentsArray[x][2];
                   if(parseFloat(installmentsArray[x][1])>1){
                        html += " - συνολικός τόκος :"+((parseFloat(current_price)*parseFloat(installmentsArray[x][1])) - parseFloat(current_price)).toFixed(2) +" €</optiom>";
                    }
                    else {
                        html += "</optiom>";
                    }
                }   
            }
            html += "</select></p>";  
            jQuery(".product-info .price-wrapper").append(html);  
            jQuery(".installments-box option[value=\'"+max_installment[0][0]+"\']").prop("selected", true);  

            jQuery("p.product-page-price").append("<span class=\'installments-label\'></span>");  
            calculateInstallments(max_installment[0][0], max_installment_free_rate, installmentsArray);  

            jQuery(".installments-box").change(function() { 
                calculateInstallments(jQuery(this).val(), max_installment_free_rate, installmentsArray);  
            });  
        }  



    function calculateInstallments(selectedInstallment, max_installment_free_rate, installmentsArray){  
        if(selectedInstallment == -1){jQuery(".installments-label").hide();}  
        else {  
            jQuery(".installments-label").show();  

            if(parseInt(selectedInstallment) <= parseInt(max_installment_free_rate)){
                jQuery(".installments-label").html("ή με "+(parseFloat(current_price)/parseFloat(selectedInstallment )).toFixed(2)+"<span class=\'woocommerce-Price-currencySymbol\'>€</span>/μήνα</span>");
            }
            else{
                jQuery(".installments-label").html("ή με "+( (parseFloat(current_price)*parseFloat(installmentsArray[selectedInstallment][1])) /parseFloat(selectedInstallment )).toFixed(2)+"<span class='woocommerce-Price-currencySymbol'>€</span>/μήνα</span>"); 
            }

        }  
    }  
    </script>
    <?php

    return true;
}

0 个答案:

没有答案