Plugin Directory

Changeset 2108304


Ignore:
Timestamp:
06/18/2019 06:13:00 PM (7 years ago)
Author:
reality66
Message:

Fix product catalog/select2 issues

Location:
cart66-cloud/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • cart66-cloud/trunk/cart66-cloud.php

    r1868741 r2108304  
    44Plugin URI: http://cart66.com
    55Description: Secure Ecommerce For WordPress.
    6 Version: 2.2.2
     6Version: 2.3.0
    77Author: Reality66
    88Author URI: http://www.reality66.com
  • cart66-cloud/trunk/includes/admin/cc-product-meta-box.php

    r1520079 r2108304  
    33add_action( 'load-post.php',     'cc_product_meta_box_setup' );
    44add_action( 'load-post-new.php', 'cc_product_meta_box_setup' );
    5 add_action( 'wp_ajax_cc_ajax_product_search', 'cc_ajax_product_search' );
    6 
    7 function cc_ajax_product_search( ) {
    8     CC_Log::write( 'AJAX product search: ' . print_r( $_REQUEST['search'], true ) );
    9     $products = CC_Cloud_Product::search( $_REQUEST['search'] );
    10     $options = array();
    11 
    12     /*
    13     Product info from search results: Array
    14     (
    15         [id] => 54d3e70dd2a57d1adc002eb1
    16         [name] => Ooma HD2 Handset
    17         [sku] => hd2
    18         [price] => 60.0
    19         [on_sale] =>
    20         [sale_price] =>
    21         [currency] => $
    22         [expires_after] =>
    23         [formatted_price] => $60.00
    24         [formatted_sale_price] => $
    25         [digital] =>
    26         [type] => product
    27         [status] => available
    28     )
    29      */
    30     foreach ( $products as $p ) {
    31         CC_Log::write( 'Product info from search results: ' . print_r( $p, true ) );
    32         $options[] = array(
    33             'id' => json_encode( $p ),
    34             'text' => $p['name']
    35         );
    36     }
    37 
    38     echo json_encode( $options );
    39     die();
    40 }
    415
    426function cc_product_meta_box_setup() {
     
    459   
    4610    $url = cc_url();
    47     wp_enqueue_style( 'select2', $url .'resources/css/select2.css' );
    48     wp_enqueue_script( 'select2', $url . 'resources/js/select2.min.js' );
    49 
     11    //wp_enqueue_style( 'choices-base', 'https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/base.min.css');
     12    wp_enqueue_style( 'choices', 'https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css');
     13    wp_enqueue_script( 'choices', 'https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js');
    5014}
    5115
     
    6832function cc_product_meta_box_render( $post, $box ) {
    6933
    70     $product_name = get_post_meta( $post->ID, '_cc_product_name', true );
     34    $selected_product_sku = get_post_meta( $post->ID, '_cc_product_sku', true );
    7135    $layout = get_post_meta( $post->ID, '_cc_product_layout', true );
    72 
    73     if ( empty( $value ) ) {
    74         $value = 'Select Product';
    75     }
     36    $cloud_product = new CC_Cloud_Product();
     37    $cc_products = $cloud_product->get_products();
    7638
    7739    $data = array(
    7840        'post' => $post,
    7941        'box' => $box,
    80         'value' => $product_name,
    81         'layout' => $layout
     42        'selected_product_sku' => $selected_product_sku,
     43        'layout' => $layout,
     44        'cc_products' => $cc_products
    8245    );
    8346
     
    12689    $prefix   = '_cc_product_';
    12790
    128     // Get the posted data and sanitize it for use as an HTML class.
    129     $product_info = ( isset( $_POST[ '_cc_product_json' ] ) ? sanitize_text_field( $_POST[ '_cc_product_json' ] ) : '' );
    130     $product_info = stripslashes( $product_info );
    131     $product_info = json_decode( $product_info, true );
     91    if ( isset( $_POST[ '_cc_product_sku' ] ) && strlen( $_POST[ '_cc_product_sku' ] ) > 0 ) {
     92        $cc_product = CC_Cloud_Product::find_by_sku($_POST[ '_cc_product_sku' ]);
     93        CC_Log::write( print_r($cc_product, true) );
     94    }
    13295
    133     if ( is_array( $product_info ) ) {
     96    if ( is_array( $cc_product ) ) {
    13497
    13598        // Get the meta value of the custom field key.
     
    138101        if ( '' == $old_value ) {
    139102            // If a new meta value was added and there was no previous value, add it.
    140             add_post_meta( $post_id, $json_key, $product_info, true );
    141             foreach( $product_info as $key => $value ) {
     103            add_post_meta( $post_id, $json_key, $cc_product, true );
     104            foreach( $cc_product as $key => $value ) {
    142105                add_post_meta( $post_id, $prefix . $key, $value, true );
    143106            }
    144         } elseif ( $product_info != $old_value ) {
     107        } elseif ( $cc_product != $old_value ) {
    145108            // If the new meta value does not match the old value, update it.
    146             update_post_meta( $post_id, $json_key, $product_info );
    147             foreach( $product_info as $key => $value ) {
     109            update_post_meta( $post_id, $json_key, $cc_product );
     110            foreach( $cc_product as $key => $value ) {
    148111                update_post_meta( $post_id, $prefix . $key, $value );
    149112            }
    150         } elseif ( '' == $product_info && $old_value ) {
     113        } elseif ( '' == $cc_product && $old_value ) {
    151114            // TODO: $product_info will never be empty here because in order to get here it has to be an array
    152115            // If there is no new meta value but an old value exists, delete it.
  • cart66-cloud/trunk/includes/class-cc-product.php

    r1868741 r2108304  
    115115            $post = array_shift( $posts );
    116116            if ( is_object( $post ) && $post->ID > 0 ) {
    117                 $results = CC_Cloud_Product::search( $sku );
     117                $cc_product = CC_Cloud_Product::find_by_sku( $sku );
    118118                // CC_Log::write( 'Updating product info for post id: ' . $post->ID . " :: " . print_r( $results, true ) );
    119                 if( is_array( $results ) && count( $results ) ) {
    120                     $product_info = array_shift( $results );
    121                     update_post_meta( $post->ID, $this->json_key, $product_info );
    122                     foreach( $product_info as $key => $value ) {
     119                if( is_array( $cc_product ) ) {
     120                    update_post_meta( $post->ID, $this->json_key, $cc_product );
     121                    foreach( $cc_product as $key => $value ) {
    123122                        update_post_meta( $post->ID, $this->prefix . $key, $value );
    124123                    }
  • cart66-cloud/trunk/includes/cloud/class-cc-cloud-product.php

    r1520079 r2108304  
    141141    }
    142142
     143    public static function find_by_sku( $sku ) {
     144        self::init();
     145
     146        CC_Log::write( "CC_Cloud_Product query param: " . print_r( $sku, true ) );
     147        $url = self::$cloud->api . 'products/sku/' . urlencode( $sku );
     148
     149        CC_Log::write( "Search the cloud for products with this URL: $url" );
     150
     151        $headers = self::$cloud->basic_auth_header( array( 'Accept' => 'application/json' ) );
     152
     153        if ( $headers ) {
     154            $response = wp_remote_get( $url, $headers );
     155
     156            if( self::$cloud->response_ok( $response ) ) {
     157                $product = json_decode( $response['body'], true );
     158            } else {
     159                CC_Log::write( "Product search failed: $url :: " . print_r( $response, true ) );
     160                throw new CC_Exception_API( "Failed to retrieve products from Cart66 Cloud" );
     161            }
     162        }
     163
     164        return $product;
     165    }
     166
     167
    143168}
  • cart66-cloud/trunk/readme.txt

    r1868741 r2108304  
    55Requires at least: 4.0
    66Requires PHP: 5.4
    7 Tested up to: 4.9
    8 Stable tag: 2.2.2
     7Tested up to: 5.2.1
     8Stable tag: 2.3.0
    99License: GPLv3
    1010License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    9191= Minimum Requirements =
    9292
    93 * WordPress 3.2 or greater
     93* WordPress 4.0 or greater
    9494* PHP version 5.4 or greater
    9595* MySQL version 5.0 or greater
     
    138138
    139139== Changelog ==
     140
     141= 2.3.0 =
     142- Fix: Issue with product catalog showing incorrect prices for products
     143- Fix: Metabox for selecting the product for a Product post type didn't always
     144  save correctly
     145- Update: Other minor stability improvements and library updates
    140146
    141147= 2.2.2 =
  • cart66-cloud/trunk/views/admin/html-product-meta-box.php

    r1520079 r2108304  
    99?>
    1010
    11 <script langage="text/javascript">
    12     jQuery(document).ready(function($) {
     11<style type="text/css">
     12    .choices .choices__inner {
     13        min-height: auto;
     14        padding-right: 0;
     15        width: 90%;
     16    }
    1317
    14         $('#_cc_product_json').select2({
    15             width: '100%',
    16             minimumInputLength: 2,
    17             allowClear: true,
    18             ajax: {
    19                 url: ajaxurl,
    20                 dataType: 'json',
    21                 data: function (term, page) {
    22                     return {
    23                         action: 'cc_ajax_product_search',
    24                         search: term
    25                     };
    26                 },
    27                 results: function (data, page) {
    28                   return { results: data };
    29                 }
    30             }
    31         });
     18    .choices .choices__list--single {
     19        width: 90%;
     20    }
    3221
     22    .choices .choices__item--selectable {
     23        width: 90%;
     24    }
    3325
    34     });
    35 </script>
     26    .choices[data-type*=select-one]:after {
     27        right: 35px;
     28    }
     29
     30    .choices .choices__list--dropdown {
     31        width: 93%;
     32    }
     33</style>
    3634
    3735<div class="cc_meta_box_option">
    38     <input type="hidden" name="_cc_product_json" id="_cc_product_json" data-placeholder="<?php echo $value; ?>" />
     36    <select name="_cc_product_sku" id="_cc_product_sku" class="choices">
     37        <option placeholder value="">Choose Product</option>
     38        <?php foreach ($cc_products as $cc_product): ?>
     39            <?php if ($cc_product["sku"] == $selected_product_sku): ?>
     40                <option value="<?php echo $cc_product["sku"] ?>" selected>
     41                    <?php echo $cc_product["name"] ?>
     42                </option>
     43            <?php else: ?>
     44                <option value="<?php echo $cc_product["sku"] ?>">
     45                    <?php echo $cc_product["name"] ?>
     46                </option>
     47            <?php endif ?>
     48        <?php endforeach ?>
     49    </select>
    3950</div>
    4051
     
    4960    </select>
    5061</div>
     62
     63<script>
     64    $(document).ready(function() {
     65        var choices = new Choices($(".choices")[0], {
     66        });
     67    });
     68</script>
Note: See TracChangeset for help on using the changeset viewer.