File: /var/www/dinamo-shop/wp-content/themes/dinamo-shop/functions.php
<?php
define( 'THEMEROOT', get_stylesheet_directory_uri() );
define( 'FILEROOT', dirname(__DIR__) . '/' . get_option('stylesheet') );
load_theme_textdomain('dinamo_shop', get_template_directory() . '/languages');
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script( 'utility', THEMEROOT . '/assets/js/utility.bundle.js', null, null, true );
wp_enqueue_script( 'vendor', THEMEROOT . '/assets/js/vendor.bundle.js', null, null, true );
wp_enqueue_script( 'core', THEMEROOT . '/assets/js/core.bundle.js', array('utility', 'vendor'), null, true );
wp_enqueue_style( 'main-style', THEMEROOT . '/assets/css/styles.css');
});
function calclulate_product_prices($product){
if( $product->is_type( 'simple' ) ){
$reg_price = $product->regular_price;
$sale_price = $product->sale_price;
if($sale_price && $sale_price < $reg_price){
$final_price = $sale_price;
}else{
$sale_price = null;
$final_price = $reg_price;
}
} elseif( $product->is_type( 'variable' ) ){
$reg_price = $product->get_variation_regular_price('max', true);
$sale_price = $product->get_variation_sale_price('min', true);
$final_price = $product->get_variation_price( 'min', true );
if($sale_price && $sale_price < $reg_price){
}else{
$sale_price = null;
}
}
/*$chosen_id = htmlspecialchars($_GET["player"]);
if ($chosen_id){
$number_and_print_id = get_field('print_name_and_number_product', 'option');
$number_and_print = new WC_Product($number_and_print_id);
$number_and_name_print_price = $number_and_print->get_price();
$player_print_price = $final_price + $number_and_name_print_price;
}*/
return array(
'reg_price' => $reg_price,
'final_price' => $final_price,
'sale_price' => $sale_price,
'player_print_price' => $player_print_price
);
}
// WooCommerce
if( class_exists('WooCommerce') ){
require_once 'inc/woocommerce/index.php';
}
add_action( 'woocommerce_thankyou_WC_TPAYWAY', 'bbloomer_add_content_thankyou' );
function bbloomer_add_content_thankyou() {
echo '<p> Dostava će biti izvršena putem dostavne službe u roku od 4-6 radnih dana od obrade. </p>';
}
// Add your own action for the bank instructions
//add_action( 'woocommerce_email_before_order_table', 'prefix_email_instructions', 9, 3 );
function prefix_email_instructions( $order, $sent_to_admin, $plain_text = false ) {
// Get the gateway object
$gateways = WC_Payment_Gateways::instance();
$available_gateways = $gateways->get_available_payment_gateways();
$gateway = isset( $available_gateways['bacs'] ) ? $available_gateways['bacs'] : false;
// We won't do anything if the gateway is not available
if ( false == $gateway ) {
return;
}
// Add only the email instructions
if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'on-hold' ) ) {
if ( $gateway->instructions ) {
echo "Molimo vas da izvršite uplatu koristeći podatke niže.
Detalji o našoj banci
Banka: Privredna Banka Zagreb
Broj računa: IBAN: HR
VBDI (vodeći broj banke): 2500009
IBAN: 8723400091110560684
BIC: HAAB HR 22";
echo wpautop( wptexturize( $gateway->instructions ) ) . PHP_EOL;
}
}
}
// Remove the original bank details
//add_action( 'init', 'prefix_remove_bank_details', 100 );
function prefix_remove_bank_details() {
// Do nothing, if WC_Payment_Gateways does not exist
if ( ! class_exists( 'WC_Payment_Gateways' ) ) {
return;
}
// Get the gateways instance
$gateways = WC_Payment_Gateways::instance();
// Get all available gateways, [id] => Object
$available_gateways = $gateways->get_available_payment_gateways();
if ( isset( $available_gateways['bacs'] ) ) {
// If the gateway is available, remove the action hook
remove_action( 'woocommerce_email_before_order_table', array( $available_gateways['bacs'], 'email_instructions' ), 10, 3 );
}
}
add_action( 'admin_init', function(){
$_products = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1
));
while( $_products->have_posts() ) : $_products->the_post();
$count = esc_attr( get_post_meta( get_the_ID(), '_post_views_count', true ) );
if( ! $count ) {
$count = 1;
add_post_meta( get_the_ID(), '_post_views_count', $count );
}
endwhile;
});
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_payment_gateway_disable_country' );
function kia_hide_mnm_meta_in_emails( $meta ) {
return $meta;
}
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'kia_hide_mnm_meta_in_emails' );
function bbloomer_payment_gateway_disable_country( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( isset( $available_gateways['cod'] ) && WC()->customer && WC()->customer->get_billing_country() != 'HR' ) {
unset( $available_gateways['cod'] );
}
if(WC()->cart) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item["variation"] && $cart_item["variation"]["print_type"] && $cart_item["variation"]["print_type"] != "0")
unset($available_gateways['cod']);
}
}
return $available_gateways;
}
// Theme supports
add_action( 'after_setup_theme', function() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'custom-logo' );
});
add_filter( 'woocommerce_checkout_fields' , 'custom_remove_woo_checkout_fields' );
function custom_remove_woo_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['required'] = true;
$fields['billing']['billing_address_2']['label'] = __('Kućni broj');
$fields['shipping']['shipping_address_2']['required'] = true;
$fields['shipping']['shipping_address_2']['label'] = __('Kućni broj', 'dinamoshop');
unset($fields['billing']['billing_address_2']['label_class']);
unset($fields['billing']['billing_address_2']['placeholder']);
unset($fields['shipping']['shipping_address_2']['placeholder']);
unset($fields['billing']['billing_state']);
unset($fields['shipping']['shipping_state']);
return $fields;
}
function woocommerce_custom_price_to_cart_item( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if( !WC()->session->__isset( "reload_checkout" ) ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$reg_price = $value['data']->get_price();
$value["old_price"] = $reg_price;
WC()->cart->cart_contents[$key] = $value;
$new_price = $reg_price;
if( isset( $value["variation"]) && isset($value["variation"]["print_type"]) && $value["variation"]["print_type"] != "0" ) {
//for woocommerce version lower than 3
$print_type = $value["variation"]["print_type"];
$print_product_id = get_product_for_print_type($print_type);
$print_product = wc_get_product($print_product_id);
$reg_price = $value['data']->get_price();
error_log("tusam");
error_log(json_encode($print_product));
if(get_field("free_personalization", $value["product_id"])){
$print_price = 0;
}else {
$print_price = $print_product->get_price();
}
$new_price = (float)$print_price + (float)$reg_price;
//echo "NOVA CIJENA: " . $new_price;
$value['data']->set_price($new_price);
}
error_log("Dodana custom cijena");
error_log(json_encode($new_price));
error_log(json_encode($value['data']->get_price()));
if ( isset($value["variation"]["print_europa_league"]) && $value["variation"]["print_europa_league"] == "1") {
$europa_league_print_product_id = get_field('print_europa_league', 'option');
$europa_league_print_product = new WC_Product($europa_league_print_product_id);
$europa_league_print_price = $europa_league_print_product->get_price();
$new_price_europa_league = (float)$europa_league_print_price + (float)$new_price;
$value['data']->set_price($new_price_europa_league);
}
}
WC()->cart->set_session();
error_log("Sadrzaj kosarice");
error_log(json_encode(WC()->cart));
}
}
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_custom_price_to_cart_item', 9999 );
function add_fake_error($posted) {
if ($_POST['confirm-order-flag'] == "1") {
wc_add_notice( __( "custom_notice", 'fake_error' ), 'error');
}
}
add_action('woocommerce_after_checkout_validation', 'add_fake_error');
function woocommerce_custom_price_to_cart_item_reset( $cart_object ) {
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->cart_contents as $key => $value ) {
if( isset( $value["variation"]) && (isset($value["variation"]["print_type"]) && $value["variation"]["print_type"] != "0" || $value["variation"]["print_europa_league"] == "1" )) {
//for woocommerce version lower than 3
$print_type = $value["variation"]["print_type"];
if($value["old_price"]) {
$value['data']->set_price($value["old_price"]);
$value["old_price"] = null;
WC()->cart->cart_contents[$key] = $value;
}
}
}
}
}
add_action( 'woocommerce_after_calculate_totals', 'woocommerce_custom_price_to_cart_item_reset', 9999 );
function my_wc_hide_in_stock_message( $html, $product ) {
return '';
}
add_filter( 'woocommerce_get_stock_html', 'my_wc_hide_in_stock_message', 10, 2 );
// Custom image sizes
add_action( 'init', function() {
remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
add_image_size( 'logotype', 300, 300, true );
add_image_size( 'desktop_hero_image', 2500, 700, true );
add_image_size( 'mobile_hero_image', 600, 680, true );
add_image_size( '50w_image', 1500, 1500, true );
add_image_size( '1x1_ratio', 500, 500, true );
add_image_size( 'product_thumbnail_cart', 100, 100, true );
add_image_size( 'product_thumbnail', 272, 272, true );
add_image_size( 'product_thumbnail_retina', 544, 544, true );
add_image_size( 'product_thumbnail_small', 162, 162, true );
add_image_size( 'product_thumbnail_small_retina', 324, 324, true );
add_image_size( 'product_photo', 776, 776, true );
add_image_size( 'product_photo_retina', 1552, 1552, true );
add_image_size( 'product_featured', 2334, 2334, true );
add_image_size( 'page_featured', 3074, 1238, true );
});
// Navigation menus
register_nav_menus(array(
'main-nav' => __( 'Glavna navigacija', 'dinamo_shop' ),
'footer-nav' => __( 'Footer navigacija', 'dinamo_shop' ),
));
// Footer sidebars
add_action( 'init', function(){
register_sidebar(array(
'name' => __( 'Footer Column 1', 'dinamo_shop' ),
'id' => 'dinamo-footer-1',
));
register_sidebar(array(
'name' => __( 'Footer Column 2', 'dinamo_shop' ),
'id' => 'dinamo-footer-2',
));
register_sidebar(array(
'name' => __( 'Footer Column 3', 'dinamo_shop' ),
'id' => 'dinamo-footer-3',
));
register_sidebar(array(
'name' => __( 'Footer Column 4', 'dinamo_shop' ),
'id' => 'dinamo-footer-4',
));
register_sidebar(array(
'name' => __( 'Footer Social Icons', 'dinamo_shop' ),
'id' => 'dinamo-social-icons',
));
});
// add custom class to li element in nav_menu
add_filter('wp_nav_menu_objects', function( $objects, $args ){
foreach( $objects as $key => $item ){
$objects[$key]->classes[] = 'nav-item';
}
return $objects;
}, 10, 2);
// add custom class to an element in nav_menu
add_filter('wp_nav_menu', function( $link_class ){
return preg_replace( '/<a /', '<a class="nav-link"', $link_class );
});
// Dinamo breadcrumbs
function dinamo_breadcrumbs(){
$homepage = get_option( 'page_on_front' );
$homeurl = get_permalink( $homepage );
$hometitle = get_the_title( $homepage );
$current = get_the_ID();
$curr_title = get_the_title( $current );
if( is_tax( 'dinamo_players' ) ){
$curr_title = __("Kupuj po igraču", 'dinamo_shop');
}
$html = '<section class="section bg-gray">
<div class="container">
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="' . $homeurl . '">' . $hometitle . '</a></li>
<li class="breadcrumb-item active" aria-current="page">' . $curr_title . '</li>
</ol>
</nav>
</div>
</section>';
return $html;
}
// change invoice pdf title
add_filter( 'wpo_wcpdf_invoice_title', 'wpo_wcpdf_invoice_title', 10, 2 );
function wpo_wcpdf_invoice_title ( $title, $document ) {
$title = __( 'Narudžba', 'dinamo_shop' );
return $title;
}
function custom_woocommerce_hidden_order_itemmeta($arr) {
$arr[] = 'print_option';
$arr[] = 'print_number';
$arr[] = 'print_name';
$arr[] = 'print_sponsor';
$arr[] = 'print_number_value';
$arr[] = 'print_name_value';
$arr[] = 'print_type';
return $arr;
}
add_filter('woocommerce_hidden_order_itemmeta', 'custom_woocommerce_hidden_order_itemmeta', 10, 1);
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$all_meta_data=get_metadata( 'order_item', $item_id, "", "");
$customized_array= array();
foreach($all_meta_data as $data_meta_key => $value)
{
}
$print_option = $item->get_meta("print_type");
$print_name_value = $item->get_meta("print_name_value");
$print_number_value = $item->get_meta("print_number_value");
$print_name = get_print_name_for_print_type($print_option, $print_name_value, $print_number_value );
if($item->get_meta("print_europa_league")) {
$europa_league_print_id = get_field('print_europa_league', 'option');
$print_name .= "<br />" . get_the_title($europa_league_print_id);
}
echo '<p><span style="display:inline-block; ">' . $print_name . '</span></p>';
}
function woocommerce_form_field( $key, $args, $value = null ) {
$defaults = array(
'type' => 'text',
'label' => '',
'description' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'autocomplete' => false,
'id' => $key,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
'autofocus' => '',
'priority' => '',
);
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'woocommerce_form_field_args', $args, $key, $value );
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
}
if ( is_string( $args['label_class'] ) ) {
$args['label_class'] = array( $args['label_class'] );
}
if ( is_null( $value ) ) {
$value = $args['default'];
}
// Custom attribute handling.
$custom_attributes = array();
$args['custom_attributes'] = array_filter( (array) $args['custom_attributes'], 'strlen' );
if ( $args['maxlength'] ) {
$args['custom_attributes']['maxlength'] = absint( $args['maxlength'] );
}
if ( ! empty( $args['autocomplete'] ) ) {
$args['custom_attributes']['autocomplete'] = $args['autocomplete'];
}
if ( true === $args['autofocus'] ) {
$args['custom_attributes']['autofocus'] = 'autofocus';
}
if ( $args['description'] ) {
$args['custom_attributes']['aria-describedby'] = $args['id'] . '-description';
}
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
if ( ! empty( $args['validate'] ) ) {
foreach ( $args['validate'] as $validate ) {
$args['class'][] = 'validate-' . $validate;
}
}
$field = '';
$label_id = $args['id'];
$sort = $args['priority'] ? $args['priority'] : '';
$field_container = '%3$s';
switch ( $args['type'] ) {
case 'country':
$countries = 'shipping_country' === $key ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
if ( 1 === count( $countries ) ) {
$field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
$field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys( $countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" readonly="readonly" />';
} else {
// country_to_state country_select
$field = '<select style="width:100%;" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country-picker ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . '><option value="">' . esc_html__( 'Select a country…', 'woocommerce' ) . '</option>';
foreach ( $countries as $ckey => $cvalue ) {
$field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . $cvalue . '</option>';
}
$field .= '</select>';
$field .= '<noscript><button type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'woocommerce' ) . '">' . esc_html__( 'Update country', 'woocommerce' ) . '</button></noscript>';
}
break;
case 'state':
/* Get country this state field is representing */
$for_country = isset( $args['country'] ) ? $args['country'] : WC()->checkout->get_value( 'billing_state' === $key ? 'billing_country' : 'shipping_country' );
$states = WC()->countries->get_states( $for_country );
if ( is_array( $states ) && empty( $states ) ) {
$field_container = '<p class="form-row %1$s" id="%2$s" style="display: none">%3$s</p>';
$field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" readonly="readonly" data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '"/>';
} elseif ( ! is_null( $for_country ) && is_array( $states ) ) {
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="state_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ? $args['placeholder'] : esc_html__( 'Select an option…', 'woocommerce' ) ) . '" data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '">
<option value="">' . esc_html__( 'Select an option…', 'woocommerce' ) . '</option>';
foreach ( $states as $ckey => $cvalue ) {
$field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . $cvalue . '</option>';
}
$field .= '</select>';
} else {
$field .= '<input type="text" class="form-control full-width ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" ' . implode( ' ', $custom_attributes ) . ' data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '"/>';
}
break;
case 'textarea':
$field .= '<textarea name="' . esc_attr( $key ) . '" class="form-control full-width ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $value ) . '</textarea>';
break;
case 'checkbox':
$field = '<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . '>
<input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="1" ' . checked( $value, 1, false ) . ' /> ' . $args['label'] . $required . '<span class="checkmark"></span></label>';
break;
case 'text':
case 'password':
case 'datetime':
case 'datetime-local':
case 'date':
case 'month':
case 'time':
case 'week':
case 'number':
case 'email':
case 'url':
case 'tel':
$field .= '<input ' . ($args['required'] ? "required" : "") . ' type="' . esc_attr( $args['type'] ) . '" class="form-control full-width ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
if($args['required']){
$field .= "<p style='display:none;' data-required-error-msg class='errorMsg'>Ovo polje je obvezno</p>";
}
break;
case 'select':
$field = '';
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
if ( '' === $option_key ) {
// If we have a blank option, select2 needs a placeholder.
if ( empty( $args['placeholder'] ) ) {
$args['placeholder'] = $option_text ? $option_text : __( 'Choose an option', 'woocommerce' );
}
$custom_attributes[] = 'data-allow_clear="true"';
}
$options .= '<option value="' . esc_attr( $option_key ) . '" ' . selected( $value, $option_key, false ) . '>' . esc_attr( $option_text ) . '</option>';
}
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '">
' . $options . '
</select>';
}
break;
case 'radio':
$label_id .= '_' . current( array_keys( $args['options'] ) );
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$field .= '<input type="radio" class="input-radio ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" ' . implode( ' ', $custom_attributes ) . ' id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
$field .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="radio ' . implode( ' ', $args['label_class'] ) . '">' . $option_text . '</label>';
}
}
break;
}
if ( ! empty( $field ) ) {
$field_html = '';
if ( $args['label'] && 'checkbox' !== $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class=" form-label ' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>';
}
$field_html .= $field;
if ( $args['description'] ) {
$field_html .= wp_kses_post( $args['description'] );
}
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$field = sprintf( $field_container, $container_class, $container_id, $field_html );
}
/**
* Filter by type.
*/
$field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value );
/**
* General filter on form fields.
*
* @since 3.4.0
*/
$field = apply_filters( 'woocommerce_form_field', $field, $key, $args, $value );
if ( $args['return'] ) {
return $field;
} else {
echo $field; // WPCS: XSS ok.
}
}
function get_lowest_price( $object_id, $regular_price ) {
if( $lowest_price_30_days = get_post_meta( $object_id, '_lowest_price_30_days', true ) ) {
return $lowest_price_30_days;
}
return $regular_price;
}
function get_lowest_price_html( $product ) {
$price_html = "";
if( is_admin() ) {
return $price_html;
}
// CHANGE PRICES ONLY IF PRODUCT IS ON SALE
if ( !$product->is_on_sale() ) {
return $price_html;
}
if( $product->get_type() == 'variable' ) {
// VARIABLE PRODUCTS
$prices = $product->get_variation_prices( false );
if ( !empty( $prices['price'] ) ) {
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
$min_reg_price = current( $prices['regular_price'] );
$max_reg_price = end( $prices['regular_price'] );
// CHANGE PRICES ONLY IF VARIANT PRICES ARE THE SAME, IN OTHER CASES USE WP DEFAULTS
// IF PRICES ARE DIFFERENT DISPLAY RANGE & SHOW PRICES FOR EACH VARIANT
if( !( WPLP_VARIANT_LOOP == 'min' && get_the_id() != get_queried_object()->ID ) && ( $min_reg_price !== $max_reg_price || $min_price !== $max_price ) ) {
return $price_html;
}
$regular_price = $product->get_variation_regular_price( 'min' );
$actual_price = wc_get_price_to_display( $product, array( 'price' => $min_price ) );
}
} else {
// OTHER PRODUCTS (SIMPLE, VARIANTS, GROUPED, ETC.)
$regular_price = $product->get_regular_price();
$actual_price = wc_get_price_to_display( $product );
}
$lowest_price_in_30_days = wc_get_price_to_display( $product, array( 'price' => get_lowest_price( $product->get_id(), $regular_price ) ) );
if( WPLP_DISPLAY_TYPE == 'text' ) {
$price_html = '<h4 class="lowest-price">' . __( 'Najniža cijena u zadnjih 30 dana', 'lowest-price' ) . ': <span class="lowest_amount">' . price_with_secondary($lowest_price_in_30_days) . $product->get_price_suffix() . '</span></h4><br />';
//$price_html .= '<span class="actual_price">' . __( 'Actual price', 'lowest-price' ) . ': <span class="actual_amount">' . wc_price( $actual_price ) . $product->get_price_suffix() . '</span></span>';
} else {
$price_html = wc_format_sale_price( $lowest_price_in_30_days, $actual_price ) . $product->get_price_suffix();
}
return $price_html;
}
function price_to_eur($price){
$conversion_rate = get_field('conversion_rate', 'options');
$formatted = round($price / $conversion_rate, wc_get_price_decimals());
$formatted = number_format($formatted, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator()) . " EUR";
return $formatted;
}
function price_to_hrk($price){
$conversion_rate = get_field('conversion_rate', 'options');
$formatted = round($price * $conversion_rate, wc_get_price_decimals());
$formatted = number_format($formatted, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator()) . " kn";
return $formatted;
}
function price_with_secondary($price){
$has_secondary_currency = get_field('has_secondary_currency', 'options');
$active_currency = get_woocommerce_currency();
if ($has_secondary_currency){
if ($active_currency == 'HRK'){
$formatted = price_to_eur($price) . " ~ " . wc_price($price);
} else{
$formatted = wc_price($price) . " ~ " . price_to_hrk($price);
}
}
else{
$formatted = wc_price($price);
}
return $formatted;
}
function secondary_price_output($price){
$has_secondary_currency = get_field('has_secondary_currency', 'options');
$active_currency = get_woocommerce_currency();
if ($has_secondary_currency){
if($active_currency == "HRK"){
return price_to_eur($price);
} else {
return price_to_hrk($price);
}
}
}
add_action('woocommerce_save_account_details', 'update_account', 10, 1);
function update_account($user_id){
require_once( __DIR__ . '/vendor/autoload.php' );
$mailchimp = new \MailchimpMarketing\ApiClient();
$mailchimp->setConfig([
'apiKey' => MAILCHIMP_API,
'server' => MAILCHIMP_SERVER
]);
$checked = $_POST['newsletter'];
$current = get_user_meta($user_id, 'subscribed_to_newsletter', true);
$user = get_user_by('id', $user_id);
$first_name = get_user_meta($user_id, 'first_name', true);
$last_name = get_user_meta($user_id, 'last_name', true);
if($current == 'yes' && !$checked){
update_user_meta($user_id, 'subscribed_to_newsletter', 'no');
$mailchimp->lists->setListMember(MAILCHIMP_LIST_ID, md5($user->user_email),
array(
'email_address' => $user->user_email,
'status' => 'unsubscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
)
);
} else if(($current == 'no' || !$current) && $checked == 'on'){
update_user_meta($user_id, 'subscribed_to_newsletter', 'yes');
$mailchimp->lists->setListMember(MAILCHIMP_LIST_ID, md5($user->user_email),
array(
'email_address' => $user->user_email,
'status_if_new' => 'subscribed',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
)
);
}
update_user_meta($user_id, 'order_comment', $_POST['shipping_notice']);
$customer = new WC_Customer( $user_id );
if ( $customer ) {
$customer->set_billing_phone( $_POST['shipping_phone'] );
$customer->set_shipping_company( $_POST['shipping_company'] );
$customer->set_shipping_address_1( $_POST['shipping_address'] );
$customer->set_shipping_city( $_POST['shipping_city'] );
$customer->set_shipping_postcode( $_POST['shipping_zip'] );
$customer->set_shipping_country( $_POST['shipping_state'] );
$customer->save();
}
}
add_action( 'woocommerce_checkout_order_processed', 'checkout_subscription', 1, 1 );
function checkout_subscription( $order_id ){
require_once( __DIR__ . '/vendor/autoload.php' );
$mailchimp = new \MailchimpMarketing\ApiClient();
$mailchimp->setConfig([
'apiKey' => MAILCHIMP_API,
'server' => MAILCHIMP_SERVER
]);
$user = wp_get_current_user();
$checked = $_POST['newsletter'];
$current = get_user_meta($user->ID, 'subscribed_to_newsletter', true);
if(is_user_logged_in()){
$first_name = get_user_meta($user->ID, 'first_name', true);
$last_name = get_user_meta($user->ID, 'last_name', true);
if($current == 'yes' && !$checked){
update_user_meta($user->ID, 'subscribed_to_newsletter', 'no');
$mailchimp->lists->setListMember(MAILCHIMP_LIST_ID, md5($user->user_email),
array(
'email_address' => $user->user_email,
'status_if_new' => 'subscribed',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
)
);
} else if(($current == 'no' || !$current) && $checked == 'on'){
update_user_meta($user->ID, 'subscribed_to_newsletter', 'yes');
$mailchimp->lists->setListMember(MAILCHIMP_LIST_ID, md5($user->user_email),
array(
'email_address' => $user->user_email,
'status_if_new' => 'subscribed',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
)
);
}
} else {
if ($checked == 'on'){
$first_name = $_POST['billing_first_name'];
$last_name = $_POST['billing_last_name'];
$mailchimp->lists->setListMember(MAILCHIMP_LIST_ID, md5($_POST['billing_email']),
array(
'email_address' => $_POST['billing_email'],
'status_if_new' => 'subscribed',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
)
);
}
}
}
?>