Untitled
unknown
plain_text
2 years ago
24 kB
8
Indexable
/* hide both products that have no price or that have zero price */
add_action( 'pre_get_posts', 'custom_product_archive_query' );
function custom_product_archive_query( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if (isset($_GET['min-price']) || isset($_GET['max-price'])) {
return;
}
if ( is_post_type_archive( 'product' ) || is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
$meta_query = ($mq = $query->get( 'meta_query' )) && is_array($mq) ? $mq : [];
$meta_query[] = [
'relation' => 'AND',
array(
'key' => '_price',
'value' => 1,
'type' => 'NUMERIC',
'compare' => '>=',
),
array(
'key' => '_price',
'compare' => 'EXISTS',
),
];
$query->set( 'meta_query', $meta_query );
}
}
/* See query tegi väga aeglaseks toodete kuvamise */
/*
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
// Exclude front page
if( is_front_page() ) return $meta_query;
$meta_query['relation'] = 'OR';
$meta_query[] = array(
'key' => '_price',
'value' => '',
'type' => 'numeric',
'compare' => '!='
);
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '!='
);
return $meta_query;
}
*/
/* Remove shipping options from cart */
function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
// Add custom button after cart table for b2b users to direct to purchase list
function add_custom_button_after_cart_table() {
if (is_user_logged_in() && (intval(get_option('b2bking_enable_purchase_lists_setting', 1)) === 1)){
// should not appear if user has a dynamic rule replace prices with quote
$user_id = get_current_user_id();
$account_type = get_user_meta($user_id,'b2bking_account_type', true);
if ($account_type === 'subaccount'){
// for all intents and purposes set current user as the subaccount parent
$parent_user_id = get_user_meta($user_id, 'b2bking_account_parent', true);
$user_id = $parent_user_id;
}
if (get_transient('b2bking_replace_prices_quote_user_'.$user_id) !== 'yes'){
?>
<a href="/minu-konto/purchase-lists" id="komplektidnupp" class="button">Minu komplektid</a>
<?php
}
}
}
add_action('woocommerce_cart_actions', 'add_custom_button_after_cart_table');
// Add the checkbox field to the checkout form
add_action('woocommerce_after_order_notes', 'add_custom_checkbox_to_checkout');
function add_custom_checkbox_to_checkout($checkout) {
echo '<div id="custom_checkbox_field">';
woocommerce_form_field('custom_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Soovin hinnapakkumist kodumasinate paigaldusele'),
'required' => false,
), $checkout->get_value('custom_checkbox'));
echo '</div>';
}
// Save the custom field value to the order meta
add_action('woocommerce_checkout_create_order', 'save_custom_checkbox_to_order_meta');
function save_custom_checkbox_to_order_meta($order) {
if ($_POST['custom_checkbox']) {
$order->update_meta_data('_custom_checkbox', 'yes');
}
}
// Display the custom field value on the order edit page
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_checkbox_on_order_edit');
function display_custom_checkbox_on_order_edit($order) {
$custom_checkbox = $order->get_meta('_custom_checkbox');
echo '<p><strong>' . __('Soovin hinnapakkumist kodumasinate paigaldusele') . ':</strong> ' . ($custom_checkbox ? 'Jah' : 'Ei') . '</p>';
}
// Add "sis. KM" after product price on single product page
add_filter( 'woocommerce_get_price_html', 'text_after_price' );
function text_after_price($price){
if (is_product()) {
$text_to_add_after_price = ' <small style="font-size: 12px; color: #BBB;"> (sis. KM)</small>';
return $price . $text_to_add_after_price;
} else {
return $price;
}
}
/*
* Set this to the endpoint you want to use, e.g.:
* 'orders', 'downloads', 'edit-account'
*/
const WPT_DEFAULT_MYACCOUNT_ENDPOINT = 'orders';
/**
* Adjust the My Account menu items.
*/
function custom_remove_dashboard_from_myaccount($menu_items, $endpoints) {
// Remove "Dashboard" from the list of available menu items.
if (array_key_exists('dashboard', $menu_items)) {
unset($menu_items['dashboard']);
}
return $menu_items;
}
add_filter('woocommerce_account_menu_items', 'custom_remove_dashboard_from_myaccount', 90, 2);
/**
* Redirect /my-account/ to /my-account/WPT_DEFAULT_MYACCOUNT_ENDPOINT/
*/
function custom_redirect_from_dashboard() {
$dashboard_query_path = '';
$current_query_path = '';
if (is_user_logged_in() && function_exists('wc_get_account_endpoint_url')) {
// Get the Dashboard (my-account) query path.
// This is usually "/my-account/"
$dashboard_query_path = trailingslashit(parse_url(wc_get_account_endpoint_url('dashboard'), PHP_URL_PATH));
// Get this query's path, which could be anything:
// /my-account/
// /my-account/orders/
// /some-other-content/
global $wp;
$current_url = home_url($wp->request);
$current_query_path = trailingslashit(parse_url($current_url, PHP_URL_PATH));
}
if (empty($dashboard_query_path) || empty($current_query_path)) {
// If we don't know what the dashboar'ds query path is, don't do
// anything. Maybe WooCommerce isn't installed.
} elseif ($dashboard_query_path !== $current_query_path) {
// If the current query path is NOT the dashboard query path,
// don't do anything special here.
} elseif (empty($redirect_to_url = wc_get_account_endpoint_url(WPT_DEFAULT_MYACCOUNT_ENDPOINT))) {
// If we can't get the URL of redirect-to endpoint we want, don't
// do anything.
} else {
// Redirect the browser: $redirect_to_url
wp_safe_redirect($redirect_to_url);
exit();
}
}
add_action('template_redirect', 'custom_redirect_from_dashboard');
// Change account menu items and their order
function custom_woocommerce_account_menu_items($items) {
$new_items = array();
// Specify the order of menu items
$new_items['orders'] = $items['orders']; // Tellimused
$new_items['bulkorder'] = $items['bulkorder']; // Komplekteerimine
$new_items['purchase-lists'] = $items['purchase-lists']; // Komplektid
$new_items['edit-address'] = $items['edit-address']; // Aadressid
$new_items['edit-account'] = $items['edit-account']; // Konto andmed
$new_items['customer-logout'] = $items['customer-logout']; // Logi välja
return $new_items;
}
add_filter('woocommerce_account_menu_items', 'custom_woocommerce_account_menu_items', 9999);
function modify_compare_link() {
//Works only on 'vordle' page
if(is_page(3163)) {
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', (event) => {
// select the anchor inside the specified div
var link = document.querySelector('.rey-compare-emptyPage-content a');
// if the link exists, change its href
if (link) {
link.href = '/e-pood';
}
});
</script>
<?php
}
}
add_action('wp_footer', 'modify_compare_link');
//Translate strings
function translate_strings( $translated ) {
$text = array(
'Hulgitellimuse vorm' => 'Komplekteerimine',
'Järeltellimisel' => 'Tellimisel',
);
$translated = str_ireplace( array_keys( $text ), $text, $translated );
return $translated;
}
add_filter( 'gettext', 'translate_strings', 20 );
/*if (is_user_logged_in()) {
$userId = get_current_user_id();
$group = get_user_meta( $userId, 'b2bking_customergroup', true );
$isB2B = get_user_meta( $userId, 'b2bking_b2buser', true );
if ($isB2B == 'yes' && $group != 'no') {
function reyajaxfilter_register_price_filter_widget() {
//
}
}
}*/
add_filter( 'woocommerce_get_catalog_ordering_args', 'woocommerceGetCatalogOrderingArgs' );
function woocommerceGetCatalogOrderingArgs( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'vd-price' == $orderby_value ) {
$userId = get_current_user_id();
$group = get_user_meta( $userId, 'b2bking_customergroup', true );
switch ($group) {
case 2102:
$args['meta_key'] = 'b2bking_regular_product_price_group_2102';
break;
case 6222:
$args['meta_key'] = 'b2bking_regular_product_price_group_6222';
break;
case 8180:
$args['meta_key'] = 'b2bking_regular_product_price_group_8180';
break;
default:
$args['meta_key'] = '_price';
}
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
}
if ( 'vd-price-desc' == $orderby_value ) {
$userId = get_current_user_id();
$group = get_user_meta( $userId, 'b2bking_customergroup', true );
switch ((int) $group) {
case 2102:
$args['meta_key'] = 'b2bking_regular_product_price_group_2102';
break;
case 6222:
$args['meta_key'] = 'b2bking_regular_product_price_group_6222';
break;
case 8180:
$args['meta_key'] = 'b2bking_regular_product_price_group_8180';
break;
default:
$args['meta_key'] = '_price';
}
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
}
return $args;
}
function remove_price_orderby_optionss($orderby_options) {
unset($orderby_options['price']);
unset($orderby_options['price-desc']);
return $orderby_options;
}
add_filter('woocommerce_catalog_orderby', 'remove_price_orderby_optionss', 999);
add_filter( 'woocommerce_default_catalog_orderby_options', 'woocommerceCatalogOrderBy' );
add_filter( 'woocommerce_catalog_orderby', 'woocommerceCatalogOrderBy', 999 );
function woocommerceCatalogOrderBy( $sortby ) {
$sortby['vd-price'] = __('Odavamad enne', 'hektaur');
$sortby['vd-price-desc'] = __('Kallimad enne', 'hektaur');
return $sortby;
}
/*Hide products that dont have a price or have 0 price
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
$meta_query['relation'] = 'OR';
$meta_query[] = array(
'key' => '_price',
'value' => '',
'type' => 'numeric',
'compare' => '!='
);
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '!='
);
return $meta_query;
}
*/
function exclude_zero_price_products_from_ajax_search( $query ) {
// Check if the query is a search and is AJAX
if ( $query->is_search() && defined('DOING_AJAX') && DOING_AJAX ) {
// Exclude products where the price is empty or zero
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
),
array(
'key' => '_price',
'value' => '',
'compare' => '!=',
)
);
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_zero_price_products_from_ajax_search' );
// Lisa uus väli toote andmete kasti
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_url_field' );
function woo_add_custom_url_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
// URL väli
woocommerce_wp_text_input(
array(
'id' => 'tooteleht',
'label' => __( 'Tootelehe URL', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Tootelehe .pdf-i URL', 'woocommerce' )
)
);
echo '</div>';
}
// Salvesta meta väli
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_url_field_save' );
function woo_add_custom_url_field_save( $post_id ) {
$woocommercetooteleht = $_POST['tooteleht'];
if ( ! empty( $woocommercetooteleht ) ) {
// Valideeri, et sisestatud väärtus on URL
if ( filter_var( $woocommercetooteleht, FILTER_VALIDATE_URL ) ) {
update_post_meta( $post_id, 'tooteleht', esc_url( $woocommercetooteleht ) );
} else {
// Lisa teade, kui URL ei ole kehtiv
wc_add_notice( __( 'Palun sisesta kehtiv URL.', 'woocommerce' ), 'error' );
}
}
}
function loadtooteleht() {
global $post;
// Kontrolli, kas see on toote leht
if ( is_product() ) {
$custom_meta_url = get_post_meta( $post->ID, 'tooteleht', true );
if ( ! empty( $custom_meta_url ) ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
let custom_meta_url = '<?php echo esc_url( $custom_meta_url ); ?>';
$('.woocommerce-product-attributes.shop_attributes tbody').append(`
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--custom-url">
<th class="woocommerce-product-attributes-item__label">Tooteleht</th>
<th class="woocommerce-product-attributes-item__value"><a href="${custom_meta_url}" target="_blank">link</a></th>
</tr>
`);
});
</script>
<?php
}
}
}
add_action( 'wp_footer', 'loadtooteleht' );
add_action( 'save_post_product', function ( $post_id ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check if it's a product
if ( 'product' != get_post_type( $post_id ) )
return $post_id;
// Get min and max price for each group
$minPrice8180 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_8180', true );
$maxPrice8180 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_8180', true );
if ( empty( $minPrice8180 ) ) {
$minPrice8180 = get_post_meta( $post_id, '_regular_price', true );
$maxPrice8180 = get_post_meta( $post_id, '_regular_price', true );
}
$minPrice6222 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_6222', true );
$maxPrice6222 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_6222', true );
if ( empty( $minPrice6222 ) ) {
$minPrice6222 = get_post_meta( $post_id, '_regular_price', true );
$maxPrice6222 = get_post_meta( $post_id, '_regular_price', true );
}
$minPrice2102 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_2102', true );
$maxPrice2102 = get_post_meta( $post_id, 'b2bking_regular_product_price_group_2102', true );
if ( empty( $minPrice2102 ) ) {
$minPrice2102 = get_post_meta( $post_id, '_regular_price', true );
$maxPrice2102 = get_post_meta( $post_id, '_regular_price', true );
}
global $wpdb;
$tableGroup8180 = $wpdb->prefix . 'group_8180_lookup'; // Äriklient
$tableGroup6222 = $wpdb->prefix . 'group_6222_lookup'; // Mööbil valmistajad
$tableGroup2102 = $wpdb->prefix . 'group_2102_lookup'; // Hulgi müüjad
// Insert or update data in the table
$wpdb->replace(
$tableGroup8180,
array(
'product_id' => $post_id,
'min_price' => $minPrice8180,
'max_price' => $maxPrice8180
),
array( '%d', '%f', '%f' )
);
$wpdb->replace(
$tableGroup6222,
array(
'product_id' => $post_id,
'min_price' => $minPrice6222,
'max_price' => $maxPrice6222
),
array( '%d', '%f', '%f' )
);
$wpdb->replace(
$tableGroup2102,
array(
'product_id' => $post_id,
'min_price' => $minPrice2102,
'max_price' => $maxPrice2102
),
array( '%d', '%f', '%f' )
);
});
add_filter( 'woocommerce_price_filter_sql', function( $sql, $metaQuerySql, $taxQuerySql ) {
global $wpdb;
$userId = get_current_user_id();
$group = get_user_meta( $userId, 'b2bking_customergroup', true );
$search = reyajaxfilter_search_query();
$searchQuerySql = $search ? ' AND ' . $search : '';
if ($group === 'no') {
$sql = "
SELECT min( min_price ) as min_price, MAX( max_price ) as max_price
FROM {$wpdb->wc_product_meta_lookup}
WHERE product_id IN (
SELECT ID FROM {$wpdb->posts}
" . $taxQuerySql['join'] . $metaQuerySql['join'] . "
WHERE {$wpdb->posts}.post_type IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_post_type', array( 'product' ) ) ) ) . "')
AND {$wpdb->posts}.post_status = 'publish'
" . $taxQuerySql['where'] . $metaQuerySql['where'] . $searchQuerySql . '
)';
} else {
$sql = "
SELECT min( min_price ) as min_price, MAX( max_price ) as max_price
FROM {$wpdb->prefix}group_{$group}_lookup
WHERE product_id IN (
SELECT ID FROM {$wpdb->posts}
" . $taxQuerySql['join'] . $metaQuerySql['join'] . "
WHERE {$wpdb->posts}.post_type IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_post_type', array( 'product' ) ) ) ) . "')
AND {$wpdb->posts}.post_status = 'publish'
" . $taxQuerySql['where'] . $metaQuerySql['where'] . $searchQuerySql . '
)';
}
return $sql;
}, 10, 3 );
add_action('init', function () {
if (isset($_GET['regenerate_b2b_lookup']) && $_GET['regenerate_b2b_lookup'] === 'yes') {
global $wpdb;
// Get all product IDs
$product_ids = get_posts(array(
'post_type' => 'product',
'numberposts' => -1,
'fields' => 'ids'
));
foreach ($product_ids as $product_id) {
global $wpdb;
$tableGroup8180 = $wpdb->prefix . 'group_8180_lookup'; // Äriklient
$tableGroup6222 = $wpdb->prefix . 'group_6222_lookup'; // Mööbil valmistajad
$tableGroup2102 = $wpdb->prefix . 'group_2102_lookup'; // Hulgi müüjad
// Get min and max price for each group
$minPrice8180 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_8180', true );
$maxPrice8180 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_8180', true );
if ( empty( $minPrice8180 ) ) {
$minPrice8180 = get_post_meta( $product_id, '_regular_price', true );
$maxPrice8180 = get_post_meta( $product_id, '_regular_price', true );
}
$minPrice6222 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_6222', true );
$maxPrice6222 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_6222', true );
if ( empty( $minPrice6222 ) ) {
$minPrice6222 = get_post_meta( $product_id, '_regular_price', true );
$maxPrice6222 = get_post_meta( $product_id, '_regular_price', true );
}
$minPrice2102 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_2102', true );
$maxPrice2102 = get_post_meta( $product_id, 'b2bking_regular_product_price_group_2102', true );
if ( empty( $minPrice2102 ) ) {
$minPrice2102 = get_post_meta( $product_id, '_regular_price', true );
$maxPrice2102 = get_post_meta( $product_id, '_regular_price', true );
}
// Insert or update data in the table
$wpdb->replace(
$tableGroup8180,
array(
'product_id' => $product_id,
'min_price' => $minPrice8180,
'max_price' => $maxPrice8180
),
array( '%d', '%f', '%f' )
);
$wpdb->replace(
$tableGroup6222,
array(
'product_id' => $product_id,
'min_price' => $minPrice6222,
'max_price' => $maxPrice6222
),
array( '%d', '%f', '%f' )
);
$wpdb->replace(
$tableGroup2102,
array(
'product_id' => $product_id,
'min_price' => $minPrice2102,
'max_price' => $maxPrice2102
),
array( '%d', '%f', '%f' )
);
}
}
if (isset($_GET['create_tables']) && $_GET['create_tables'] === 'yes') {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$tableGroup8180 = $wpdb->prefix . 'group_8180_lookup'; // Äriklient
$tableGroup6222 = $wpdb->prefix . 'group_6222_lookup'; // Mööbil valmistajad
$tableGroup2102 = $wpdb->prefix . 'group_2102_lookup'; // Hulgi müüjad
// SQL queries for table creation
$sqlGroup8180 = "CREATE TABLE $tableGroup8180 (
product_id bigint(20) NOT NULL,
min_price decimal(10, 2) NOT NULL,
max_price decimal(10, 2) NOT NULL,
PRIMARY KEY (product_id)
) $charset_collate;";
$sqlGroup6222 = "CREATE TABLE $tableGroup6222 (
product_id bigint(20) NOT NULL,
min_price decimal(10, 2) NOT NULL,
max_price decimal(10, 2) NOT NULL,
PRIMARY KEY (product_id)
) $charset_collate;";
$sqlGroup2102 = "CREATE TABLE $tableGroup2102 (
product_id bigint(20) NOT NULL,
min_price decimal(10, 2) NOT NULL,
max_price decimal(10, 2) NOT NULL,
PRIMARY KEY (product_id)
) $charset_collate;";
// Include upgrade.php
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the tables
dbDelta($sqlGroup8180);
dbDelta($sqlGroup6222);
dbDelta($sqlGroup2102);
}
});
add_filter('reycore/woocommerce/search/ajax_args', function($args) {
$currentUserId = get_current_user_id();
$accountType = get_user_meta($currentUserId,'b2bking_account_type', true);
if ($accountType === 'subaccount'){
$parentUserId = get_user_meta($currentUserId, 'b2bking_account_parent', true);
$currentUserId = $parentUserId;
}
$allTheIDs = get_transient('b2bking_user_'.$currentUserId.'_ajax_visibility');
$allProductIds = get_transient('b2bking_all_products_ids');
if ( ! $allProductIds){
$allProductIds = array(
'posts_per_page' => -1,
'post_type' => 'product',
'fields' => 'ids',
);
set_transient('b2bking_all_products_ids', get_posts($allProductIds));
}
$allTheIDs = apply_filters('b2bking_ids_post_in_visibility', $allTheIDs);
$allIds = array_diff($allProductIds, $allTheIDs);
$args['post__not_in'] = $allIds;
return $args;
});Editor is loading...