Plugin Directory

Changeset 2578312


Ignore:
Timestamp:
08/04/2021 06:38:40 PM (4 years ago)
Author:
appmaxplataforma
Message:

Validando e realizando envio de valores personalizados pro produto.

Location:
appmax-woocommerce
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • appmax-woocommerce/trunk/appmax-woocommerce.php

    r2552677 r2578312  
    33 * Plugin Name: AppMax WooCommerce
    44 * Description: Gateway de pagamento AppMax para WooCommerce.
    5  * Version: 2.0.40
     5 * Version: 2.0.41
    66 * License: GPLv2 or later
    77 * Author: AppMax Plataforma de Vendas Ltda
     
    2424    class AppMax_WC
    2525    {
    26         const VERSION = '2.0.40';
     26        const VERSION = '2.0.41';
    2727
    2828        /**
     
    179179            include_once AWC_ABSPATH . '/includes/class-awc-gateway-credit-card.php';
    180180            include_once AWC_ABSPATH . '/includes/class-awc-helper.php';
     181            include_once AWC_ABSPATH . '/includes/class-awc-interest.php';
    181182            include_once AWC_ABSPATH . '/includes/class-awc-post-payment.php';
    182183            include_once AWC_ABSPATH . '/includes/class-awc-process-payment.php';
    183184            include_once AWC_ABSPATH . '/includes/class-awc-search-gateway.php';
     185            include_once AWC_ABSPATH . '/includes/class-awc-tax.php';
     186            include_once AWC_ABSPATH . '/includes/class-awc-tracking-code.php';
    184187            include_once AWC_ABSPATH . '/includes/class-awc-validation.php';
    185188            include_once AWC_ABSPATH . '/includes/class-awc-webhook.php';
    186189            include_once AWC_ABSPATH . '/includes/class-awc-webhook-post.php';
    187             include_once AWC_ABSPATH . '/includes/class-awc-tracking-code.php';
    188190        }
    189191
  • appmax-woocommerce/trunk/includes/class-awc-api.php

    r2552677 r2578312  
    115115     * @param $interest_total
    116116     * @return array|WP_Error
     117     * @throws Exception
    117118     */
    118119    public function awc_post_order( $customer_id, $interest_total )
     
    207208        return array(
    208209            'Content-Type' => 'application/json; charset=utf-8',
    209             'access-token' => $this->gateway->awc_api_key
     210            'access-token' => $this->gateway->awc_api_key,
    210211        );
    211212    }
     
    249250        $array_products = [];
    250251
    251         foreach ($items as $item => $values) {
     252        foreach ($items as $values) {
    252253            array_push( $array_products, $this->awc_get_current_product( $values ) );
    253254        }
    254255
    255         $array_products = $this->awc_distribute_interest( $array_products, $interest );
     256        $array_products = (new AWC_Interest())->awc_distribute_interest( $array_products, $interest );
    256257
    257258        $tax_total = (float) AWC_Helper::awc_get_fee_total();
     
    261262        }
    262263
    263         return $this->awc_distribute_tax( $array_products, $interest );
    264     }
    265 
    266     private function awc_distribute_interest( $products, $interest )
    267     {
    268         $distribute_interest = (float) round(($interest / count($products)), 3);
    269 
    270         return array_map(function($item) use ($distribute_interest) {
    271             $price = $item['price'] + ($distribute_interest / $item['qty']);
    272             $item['price'] = round($price, 3);
    273             return $item;
    274         }, $products);
    275     }
    276 
    277     private function awc_distribute_tax( $products, $interest )
    278     {
    279         $tax_total = (float) AWC_Helper::awc_get_fee_total();
    280 
    281         $distribute_tax = round(($tax_total / count($products)), 3);
    282 
    283         $new_products = array_map(function($item) use ($distribute_tax) {
    284             $price = $item['price'] + ($distribute_tax / $item['qty']);
    285             $item['price'] = round($price, 3);
    286             return $item;
    287         }, $products);
    288 
    289         $total_sum_products = $this->awc_get_total_sum_products( $new_products );
    290 
    291         $discount = (float) number_format( AWC_Helper::awc_get_discount_total(), 2, '.', ',' );
    292 
    293         $total_cart = (float) AWC_Helper::awc_get_total_cart();
    294 
    295         $difference = (float) round((($total_sum_products - $discount - $interest) - $total_cart), 2);
    296 
    297         if ($difference == 0.00) {
    298             return $new_products;
    299         }
    300 
    301         uasort($new_products, function($a, $b) {
    302             if ($a['price'] == $b['price']) {
    303                 return 0;
    304             }
    305             return ($a['price'] < $b['price']) ? 1 : -1;
    306         });
    307 
    308         $new_products = array_values($new_products);
    309 
    310         foreach ($new_products as $key => $product) {
    311             if ($product['qty'] == 1) {
    312                 $new_products[$key]['price'] -= $difference;
    313                 break;
    314             }
    315             if ($product['qty'] > 1) {
    316                 $new_products[$key]['price'] -= $difference / $product['qty'];
    317                 break;
    318             }
    319         }
    320 
    321         return $new_products;
    322     }
    323 
    324     private function awc_get_total_sum_products( $products )
    325     {
    326         return array_reduce($products, function($total, $item) {
    327             $total += (float) ($item['price'] * $item['qty']);
    328             return $total;
    329         });
     264        return (new AWC_Tax())->awc_distribute_tax( $array_products, $interest );
     265    }
     266
     267    /**
     268     * @param $values
     269     * @return array
     270     * @throws Exception
     271     */
     272    private function awc_get_current_product( $values )
     273    {
     274        $product = wc_get_product( $values['product_id'] );
     275
     276        if ( ! $product->get_sku() ) {
     277            throw new \Exception( "Produto do carrinho {$product->get_title()} não possui SKU registrado." );
     278        }
     279
     280        if ( empty( $values['variation_id'] ) && count( $values['variation'] ) == 0 ) {
     281            return $this->awc_get_information_product( $product, $values );
     282        }
     283
     284        return $this->awc_get_information_product_variation( $product->get_sku(), $values );
     285    }
     286
     287    /**
     288     * @param WC_Product $product
     289     * @param $values
     290     * @return array
     291     */
     292    private function awc_get_information_product( WC_Product $product, $values )
     293    {
     294        $price = (float) $product->get_price();
     295
     296        if ( ! empty( $values['rn_entry'] ) ) {
     297            $totals = $values['rn_entry']->Totals;
     298
     299            $price = (float) $product->get_price() + $totals->OptionsTotal;
     300        }
     301
     302        return [
     303            "sku" => $product->get_sku(),
     304            "price" => $price,
     305            "name" => $product->get_title(),
     306            "qty" => $values['quantity'],
     307            "description" => $product->get_description(),
     308            "image" => get_the_post_thumbnail_url( $values['product_id'] ),
     309        ];
    330310    }
    331311
     
    339319        $variation = wc_get_product( $values['variation_id'] );
    340320
     321        $price = (float) $variation->get_price();
     322
     323        if ( ! empty( $values['rn_entry'] ) ) {
     324            $totals = $values['rn_entry']->Totals;
     325
     326            $price = (float) $variation->get_price() + $totals->OptionsTotal;
     327        }
     328
    341329        return [
    342330            "sku" => "{$skuParentProduct}__{$variation->get_sku()}",
    343             "price" => (float) $variation->get_price(),
     331            "price" => $price,
    344332            "name" => $variation->get_name(),
    345333            "qty" => $values['quantity'],
     
    350338
    351339    /**
    352      * @param WC_Product $product
    353      * @param $values
    354      * @return array
    355      */
    356     private function awc_get_information_product( WC_Product $product, $values )
    357     {
    358         return [
    359             "sku" => $product->get_sku(),
    360             "price" => (float) $product->get_price(),
    361             "name" => $product->get_title(),
    362             "qty" => $values['quantity'],
    363             "description" => $product->get_description(),
    364             "image" => get_the_post_thumbnail_url( $values['product_id'] ),
    365         ];
    366     }
    367 
    368     /**
    369      * @param $values
    370      * @return array
    371      * @throws Exception
    372      */
    373     private function awc_get_current_product( $values )
    374     {
    375         $product = wc_get_product( $values['product_id'] );
    376 
    377         if ( ! $product->get_sku() ) {
    378             throw new \Exception( "Produto do carrinho {$product->get_title()} não possui SKU registrado." );
    379         }
    380 
    381         if ( empty( $values['variation_id'] ) && count( $values['variation'] ) == 0 ) {
    382             return $this->awc_get_information_product( $product, $values );
    383         }
    384 
    385         return $this->awc_get_information_product_variation( $product->get_sku(), $values );
    386 
    387     }
    388 
    389     /**
    390340     * @param $order_id
    391341     * @param $tracking_code
     
    398348        return $this->awc_post( $this->awc_get_full_url( AWC_Suffix_Api::AWS_SUFFIX_TRACKING_CODE ), $data );
    399349    }
    400 
    401350
    402351    /**
  • appmax-woocommerce/trunk/readme.txt

    r2552677 r2578312  
    44Requires at least: 4.0
    55Tested up to: 5.1
    6 Stable tag: 2.0.40
     6Stable tag: 2.0.41
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    117117== Changelog ==
    118118
     119= 2.0.41 =
     120
     121* Permitido envio de valores personalizados ao produto através da extensão do Woocommerce Extra Product Options Builder for WooCommerce.
     122
    119123= 2.0.40 =
    120124
Note: See TracChangeset for help on using the changeset viewer.