Skip to content

Commit

Permalink
9.7 release (#519)
Browse files Browse the repository at this point in the history
* bump version and readme update
* auto tests
* fix - похоже конвертация валюты сломалась... #516
* improve #522, #520, #516
* small fixes
  • Loading branch information
aiiddqd authored Nov 5, 2023
1 parent b26645a commit 26222ff
Show file tree
Hide file tree
Showing 18 changed files with 10,862 additions and 6,526 deletions.
162 changes: 93 additions & 69 deletions includes/CategoriesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,104 @@

namespace WooMS;

if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

/**
* Import Product Categories from MoySklad
*/
class CategoriesFilter
{

/**
* WooMS_Import_Product_Categories constructor.
*/
public static function init()
{

add_action('admin_init', array(__CLASS__, 'settings_init'), 50);
add_filter('wooms_url_get_products_filters', array(__CLASS__, 'product_add_filter_by_folder'), 10);
add_filter('wooms_url_get_bundle_filter', array(__CLASS__, 'product_add_filter_by_folder'), 10);
add_filter('wooms_url_get_service_filter', array(__CLASS__, 'product_add_filter_by_folder'), 10);

}


/**
* Добавляем фильтр по папке
* Если выбрана группа для синка
* Use $url_api = apply_filters('wooms_url_get_products', $url);
*/
public static function product_add_filter_by_folder($filters)
{
if ( ! $groups = get_option('wooms_set_folders')) {
return $filters;
}

$groups = explode(',', $groups);

if(empty($groups)){
return $filters;
}

foreach($groups as $group){
$filters[] = 'pathName~=' . trim($group);
}

return $filters;
}


/**
* Settings UI
*/
public static function settings_init()
{

register_setting('mss-settings', 'wooms_set_folders');
add_settings_field(
$name = 'wooms_set_folders',
$title = 'Группы товаров для фильтрации',
$render = function($args){
printf('<input type="text" name="%s" value="%s" size="50" />', $args['key'], $args['value']);
printf('<p><small>%s</small></p>',
'Тут можно указать группы для фильтрации товаров через запятую. Например: "Мебель/Диваны,Пицца,Одежда/Обувь/Ботинки"'
);
},
$setings = 'mss-settings',
$group = 'wooms_product_cat',
$arts = [
'key' => 'wooms_set_folders',
'value' => get_option('wooms_set_folders'),
]
);

}
class CategoriesFilter {

public static $groups = [];
/**
* WooMS_Import_Product_Categories constructor.
*/
public static function init() {
add_action( 'admin_init', array( __CLASS__, 'add_settings' ), 50 );

$groups = get_option( 'wooms_set_folders' );

if ( $groups ) {
self::$groups = explode( ',', $groups );
}

if ( empty( self::$groups ) ) {
return;
}

add_filter( 'wooms_url_get_products_filters', array( __CLASS__, 'product_add_filter_by_folder' ), 10 );
add_filter( 'wooms_url_get_bundle_filter', array( __CLASS__, 'product_add_filter_by_folder' ), 10 );
add_filter( 'wooms_url_get_service_filter', array( __CLASS__, 'product_add_filter_by_folder' ), 10 );
add_filter( 'wooms_productfolder', array( __CLASS__, 'filter_folders' ), 10 );
}

public static function filter_folders( $filter_folders ) {

$new_folders = [];
foreach ( $filter_folders as $folder ) {

if(empty($folder['pathName'])){
foreach(self::$groups as $group){
if(str_starts_with($folder['name'], $group)) {
$new_folders[] = $folder;
}
}
} else {
foreach(self::$groups as $group){
if(str_starts_with($folder['pathName'], $group)){
$new_folders[] = $folder;
}
}

}
}

if ( $new_folders ) {
return $new_folders;
}

return $filter_folders;
}

/**
* Добавляем фильтр по папке
* Если выбрана группа для синка
* Use $url_api = apply_filters('wooms_url_get_products', $url);
*/
public static function product_add_filter_by_folder( $filters ) {

foreach ( self::$groups as $group ) {
$filters[] = 'pathName~=' . trim( $group );
}
return $filters;
}


/**
* Settings UI
*/
public static function add_settings() {

register_setting( 'mss-settings', 'wooms_set_folders' );
add_settings_field(
$name = 'wooms_set_folders',
$title = 'Группы товаров для фильтрации',
$render = function ($args) {
printf( '<input type="text" name="%s" value="%s" size="50" />', $args['key'], $args['value'] );
printf( '<p><small>%s</small></p>',
'Тут можно указать группы для фильтрации товаров через запятую. Например: "Мебель/Диваны,Пицца,Одежда/Обувь/Ботинки"'
);
},
$setings = 'mss-settings',
$group = 'wooms_product_cat',
$arts = [
'key' => 'wooms_set_folders',
'value' => get_option( 'wooms_set_folders' ),
]
);

}

}

Expand Down
75 changes: 45 additions & 30 deletions includes/CurrencyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* Additional notes for send order
*
* @todo need refactoring after https://github.com/wpcraft-ru/wooms/issues/516
*/
class CurrencyConverter
{
Expand All @@ -17,10 +19,12 @@ public static function init()
add_filter('wooms_product_price', [__CLASS__, 'chg_price'], 33, 4);
add_filter('wooms_sale_price', [__CLASS__, 'chg_price'], 32, 4);

add_action('wooms_main_walker_started', [__CLASS__, 'cache_data']);

add_action('admin_init', array(__CLASS__, 'add_settings'), 50);
}

public static function chg_price($price, $data_api, $product_id, $price_meta)
public static function chg_price($price, $data_api, $product_id, $price_meta)
{
if (!self::is_enable()) {
return $price;
Expand All @@ -30,23 +34,9 @@ public static function chg_price($price, $data_api, $product_id, $price_meta)
return $price;
}

if (!$currency = get_transient('wooms_currency_api')) {
$currency = request('entity/currency/');
set_transient('wooms_currency_api', $currency, HOUR_IN_SECONDS);
}
$currency_ms = self::get_currency();

$woocommerce_currency = get_woocommerce_currency();
$api_currency = self::get_currency_code_price_meta($price_meta);

if(empty($api_currency)){
return $price;
}

if ($woocommerce_currency == $api_currency) {
return $price;
}

$price_by_rate = self::update_price_by_rate($price, $api_currency);
$price_by_rate = self::get_price_converted($price_meta, $currency_ms);

do_action(
'wooms_logger',
Expand All @@ -55,24 +45,52 @@ public static function chg_price($price, $data_api, $product_id, $price_meta)
[
'цена исходная' => $price,
'цена после конвертации' => $price_by_rate,
'валюта сайта' => $woocommerce_currency,
'валюта api' => $api_currency,
]
);
// if ($product_id == 26226) {
// dd($api_currency, $woocommerce_currency, $price, $currency, $price_meta);
// }

return $price_by_rate;
}

public static function get_price_converted($price_meta, $currency_ms){

public static function update_price_by_rate($price = 0, $api_currency = 'RUB'){
if (!$currency = get_transient('wooms_currency_api')) {
$currency = request('entity/currency');
set_transient('wooms_currency_api', $currency, HOUR_IN_SECONDS);
$woocommerce_currency = get_woocommerce_currency();

$rate = 1;

foreach($currency_ms['rows'] as $currency_row){
if($currency_row['meta']['href'] == $price_meta['currency']['meta']['href']){
$rate = $currency_row['rate'];

}
}

$price = $price_meta['value'] * $rate;
return $price;

}

public static function get_currency(){
$currency = get_transient('wooms_currency_api');
if($currency){
return $currency;
}

$currency = request('entity/currency/');
set_transient('wooms_currency_api', $currency, DAY_IN_SECONDS);
return $currency;
}

public static function cache_data(){
delete_transient('wooms_currency_api');
$currency = request('entity/currency/');
set_transient('wooms_currency_api', $currency, DAY_IN_SECONDS);
}


public static function update_price_by_rate($price = 0, $api_currency = 'RUB'){

$currency = self::get_currency();

$rate = 1;

foreach($currency['rows'] as $currency_row){
Expand All @@ -95,10 +113,7 @@ public static function get_currency_code_price_meta($price_meta = [])

$price_currency_href = $price_meta['currency']['meta']['href'];

if (!$currency = get_transient('wooms_currency_api')) {
$currency = request('entity/currency');
set_transient('wooms_currency_api', $currency, HOUR_IN_SECONDS);
}
$currency = self::get_currency();

if (empty($currency['rows'])) {
return false;
Expand Down
16 changes: 13 additions & 3 deletions includes/ProductVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,24 @@ public static function update_variation( $row ) {
}

if(empty($row['product']['meta']['href'])){
ddcli($row);
throw new \Error('empty $row[product][meta][href]');
}

$product_href = $row['product']['meta']['href'];
$product_id = self::get_product_id_by_uuid( $product_href );
$product_parent = wc_get_product( $product_id );

if(empty($product_parent)){
do_action(
'wooms_logger_error',
__CLASS__,
sprintf( 'Нет базового продукта для вариации. %s', json_encode( ['product_id' => $product_id, '$row id' => $row ] ) )
);

return null;

}

if ( ! $product_parent->is_type( 'variable' ) ) {
$product_parent = new \WC_Product_Variable( $product_parent );
$product_parent->save();
Expand Down Expand Up @@ -645,8 +657,6 @@ public static function display_state() {
$strings[] = sprintf( 'Журнал обработки: <a href="%s">открыть</a>', admin_url( 'admin.php?page=wc-status&tab=logs' ) );
}

$strings[] = sprintf( 'Количество обработанных записей: %s', empty( self::get_state( 'count' ) ) ? 0 : self::get_state( 'count' ) );

?>
<div>
<?php
Expand Down
18 changes: 9 additions & 9 deletions includes/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function process_rows( $rows = [] ) {
throw new Error('$rows is empty');
}

$ids = [];
foreach ( $rows as $row ) {
$ids[] = $row['id'];

if ( apply_filters( 'wooms_skip_product_import', false, $row ) ) {
continue;
Expand All @@ -125,12 +127,18 @@ function process_rows( $rows = [] ) {
}

$data = apply_filters( 'wooms_product_data', [], $row );

product_update( $row, $data );
}


return true;
} catch (Throwable $e) {
do_action( 'wooms_logger_error', __NAMESPACE__, 'Главный обработчик завершился с ошибкой... ' . $e->getMessage() );

$message = sprintf("wooms process fails: %s, ids: %s, code: %s", $e->getMessage(), json_encode($ids), $e->getCode());
do_action( 'wooms_logger_error', __NAMESPACE__, $message );
error_log($message);

return false;
}

Expand Down Expand Up @@ -266,8 +274,6 @@ function product_update( array $row, array $data = [] ) {
*/
$product = apply_filters( 'wooms_product_save', $product, $data_api, $product_id );



//save data of source
if ( apply_filters( 'wooms_logger_enable', false ) ) {
$product->update_meta_data( 'wooms_data_api', json_encode( $data_api, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) );
Expand Down Expand Up @@ -341,14 +347,8 @@ function product_update( array $row, array $data = [] ) {

$product = apply_filters( 'wooms_product_update', $product, $row, $data );

// return $product;

$product_id = $product->save();

if(empty(intval($product_id))){
throw new Error('$product_id is broke');
}

do_action(
'wooms_logger',
__NAMESPACE__,
Expand Down
Loading

0 comments on commit 26222ff

Please sign in to comment.