Skip to content

Instantly share code, notes, and snippets.

Avatar

Evgeny Lukin DxDiagDx

View GitHub Profile
@DxDiagDx
DxDiagDx / app.py
Created May 3, 2021
WordPress + Python: удалить пост
View app.py
import requests
import base64
url = "https://wholeblogs.com/wp-json/wp/v2/posts/"
postID = '1'
user = "your-username"
password = "your-application-password"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
@DxDiagDx
DxDiagDx / app.py
Last active May 3, 2021
WordPress + Python: редактировать пост
View app.py
import requests
import base64
url = "https://wholeblogs.com/wp-json/wp/v2/posts/"
postID = '1'
user = "your-username"
password = "your-application-password"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
@DxDiagDx
DxDiagDx / app.py
Created May 3, 2021
WordPress + Python: создать пост
View app.py
import requests
import base64
url = "https://wholeblogs.com/wp-json/wp/v2/posts"
user = "your-username"
password = "your-application-password"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = {
@DxDiagDx
DxDiagDx / python-test.py
Last active May 3, 2021
WordPress + Python: авторизация
View python-test.py
import requests
import json
import base64
url = "https://example.com/wp-json/wp/v2/posts"
user = "your-username"
password = "your-application-password"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
@DxDiagDx
DxDiagDx / functions.php
Created Apr 27, 2021
Woo Import: добавить произвольное поле в стандартный импорт
View functions.php
/**
* Register the 'Custom Column' column in the importer.
*
* @param array $options
* @return array $options
*/
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
function add_column_to_importer( $options ) {
// column slug => column name
$options['_price_types_1'] = 'Оптовая цена'; return $options;
@DxDiagDx
DxDiagDx / functions.php
Last active Apr 2, 2021
Woo: убираем не нужный / лишний тег H2 на страницах категорий
View functions.php
// Заменяем тег <H2> на <H3> у подкатегорий
remove_action( 'woocommerce_shop_loop_subcategory_title', 'woocommerce_template_loop_category_title', 10 );
add_action( 'woocommerce_shop_loop_subcategory_title', 'custom_woocommerce_template_loop_category_title', 10 );
function custom_woocommerce_template_loop_category_title( $category ) {
echo '<h3 class="woocommerce-loop-category__title">';
echo esc_html( $category->name );
if ( $category->count > 0 ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . esc_html( $category->count ) . ')</mark>', $category );
}
@DxDiagDx
DxDiagDx / functions.php
Created Apr 1, 2021
Woo: cкрыть категорию Uncategorized со страницы магазина
View functions.php
// Скрыть категорию Uncategorized со страницы магазина
add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) {
foreach( $terms as $key => $term ) {
if ( !in_array( $term->slug, array( 'uncategorized' ) ) ) { //ваш слаг категории
$new_terms[] = $term;
}
}
@DxDiagDx
DxDiagDx / functions.php
Created Apr 1, 2021
Woo: показать пустые категории без товаров в виджете
View functions.php
//* Used when the widget is displayed as a dropdown
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'sb_exclude_wc_widget_categories' );
//* Used when the widget is displayed as a list
add_filter( 'woocommerce_product_categories_widget_args', 'sb_exclude_wc_widget_categories' );
function sb_exclude_wc_widget_categories( $cat_args ) {
$term = get_term_by('slug', 'uncategorized', 'product_cat');
if ($term) {
$cat_args['exclude'] = array( $term -> term_id); // category to exclude
// remove category ID from include list if an include list exists
@DxDiagDx
DxDiagDx / functions.php
Last active Apr 6, 2021
Woo: показать пустые категории без товаров на страницах категорий
View functions.php
// Показать пустые категории без товаров
add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
@DxDiagDx
DxDiagDx / functions.php
Created Mar 28, 2021
Woo: перевод на русский язык единиц измерения
View functions.php
add_filter('woocommerce_format_weight', 'localize_weight_units');
function localize_weight_units($weight) {
if ( strpos($weight, 'kg') !== false ) {
return str_replace('kg', 'кг', $weight);
}
else {
return str_replace('g', 'гр', $weight);
}
}