Changeset 2834929
- Timestamp:
- 12/16/2022 08:34:24 AM (3 years ago)
- Location:
- shinystat-analytics/trunk
- Files:
-
- 4 edited
-
README.txt (modified) (3 diffs)
-
includes/class-shinystat-analytics.php (modified) (2 diffs)
-
public/class-shinystat-analytics-public.php (modified) (4 diffs)
-
shinystat-analytics.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
shinystat-analytics/trunk/README.txt
r2816928 r2834929 5 5 Requires at least: 3.1.0 6 6 Tested up to: 6.1 7 Stable tag: 1.0.1 17 Stable tag: 1.0.12 8 8 Requires PHP: 5.6 or higher 9 9 License: GPLv2 or later … … 55 55 == Changelog == 56 56 57 = 1.0.12 = 58 * Added function shn_engage.get_product_details that calls rest api to retrieve product information from id. 59 57 60 = 1.0.11 = 58 61 * Compatibility with not defined advanced options section. … … 98 101 99 102 == Upgrade Notice == 103 104 = 1.0.12 = 105 * Added function shn_engage.get_product_details to get product information from id. 100 106 101 107 = 1.0.11 = -
shinystat-analytics/trunk/includes/class-shinystat-analytics.php
r2816928 r2834929 70 70 $this->version = SHINYSTAT_ANALYTICS_VERSION; 71 71 } else { 72 $this->version = '1.0.1 1';72 $this->version = '1.0.12'; 73 73 } 74 74 $this->plugin_name = 'shinystat-analytics'; … … 197 197 $this->loader->add_action( 'wp_head', $plugin_public, 'woocommerce_cart_content' ); 198 198 199 //add custom endpoint to retrieve product information 200 $this->loader->add_action( 'rest_api_init', $plugin_public, 'register_shinystat_rest_route' ); 201 199 202 //capture call adding product to cart and update the shn_engage structure inside the page navigation 200 203 $this->loader->add_filter( 'woocommerce_add_to_cart_fragments', $plugin_public, 'woocommerce_add_to_cart_fragments', 10, 1); -
shinystat-analytics/trunk/public/class-shinystat-analytics-public.php
r2816928 r2834929 370 370 371 371 /** 372 * Get product details by product id 373 */ 374 get_product_details: function(callback_fnc, prod_id) { 375 376 let xhr_prod = new XMLHttpRequest(); 377 xhr_prod.open('GET', "<?php echo get_rest_url( null, 'shinystat/v1/product/' ); ?>" + prod_id); 378 379 xhr_prod.onload = function() { 380 if (!!xhr_prod.responseText) { 381 382 var jsonResp = JSON.parse(xhr_prod.responseText); 383 if (!jsonResp) 384 return; 385 386 callback_fnc(jsonResp); 387 388 } 389 } 390 391 xhr_prod.send(); 392 }, 393 394 395 /** 372 396 * Add product (identified by its variant id) for input quantity 373 397 * to the cart of the current session … … 419 443 var jsonResp = JSON.parse(xhr.responseText); 420 444 if (!jsonResp) 421 return;445 return; 422 446 423 447 shn_engage.add_product(id, quantity, redirect); … … 449 473 get_cart_content: shn_engage.get_cart_content, 450 474 set_cart_content: shn_engage.set_cart_content, 475 get_product_details: shn_engage.get_product_details, 451 476 add_product: shn_engage.add_product, 452 477 update_product_quantity:shn_engage.update_product_quantity, … … 624 649 625 650 651 /** 652 * Get data about specified wc product id for the rest api response. 653 * 654 * @since 1.0.12 655 */ 656 public function get_product_details( $data ) { 657 658 if ( ! function_exists('WC') ) 659 return ['code' => 'wc_not_found', 'message' => '']; 660 661 $product = wc_get_product( $data['id'] ); 662 663 if ( ! $product ) 664 return ['code' => 'product_not_found', 'message' => 'product id does not correspond to any product']; 665 666 667 $attributes = []; 668 foreach ( $product->get_attributes() as $attribute ) { 669 if ( is_string($attribute) ) { 670 $attributes[] = $attribute; 671 } else { 672 $attribute_data = $attribute->get_data(); 673 $value = $attribute_data['value']; 674 $attributes[] = [ 675 'name' => $this->clean_field( $attribute_data['name'] ), 676 'value' => $this->clean_field( is_array($value) ? '' : $value ), 677 ]; 678 } 679 } 680 681 $categories = []; 682 $terms = get_the_terms( $product->get_id(), 'product_cat' ); 683 if ( is_array($terms) ) { 684 foreach( $terms as $term ) { 685 $categories[] = $this->clean_field( $term->name ); 686 } 687 } 688 689 return [ 690 'id' => $product->get_id(), 691 'product_title' => $this->clean_field( $product->get_name() ), 692 'product_type' => $this->clean_field( $product->get_description() ), 693 'handle' => $this->clean_field( $product->get_slug() ), 694 'url' => $this->clean_field( $product->get_permalink( $product->get_id() ) ), 695 'image' => $this->clean_field( wp_get_attachment_url( $product->get_image_id() ) ), 696 'categories' => $categories, 697 'price' => $product->price * 100, 698 'compare_at_price' => ( ($product->regular_price != '') ? $product->regular_price : 0 ) * 100, 699 'options_with_values' => $attributes, 700 ]; 701 702 } 703 704 705 /** 706 * Register rest route to get data about specified wc product. 707 * The route is /?rest_route=/shinystat/v1/product/{prod_id} 708 * 709 * @since 1.0.12 710 */ 711 public function register_shinystat_rest_route() { 712 713 register_rest_route( 'shinystat/v1', 'product/(?P<id>\d+)', [ 714 'methods' => [ 'GET' ], 715 'callback' => array($this, 'get_product_details') 716 ]); 717 718 } 719 720 626 721 } -
shinystat-analytics/trunk/shinystat-analytics.php
r2816928 r2834929 17 17 * Plugin URI: https://wordpress.org/plugins/shinystat-analytics/ 18 18 * Description: Activate the plugin and start to use ShinyStat Web Analytics and On-site Marketing Automation tools. 19 * Version: 1.0.1 119 * Version: 1.0.12 20 20 * Author: ShinyStat 21 21 * Author URI: https://www.shinystat.com … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.1 1' );38 define( 'SHINYSTAT_ANALYTICS_VERSION', '1.0.12' ); 39 39 40 40 /**
Note: See TracChangeset
for help on using the changeset viewer.