Pereiti prie turinio

logishkas

Nariai
  • Pranešimai

    3
  • Užsiregistravo

  • Lankėsi

  • Atsiliepimai

    0%

logishkas Pranešimai

  1. Prikabinu pakoreguotą variantą. Būtinai perskaitykite komentarus.

    <?php
    
    /*
    Paskirtis: Kainų rodymas dviem valiutomis Woocommerce el. parduotuvėje
    Autorius: Weed @ uzdarbis.lt, papildymus atliko logishkas
    SVARBU:
    - visi suprantame, kad šis snippetas yra negražus, netobulas ir tik "tiks iš bėdos" sprendimas
    - reikia turėti omeny, kad snippete yra perskaičiuojamos galutinės užsakymų kainos; į galutines kainas įeina daug sandų (produktų kainos, mokesčiai, pristatymo išlaidos, nuolaidos ir pan.) ir realybė tokia, kad čionais atsižvelgiama ne į visus (žr. bug'ai).
    - visiems minėtiems sandams taikomas apvalinimas iki dešimtųjų lito dalių; jei nereikia, pašalinkite round(<...>,1);
    BUG'ai:
    - kainos turi būti nurodytos be tarpų, t.y. 1000,00, o ne 1 000,00 (woocommere nustatymuose galite nustatyti, kad be tarpo rodytų);
    - veikia su shippingu, fees, bet neišbandyta (sorry, tiesiog neaktualu mano atveju) su mokesčiais (tax);
    - su kuponais viską gerai apskaičiuoja tik tuo atveju, jei jie ne procentiniai (beje, Woocommerce 2.2.7 vis dar yra bug'as, kuris net originalia valiuta blogai procentinius kuponus apskaičiuoja).
    */
    
    // nustatymai
    
    $currency = get_woocommerce_currency(); // parduotuvės valiuta
    $euro = " €"; // euro simbolis su non-breaking space
    $rate = 3.4528; // santykis
    // iš nustatymų puslpio:
    $dec_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_decimal_sep')),ENT_QUOTES);
    $tho_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_thousand_sep')),ENT_QUOTES);
    $num_dec = absint(get_option('woocommerce_price_num_decimals'));
    
    // filtrai
    
    add_filter('woocommerce_variation_price_html', 'convert_variation_price_html', 10, 2);
    add_filter('woocommerce_variation_sale_price_html', 'convert_variation_sale_price_html', 10, 2);
    add_filter('woocommerce_get_price_html','convert_get_price_html', 10, 2);
    add_filter('woocommerce_cart_item_price','convert_cart_item_price', 10, 3);
    add_filter('woocommerce_cart_item_subtotal','convert_cart_item_subtotal', 10, 3);
    add_filter('woocommerce_cart_subtotal', 'convert_cart_subtotal', 10, 3);
    add_filter('woocommerce_cart_total', 'convert_cart_total', 10,1);
    //add_filter('woocommerce_cart_total_ex_tax', 'convert_cart_total_ex_tax', 10,1);
    add_filter('woocommerce_cart_shipping_method_full_label', 'convert_cart_shipping_method_full_label', 10, 2);
    add_filter('woocommerce_cart_totals_fee_html', 'convert_cart_totals_fee_html', 10, 2);
    add_filter('woocommerce_cart_totals_coupon_html', 'convert_cart_totals_coupon_html', 10, 2);
    add_filter('woocommerce_order_formatted_line_subtotal', 'convert_order_formatted_line_subtotal', 10, 3);
    add_filter('woocommerce_get_order_item_totals', 'convert_get_order_item_totals', 10, 2);
    add_filter('woocommerce_get_formatted_order_total', 'convert_get_formatted_order_total', 10, 2);
    
    // funkcijos
    
    function convert_variation_price_html($price, $this) {
    return "<span class=\"amount\">".strip_tags($price)." (".wc_format_localized_price(number_format(strip_tags(wc_format_decimal($price)) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS["euro"].")</span>";
    }
    
    function convert_variation_sale_price_html($price, $this) {
    $prices = explode($GLOBALS['currency'], strip_tags($price));
    return "<del>".convert_variation_price_html($prices[0].$GLOBALS['currency'], $this)."</del><ins>".convert_variation_price_html($prices[1].$GLOBALS['currency'], $this)."</ins>";
    }
    
    function convert_get_price_html($price, $id) {
    $product = get_product($id);
    $prices = preg_match_all("/<span class=\"amount\">(.*?)<\/span>/", strip_tags($price, "<span>"), $matches);
    if (count($matches[1]) == 1) {
    	return $price." (".wc_format_localized_price(number_format(wc_format_decimal($price) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    } else if (count($matches[1]) == 2) {
    	if ($product->is_on_sale()) {
    		return $price." (".wc_format_localized_price(number_format(wc_format_decimal($matches[1][1]) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    	}
    	else {
    		return $price." (".wc_format_localized_price(number_format(wc_format_decimal($matches[1][0]) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro']."–".wc_format_localized_price(number_format(wc_format_decimal($matches[1][1]) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";	
    	}
    } else if (count($matches[1]) == 4) {
    	return $price." (".wc_format_localized_price(number_format(wc_format_decimal($matches[1][2]) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro']."–".wc_format_localized_price(number_format(wc_format_decimal($matches[1][3]) / $GLOBALS['rate'],$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";	
    } else {
    	return $price;
    }
    }
    
    function convert_cart_item_price($price, $cart_item, $cart_item_key) {
    $eurPrice = strip_tags($price);
    $eurPrice = preg_replace("/[^0-9\,]/","",$eurPrice);
    return $price." (".wc_format_localized_price(number_format(round(wc_format_decimal($eurPrice) / $GLOBALS['rate'],1),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    }
    
    function convert_cart_item_subtotal($price, $cart_item, $cart_item_key) {
    $eurPrice = strip_tags($price);
    $eurPrice = preg_replace("/[^0-9\,]/","",$eurPrice);
    $eurPrice = wc_format_decimal($eurPrice)/$cart_item["quantity"];
    $eurPrice = round($eurPrice / $GLOBALS['rate'],1);
    $eurPrice = $eurPrice*$cart_item["quantity"];
    return $price." (".wc_format_localized_price(number_format(wc_format_decimal($eurPrice),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    }
    
    function convert_cart_subtotal($cart_subtotal, $compound, $this) {
    $contents = WC()->cart->cart_contents;
    $eurSubtotal = 0;
    foreach ($contents as $value) {
    	$id = empty($value['variation_id']) ? $value['product_id'] : $value['variation_id'];
    	$product = new WC_Product($id);
    	$price = $product->get_price();
    	$eurPrice = round($price / $GLOBALS['rate'],1);
    	$eurSubtotal += $eurPrice*$value["quantity"];
    }
    return $cart_subtotal." (".wc_format_localized_price(number_format(wc_format_decimal($eurSubtotal),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    }
    
    function convert_cart_total($price){
    $contents = WC()->cart->cart_contents;
    $eurSubtotal = 0;
    foreach ($contents as $value) {
    	$id = empty($value['variation_id']) ? $value['product_id'] : $value['variation_id'];
    	$product = new WC_Product($id);
    	$cost = $product->get_price();
    	$eurPrice = round($cost / $GLOBALS['rate'],1);
    	$eurSubtotal += $eurPrice*$value["quantity"];
    }
    $shipping = WC()->shipping->shipping_total;
    $fees = WC()->cart->fee_total;
    $discount = wc_format_decimal(WC()->cart->get_total_discount());
    $eurDiscount = round($discount / $GLOBALS['rate'],1);
    $eurShipping = round($shipping / $GLOBALS['rate'],1);
    $eurFees = round($fees / $GLOBALS['rate'],1);
    $eurSubtotal += $eurShipping + $eurFees - $eurDiscount;
    
    return wc_format_localized_price($price)." (".wc_format_localized_price(number_format(wc_format_decimal($eurSubtotal),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    }
    
    /*function convert_cart_total_ex_tax($price){
    
    }*/
    
    function convert_cart_shipping_method_full_label($label, $method) {
    global $woocommerce;
    if ($method->cost > 0) {
    	$labels = explode("<span class=\"amount\">", $label);
    	$label = $labels[0];
    	$label .= "<span class=\"amount\">".$labels[1]."</span>";
    	$label .= "<span style=\"font-weight: normal;font-size: 13px;color: #444;\"> (".wc_format_localized_price(number_format(round(wc_format_decimal($labels[1]) / $GLOBALS['rate'], 1), $GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")</span>";
    }
    return $label;
    }
    
    function convert_cart_totals_fee_html($cart_totals_fee_html, $fee) {
    return $cart_totals_fee_html." (".wc_format_localized_price(number_format(round(wc_format_decimal($fee->amount) / $GLOBALS['rate'],1),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    }
    
    function convert_cart_totals_coupon_html($value, $coupon) {
    $values = explode("</span>", $value);
    if (count($values) == 2) {
    	$value = $values[0]." (";
    	$value .= wc_format_localized_price(number_format(round(wc_format_decimal(strip_tags($values[0])) / $GLOBALS['rate'],1), 2)).$GLOBALS["euro"];
    	$value .= ")</span>".$values[1];
    }
    return $value;
    }
    
    function convert_order_formatted_line_subtotal($subtotal, $item, $this) {
    $eurSub = 0;
    $id = empty($item['variation_id']) ? $item['product_id'] : $item['variation_id'];
    $product = new WC_Product($id);
    $price = $product->get_price();
    $eurPrice = round($price / $GLOBALS['rate'],1);
    $eurSub += $eurPrice*$item["qty"];
    return $subtotal." (".wc_format_localized_price(number_format(wc_format_decimal($eurSub),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    
    }
    
    function convert_get_order_item_totals($total_rows, $this) {
    $order = new WC_Order($this);
    $items = $order->get_items();
    $eurSub = 0;
    foreach ($items as $item) {
    	$id = empty($item['variation_id']) ? $item['product_id'] : $item['variation_id'];
    	$product = new WC_Product($id);
    	$price = $product->get_price();
    	$eurPrice = round($price / $GLOBALS['rate'],1);
    	$eurSub += $eurPrice*$item["qty"];
    }
    array_splice($total_rows, 0, 1, array("cart_subtotal" => array("label" => $total_rows["cart_subtotal"]["label"], "value" => "<span class=\"amount\">".strip_tags($total_rows["cart_subtotal"]["value"])." (".wc_format_localized_price(number_format($eurSub,$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")</span>")));
    foreach ($total_rows as &$row) {
    	$curpos = strpos($row["value"],$GLOBALS['currency']);
    	$eurpos = strpos($row["value"],$GLOBALS['euro']);
    	$len = strlen($GLOBALS['currency']);
    	$last = $curpos+$len;
    	if ($curpos !== false && $eurpos === false) { 
    		$row["value"] = substr_replace($row["value"], " (".wc_format_localized_price(number_format(round(wc_format_decimal(strip_tags($row["value"])) / $GLOBALS['rate'],1),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")", $last, 0);
    	} 
    }
    return $total_rows;
    }
    
    function convert_get_formatted_order_total($formatted_total, $this) {
    $order = new WC_Order($this);
    $items = $order->get_items();
    $eurSubtotal = 0; $fees = 0;
    foreach ($items as $item) {
    	$id = empty($item['variation_id']) ? $item['product_id'] : $item['variation_id'];
    	$product = new WC_Product($id);
    	$price = $product->get_price();
    	$eurPrice = round($price / $GLOBALS['rate'],1);
    	$eurSubtotal += $eurPrice*$item["qty"];
    }
    $shipping = $order->get_total_shipping();
    foreach ($order->get_fees() as $item) {
    	$fees += $item['line_total'];
    }
    $discount = $order->get_order_discount();
    $eurDiscount = round($discount / $GLOBALS['rate'],1);
    $eurShipping = round($shipping / $GLOBALS['rate'],1);
    $eurFees = round($fees / $GLOBALS['rate'],1);
    $eurSubtotal += $eurShipping + $eurFees - $eurDiscount;
    
    $formatted_total = $formatted_total . " (".wc_format_localized_price(number_format(wc_format_decimal($eurSubtotal),$GLOBALS['num_dec'],$GLOBALS['dec_sep'],$GLOBALS['tho_sep'])).$GLOBALS['euro'].")";
    return $formatted_total;
    }
    
    ?>
    

  2. Weed'as sakyčiau visai puikiai padirbėjo, tačiau toks skriptas tinkamai veikti negali ir netgi siūlyčiau jo nenaudoti, jei nepageidaujate smulkių netikslumų. Priežastis paprasta - vieno produkto kaina apvalinama, bet sumuojamos nekonvertuotos ir neapvalintos produktų kainos. Pvz., jei produktas kainuoja 10 Lt, suapvalinęs skriptas vieno tokio produkto kainą rodys 10/3.4528 ~= 2.90 euro. Jei tokio produkto krepšelyje turėsite tris vienetus, sumą už tris vienetus rodys ne 2.9x3=8.70 euro, o (3*10)/3.4529 ~= 8.69 euro - susidaro vieno euro cento skirtumas. 100 vienetų atveju, prarasite jau ne euro centą, o 1.31 LT.

     

    Problemos sprendimas turėtų būti sudėtingesnis..

×
×
  • Pasirinkite naujai kuriamo turinio tipą...