Last active
July 29, 2020 13:46
-
-
Save zhukovec/15f56d59e3625ee4bf3fdeeab8b3717e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// меняем символ валюты | |
add_filter( 'woocommerce_currencies', 'add_my_currency' ); | |
function add_my_currency( $currencies ) { | |
$currencies['BYN'] = __( 'Белорусский рубль', 'woocommerce' ); | |
return $currencies; | |
} | |
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); | |
function add_my_currency_symbol( $currency_symbol, $currency ) { | |
switch( $currency ) { | |
case 'BYN': $currency_symbol = 'руб.'; break; | |
} | |
return $currency_symbol; | |
} | |
/* Меняем название кнопки добавить в корзину */ | |
add_filter( 'woocommerce_product_single_add_to_cart_text', 'tb_woo_custom_cart_button_text' ); | |
add_filter( 'woocommerce_product_add_to_cart_text', 'tb_woo_custom_cart_button_text' ); | |
function tb_woo_custom_cart_button_text() { | |
return __( 'Купить', 'woocommerce' ); | |
} | |
/* Изменяем стандартные названия */ | |
add_filter('gettext', 'translate_text'); | |
add_filter('ngettext', 'translate_text'); | |
function translate_text($translated) { | |
$translated = str_ireplace('Подытог', 'Итого', $translated); | |
$translated = str_ireplace('Таблица размеров', 'Таблица размеров', $translated); | |
$translated = str_ireplace('Хит продаж', 'Хит', $translated); | |
$translated = str_ireplace('О бренде', 'О бренде', $translated); | |
$translated = str_ireplace('Новый', 'Новинка', $translated); | |
return $translated; | |
} | |
// Товары без цены или с ценой равной нулю будут в конце списка | |
/*add_action( 'pre_get_posts', 'mik_exclude_category' ); | |
function mik_exclude_category( $query ) { | |
if ( $query->is_main_query() ) { | |
$query->set( 'meta_key', '_stock_status' ); | |
$query->set( 'orderby', array('meta_value' => 'ASC', 'date' => 'DESC') ); | |
} | |
}*/ | |
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) { | |
global $post; | |
$entity['mnp'] = get_post_meta( $post->ID, 'mpn_meta_key', true ); // Please hange mpn_meta_key with the meta from which you want to get the MPN value. | |
return $entity; | |
}); | |
/* | |
* Добавляем часть формы к фрагменту | |
*/ | |
add_filter( 'woocommerce_update_order_review_fragments', 'awoohc_add_update_form_billing', 99 ); | |
function awoohc_add_update_form_billing( $fragments ) { | |
$checkout = WC()->checkout(); | |
ob_start(); | |
?> | |
<div class="woocommerce-billing-fields__field-wrapper"> | |
<?php | |
$fields = $checkout->get_checkout_fields( 'billing' ); | |
foreach ( $fields as $key => $field ) { | |
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) { | |
$field['country'] = $checkout->get_value( $field['country_field'] ); | |
} | |
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); | |
} | |
?> | |
</div> | |
<?php | |
$art_add_update_form_billing = ob_get_clean(); | |
$fragments['.woocommerce-billing-fields'] = $art_add_update_form_billing; | |
return $fragments; | |
} | |
/* | |
* Убираем поля для конкретного способа доставки | |
*/ | |
add_filter( 'woocommerce_checkout_fields', 'awoohc_override_checkout_fields' ); | |
function awoohc_override_checkout_fields( $fields ) { | |
// получаем выбранные метод доставки | |
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); | |
// проверяем текущий метод и убираем не ненужные поля | |
if ( 'free_shipping:1' === $chosen_methods[0] ) { | |
unset( $fields['billing']['billing_company'] ); | |
unset( $fields['billing']['billing_address_1'] ); | |
unset( $fields['billing']['billing_address_2'] ); | |
unset( $fields['billing']['billing_city'] ); | |
unset( $fields['billing']['billing_postcode'] ); | |
unset( $fields['billing']['billing_country'] ); | |
unset( $fields['billing']['billing_state'] ); | |
unset( $fields['billing']['billing_phone'] ); | |
unset( $fields['billing']['billing_email'] ); | |
} | |
return $fields; | |
} | |
/* | |
* Обновление формы | |
*/ | |
add_action( 'wp_footer', 'awoohc_add_script_update_shipping_method' ); | |
function awoohc_add_script_update_shipping_method() { | |
if ( is_checkout() ) { | |
?> | |
<!--Скроем поле Страна. Если используется поле Страна, то следует убрать скрытие--> | |
<style> | |
#billing_country_field { | |
display: none !important; | |
} | |
</style> <!--Выполняем обновление полей при переключении доставки--> | |
<script> | |
jQuery(document).ready(function ($) { | |
$(document.body).on('updated_checkout updated_shipping_method', function (event, xhr, data) { | |
$('input[name^="shipping_method"]').on('change', function () { | |
$('.woocommerce-billing-fields__field-wrapper').block({ | |
message: null, | |
overlayCSS: { | |
background: '#fff', | |
'z-index': 1000000, | |
opacity: 0.3 | |
} | |
}); | |
}); | |
var first_name = $('#billing_first_name').val(), | |
last_name = $('#billing_last_name').val(), | |
phone = $('#billing_phone').val(), | |
email = $('#billing_email').val(); | |
$(".woocommerce-billing-fields__field-wrapper").html(xhr.fragments[".woocommerce-billing-fields"]); | |
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_first_name"]').val(first_name); | |
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_last_name"]').val(last_name); | |
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_phone"]').val(phone); | |
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_email"]').val(email); | |
$('.woocommerce-billing-fields__field-wrapper').unblock(); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment