Plugin Directory

Changeset 2578509


Ignore:
Timestamp:
08/05/2021 05:58:59 AM (5 years ago)
Author:
redmonkey73
Message:

Modify function for set correct sub-category for product

Location:
bonus-plus-wp/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • bonus-plus-wp/trunk/bonus-plus-wp.php

    r2577633 r2578509  
    11<?php
    2 
    32/**
    43 * Plugin Name: Bonus-Plus-wp
     
    1615 * WP requires at least: 5.0
    1716 * Tested up to: 5.8
    18  * Version: 1.6
     17 * Version: 1.7
    1918 */
    2019namespace BPWP;
     
    2928    public static function init()
    3029    {
    31         define('BPWP_PLUGIN_VERSION', '1.6');
     30        define('BPWP_PLUGIN_VERSION', '1.7');
    3231
    3332        require_once __DIR__ . '/functions.php';
  • bonus-plus-wp/trunk/inc/ClientProfile.php

    r2576578 r2578509  
    164164                'GET'
    165165            );
    166             update_user_meta($user_id, 'bonus-plus', $res);
     166            update_user_meta($user_id, 'bonus-plus', $res['request']);
    167167        }
    168168    }
  • bonus-plus-wp/trunk/inc/WooProductCatExport.php

    r2577633 r2578509  
    5656        add_settings_field(
    5757            $id = 'bpwp_wrong_products_action',
    58             $title = __('Действие при импорте с товаром у которого больше 1 категории:', 'bonus-plus-wp'),
     58            $title = __('Действие при экспорте с товаром у которого больше 1 категории:', 'bonus-plus-wp'),
    5959            $callback = array(__CLASS__, 'display_wrong_products_action'),
    6060            $page = 'bpwp-settings',
     
    8484                    '<option value="%s" %s>%s</option>',
    8585                    'empty',
    86                     selected('default', $wrong_products_action, false),
     86                    selected('empty', $wrong_products_action, false),
    8787                    __( 'Импортировать товар, без категории', 'bonus-plus-wp')
    8888                );
     
    191191            foreach ($products as $product) {
    192192                $productId = $product->get_id();
    193                 // получим все категории продукта
    194                 $pCatIds = $product->get_category_ids();
    195                 $categoriesCount = count($pCatIds);
    196 
    197                 if ($categoriesCount == 1) {
    198                     $pCatId = $pCatIds[0];
    199                     $product = [
     193                $productName = $product->get_name();
     194                $productCategory = self::bpwp_get_product_child_category($productId);
     195               
     196                if (count($productCategory) !== 1) {
     197                    // У товара больше 1 дочерней категории или нет, действуем по опции
     198                    $wrong_products_action = get_option('bpwp_wrong_products_action');
     199                    switch ($wrong_products_action) {
     200                        case 'hide':
     201                            do_action(
     202                                'bpwp_logger_error',
     203                                $type = __CLASS__,
     204                                $title = __('Экспорт товаров в Бонус+, товар пропущен', 'bonus-plus-wp'),
     205                                $desc = sprintf(__('У товара [%s] %s более 1 дочерней категории', 'bonus-plus-wp'), $productId, $productName),
     206                            );
     207                            break;
     208                        case 'empty':
     209                            $exportProduct = [
     210                                'id'  => $productId,
     211                                'pid' => 0,
     212                                'n'   => $productName,
     213                                'g'   => false,
     214                            ];
     215                            $productList[] = $exportProduct;
     216                            break;
     217                    }
     218                    $wrongProducts[$productId] = $productName;
     219                } else {
     220                    foreach ($productCategory as $productCat){
     221                        $CatId = $productCat->term_id;
     222                    }
     223                    $productExport = [
    200224                        'id'  => $productId,
    201                         'pid' => $pCatId,
    202                         'n'   => $product->get_name(),
     225                        'pid' => $CatId,
     226                        'n'   => $productName,
    203227                        'g'   => false,
    204228                    ];
    205                     $productList[] = $product;
    206                 } else {
    207                     // пробуем определить родительскую категорию товара
    208                     $productParentCat = self::bpwp_export_get_parent_cat($productId, $pCatIds);
    209                     if (!$productParentCat){
    210                         // в зависимости от опции
    211                         $wrong_products_action = get_option('bpwp_wrong_products_action');
    212                         switch ($wrong_products_action) {
    213                             case 'hide':
    214                                 do_action(
    215                                     'bpwp_logger_error',
    216                                     $type = __CLASS__,
    217                                     $title = __('Экспорт товаров в Бонус+, товар пропущен', 'bonus-plus-wp'),
    218                                     $desc = sprintf(__('У товара с ID %s %s более 1 категории', 'bonus-plus-wp'), $productId, $product->name),
    219                                 );
    220                             break;
    221                             case 'empty':
    222                                 $product = [
    223                                     'id'  => $productId,
    224                                     'pid' => 0,
    225                                     'n'   => $product->get_name(),
    226                                     'g'   => false,
    227                                 ];
    228                                 $productList[] = $product;
    229                             break;
    230                         }
    231                         $wrongProducts[$productId] = $product->name;
    232                     }
     229                    $productList[] = $productExport;
    233230                }
    234231            }
     
    314311        $class = self::$lastExport['class'];
    315312        $lastExportDate = !empty(get_option(self::$lastExportOption)) ? get_option(self::$lastExportOption) : '';
    316 
    317         //$class = 'updated notice is-dismissible';
     313        $wrong_products_action = get_option('bpwp_wrong_products_action');
     314        switch ($wrong_products_action) {
     315            case 'hide':
     316                $action = __('Пропущено товаров', 'bonus-plus-wp');
     317                break;
     318            case 'empty':
     319                $action = __('Импортировано товаров, без категории', 'bonus-plus-wp');
     320                break;
     321        }
     322
    318323        $strings[] = sprintf('<strong>Результат последнего экспорта %s :</strong> %s', $lastExportDate, self::$lastExport['message']);
    319324        $strings[] = sprintf('<strong>%s: %d</strong>', esc_html(__('Найдено категорий', 'bonus-plus-wp')), self::$lastExport['cat_count']);
     
    322327        $strings[] = sprintf('<strong>%s: %d</strong>', esc_html(__('Найдено товаров', 'bonus-plus-wp')), self::$lastExport['pcount']);
    323328        $strings[] = sprintf('<strong>%s: %d</strong>', esc_html(__('Экспортировано товаров', 'bonus-plus-wp')), self::$lastExport['pexport']);
    324         $strings[] = sprintf('<strong>%s: %d</strong>', esc_html(__('Пропущено товаров', 'bonus-plus-wp')), self::$lastExport['phide']);
     329        $strings[] = sprintf('<strong>%s: %d</strong>', esc_html($action), self::$lastExport['phide']);
    325330
    326331
     
    347352
    348353    /**
    349      *  Return parent cat by product id
    350      *
     354     *  Return child category for product id https://wordpress.stackexchange.com/a/55921
     355     *  
    351356     *  @param $productId int ИД товара
    352      *  @param $pCatIds array Массид ИД категорий товара
    353      *
    354      *  @return void
    355      */
    356     public static function bpwp_export_get_parent_cat($productId, $pCatIds){
    357         if (empty($productId) || empty($pCatIds) || empty(self::$productTopCat))
    358             return false;
    359 
    360         $other_cats = array_diff($pCatIds, self::$productTopCat);
    361         $parentCats = [];
    362         foreach ($other_cats as $cat){
    363             $termChildren = get_term_children($cat, 'product_cat');
    364             if (empty($termChildren)){
    365                 $parentCats[] = $cat;
    366             }
    367         }
    368        
    369         if (count($parentCats) != 1){
    370             return false;
    371         } else {
    372             return $parentCats[0];
    373         }
    374 
    375 
     357     *
     358     *  @return array Array of child product categories
     359     */
     360    public static function bpwp_get_product_child_category($productId){
     361        //Get all terms associated with post in woocommerce's taxonomy 'product_cat'
     362        $terms = get_the_terms($productId, 'product_cat');
     363
     364        //Get an array of their IDs
     365        $term_ids = wp_list_pluck($terms, 'term_id');
     366
     367        //Get array of parents - 0 is not a parent
     368        $parents = array_filter(wp_list_pluck($terms, 'parent'));
     369
     370        //Get array of IDs of terms which are not parents.
     371        $term_ids_not_parents = array_diff($term_ids,  $parents);
     372
     373        //Get corresponding term objects.
     374        $terms_not_parents = array_intersect_key($terms,  $term_ids_not_parents);
     375
     376        return $terms_not_parents;
    376377    }
    377378}
  • bonus-plus-wp/trunk/readme.txt

    r2577633 r2578509  
    55Requires at least: 4.0
    66Tested up to: 5.8
    7 Stable tag: 1.6
     7Stable tag: 1.7
    88Requires PHP: 7.1
    99License: GPLv2 or later
     
    9393== Changelog ==
    9494
     95= 1.7 =
     96- Исправлены функция определения родительской категории у товара, добавлена документация https://github.com/evgrezanov/bonus-plus-wp/wiki/Export-products-and-product-cat/
     97
    9598= 1.6 =
    96 -Исправлены ошибки при выводе сообщения о завершении экспорта товаров и категорий
     99- Исправлены ошибки при выводе сообщения о завершении экспорта товаров и категорий
    97100
    98101= 1.5 =
Note: See TracChangeset for help on using the changeset viewer.