Changeset 2595933
- Timestamp:
- 09/09/2021 12:19:47 AM (4 years ago)
- Location:
- mag-products-integration/trunk
- Files:
-
- 8 edited
-
includes/class-admin.php (modified) (2 diffs)
-
includes/class-parameters-bag.php (modified) (2 diffs)
-
includes/class-plugin.php (modified) (4 diffs)
-
includes/class-product.php (modified) (1 diff)
-
includes/stores/class-store-interface.php (modified) (1 diff)
-
includes/stores/class-store-magento2.php (modified) (4 diffs)
-
readme.txt (modified) (2 diffs)
-
wp-storefront.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mag-products-integration/trunk/includes/class-admin.php
r2595518 r2595933 173 173 <tbody> 174 174 <tr valign="top"> 175 <td><code>product </code></td>175 <td><code>products</code></td> 176 176 <td><strong><?php _e( 'Yes', 'wp-storefront' ) ?></strong></td> 177 <td><?php _e( 'The product SKU .', 'wp-storefront' ) ?></td>177 <td><?php _e( 'The product SKU or SKUs comma separated for multiple products. The list is sorted by name ascendent.', 'wp-storefront' ) ?></td> 178 178 <td></td> 179 179 </tr> … … 242 242 <td><?php _e( 'No', 'wp-storefront' ) ?></td> 243 243 <td><?php _e( 'Show product short description.', 'wp-storefront' ) ?></td> 244 <td>true</td> 245 </tr> 246 <tr valign="top"> 247 <td><code>url_html_suffix</code></td> 248 <td><?php _e( 'No', 'wp-storefront' ) ?></td> 249 <td><?php _e( 'Add the ".html" suffix to all product URLs.', 'wp-storefront' ) ?></td> 244 250 <td>true</td> 245 251 </tr> -
mag-products-integration/trunk/includes/class-parameters-bag.php
r2595518 r2595933 77 77 'class' => '', 78 78 'product' => '', 79 'products' => '', 79 80 'store' => null, 80 81 'platform' => '', … … 82 83 'prefix' => '', 83 84 'suffix' => '$', 85 'url_html_suffix' => true, 84 86 'image_width' => '', 85 87 'image_height' => '', -
mag-products-integration/trunk/includes/class-plugin.php
r2595518 r2595933 162 162 163 163 /** 164 * The plugin is ready when a valid API endpoint is available.165 *166 * @return string Valid Magento REST API endpoint or empty string.167 * @since 1.0.0168 *169 */170 public function is_ready() {171 $is_ready = $this->get_store_manager()->get_store()->ready();172 173 return $is_ready;174 }175 176 /**177 164 * Function executed on plugin activation. 178 165 * Update plugin's options to set default values. … … 246 233 $shortcode_cache_id = sha1( serialize( $atts ) ); 247 234 248 $products = array();249 $parameters_bag = Parameters_Bag::createFromShortcode( $atts );235 $products = array(); 236 $parameters_bag = Parameters_Bag::createFromShortcode( $atts ); 250 237 if ( Plugin::get_instance()->get_cache()->is_enabled() ) { 251 238 if ( ! Plugin::get_instance()->get_cache()->is_expired( $shortcode_cache_id ) ) { … … 254 241 } 255 242 256 $platform = $parameters_bag->get( 'platform' );257 $product = $parameters_bag->get( 'product' );258 if ( empty( $platform ) || empty( $product ) ) {259 return '';260 }261 262 243 if ( empty( $products ) ) { 263 $product = $this->store_manager->get_store( $platform )->get_product( $parameters_bag ); 264 if ( $product instanceof Product ) { 265 $products = array( $product ); 266 } elseif ( $product instanceof \WP_Error ) { 267 return '<p class="wp-storefront-error">' . $product->get_error_message() . '</p>'; 268 } 244 $platform = $parameters_bag->get( 'platform' ); 245 $product_ids = $parameters_bag->get( 'products', $parameters_bag->get( 'product' ) ); 246 if ( empty( $platform ) || empty( $product_ids ) ) { 247 return ''; 248 } 249 250 $products = $this->store_manager->get_store( $platform )->get_products( $parameters_bag ); 251 if ( $products instanceof \WP_Error ) { 252 return '<p class="wp-storefront-error">' . $products->get_error_message() . '</p>'; 253 } 254 Plugin::get_instance()->get_cache()->set_cached_products( $products, $shortcode_cache_id ); 269 255 } 270 256 … … 293 279 'image_height' => $parameters_bag->get( 'image_width', 0, 'int' ), 294 280 'title' => $parameters_bag->get( 'title', 'h2', 'string' ), 295 'show_description' => $parameters_bag->get( ' description', true, 'boolean' ),281 'show_description' => $parameters_bag->get( 'show_description', true, 'boolean' ), 296 282 'description_length' => $parameters_bag->get( 'description_length', 0, 'int' ), 297 283 'prefix' => $parameters_bag->get( 'prefix', '', 'string' ), -
mag-products-integration/trunk/includes/class-product.php
r2595518 r2595933 127 127 ob_start(); ?> 128 128 <img 129 src="<?php esc_attr_e( $this->get_image_url() ) ?>"130 alt="<?php esc_attr_e( $this->get_name() ) ?>"131 <?php echo $width ? 'width="' . esc_attr( $width ) . '"' : '' ?>132 <?php echo $height ? 'width="' . esc_attr( $height ) . '"' : '' ?>129 src="<?php esc_attr_e( $this->get_image_url() ) ?>" 130 alt="<?php esc_attr_e( $this->get_name() ) ?>" 131 <?php echo $width ? 'width="' . esc_attr( $width ) . '"' : '' ?> 132 <?php echo $height ? 'width="' . esc_attr( $height ) . '"' : '' ?> 133 133 /> 134 134 <?php -
mag-products-integration/trunk/includes/stores/class-store-interface.php
r2595518 r2595933 54 54 55 55 /** 56 * Fetch a single product from e-commerce platform.56 * Fetch multiple products by their SKUs or IDs. 57 57 * 58 * @param Parameters_Bag $parameters_bag Parsed parameters from the [wp sf_product] shortcode.58 * @param Parameters_Bag $parameters_bag Parsed parameters from the [wp-storefront] shortcode. 59 59 * 60 * @return Product 61 * @since 2.0. 060 * @return Product[] Array of products. 61 * @since 2.0.1 62 62 */ 63 public function get_product ( Parameters_Bag $parameters_bag );63 public function get_products( Parameters_Bag $parameters_bag ); 64 64 } -
mag-products-integration/trunk/includes/stores/class-store-magento2.php
r2595518 r2595933 31 31 <td> 32 32 <input type="text" class="regular-text" name="wp_storefront_m2_base_url" 33 value="<?php esc_attr_e( get_option( 'wp_storefront_m2_base_url' ) ); ?>"/>33 value="<?php esc_attr_e( get_option( 'wp_storefront_m2_base_url' ) ); ?>"/> 34 34 </td> 35 35 </tr> … … 38 38 <td> 39 39 <input type="password" class="regular-text" name="wp_storefront_m2_access_token" 40 value="<?php esc_attr_e( get_option( 'wp_storefront_m2_access_token' ) ); ?>"/>40 value="<?php esc_attr_e( get_option( 'wp_storefront_m2_access_token' ) ); ?>"/> 41 41 </td> 42 42 </tr> … … 47 47 * @inheritdoc 48 48 */ 49 public function get_product( Parameters_Bag $parameters_bag ) { 50 $product = null; 51 52 $path = '/rest/all/V1/products/' . $parameters_bag->get( 'product' ); 49 public function get_products( Parameters_Bag $parameters_bag ) { 50 $products = array(); 51 $skus = array_map( function ( $sku ) { 52 return trim( $sku ); 53 }, explode( ',', $parameters_bag->get( 'products', $parameters_bag->get( 'product' ) ) ) ); 54 55 $path = '/rest/all/V1/products'; 53 56 if ( $parameters_bag->has( 'store' ) ) { 54 $path = '/rest/' . $parameters_bag->get( 'store' ) . '/V1/products /' . $parameters_bag->get( 'product' );57 $path = '/rest/' . $parameters_bag->get( 'store' ) . '/V1/products'; 55 58 } 56 59 $url = Url_Utils::build_url( 57 get_option( 'wp_storefront_m2_base_url' ), 58 $path 60 get_option( 'wp_storefront_m2_base_url' ), 61 $path, 62 array( 63 'searchCriteria[pageSize]' => count( $skus ), 64 'searchCriteria[filterGroups][0][filters][0][value]' => implode( ',', $skus ), 65 'searchCriteria[filterGroups][0][filters][0][condition_type]' => 'in', 66 'searchCriteria[filterGroups][0][filters][0][field]' => 'sku', 67 'searchCriteria[sortOrders][0][field]' => 'name', 68 'searchCriteria[sortOrders][0][direction]' => 'asc' 69 ) 59 70 ); 60 71 61 72 if ( $url ) { 62 73 $response = wp_remote_get( $url, [ 63 'headers' => [64 'Authorization' => 'Bearer ' . get_option( 'wp_storefront_m2_access_token' ),65 ],74 'headers' => array( 75 'Authorization' => 'Bearer ' . get_option( 'wp_storefront_m2_access_token' ), 76 ), 66 77 ] ); 67 78 68 79 if ( is_array( $response ) && ! empty( $response['body'] ) && $response['response']['code'] == 200 ) { 69 $data = json_decode( $response['body'], true ); 70 $product = new Product( [ 71 'name' => $data['name'], 72 'sku' => $data['sku'], 73 'image_url' => $this->get_image_url( get_option( 'wp_storefront_m2_base_url' ), $data ), 74 'url' => $this->get_url( get_option( 'wp_storefront_m2_base_url' ), $data ), 75 'short_description' => $this->get_custom_attribute( Array_Utils::value( $data, 'custom_attributes', [], 'array' ), 'short_description', '' ), 76 'price' => Array_Utils::value( $data, 'price' ), 77 'special_price' => $this->get_special_price( $data ), 78 'raw_data' => $data 79 ] ); 80 } 81 } 82 83 return $product; 80 $data = json_decode( $response['body'], true ); 81 if ( isset( $data['items'] ) ) { 82 foreach ( $data['items'] as $item ) { 83 $products[] = new Product( [ 84 'name' => $item['name'], 85 'sku' => $item['sku'], 86 'image_url' => $this->get_image_url( get_option( 'wp_storefront_m2_base_url' ), $item ), 87 'url' => $this->get_url( get_option( 'wp_storefront_m2_base_url' ), $item, $parameters_bag ), 88 'short_description' => $this->get_custom_attribute( Array_Utils::value( $item, 'custom_attributes', [], 'array' ), 'short_description', '' ), 89 'price' => Array_Utils::value( $item, 'price' ), 90 'special_price' => $this->get_special_price( $item ), 91 'raw_data' => $item 92 ] ); 93 } 94 } 95 } 96 } 97 98 return $products; 84 99 } 85 100 … … 191 206 * @param string $base_url The base URL of the Magento 2 store. 192 207 * @param array $product The REST API response data. 208 * @param Parameters_Bag $parameters_bag The shortcode attributes. 193 209 * 194 210 * @return string The product URL without categories. 195 211 * @since 2.0.0 196 212 */ 197 protected function get_url( $base_url, array $product ) {213 protected function get_url( $base_url, array $product, Parameters_Bag $parameters_bag ) { 198 214 $url_key = $this->get_custom_attribute( Array_Utils::value( $product, 'custom_attributes', [], 'array' ), 'url_key', false ); 199 215 $url = ''; 200 216 201 217 if ( $url_key ) { 202 $url = $base_url . '/' . $url_key . '.html';203 } 204 205 return $url;218 $url = $base_url . '/' . $url_key . ($parameters_bag->get( 'url_html_suffix', true, 'boolean' ) ? '.html' : ''); 219 } 220 221 return apply_filters( 'wp_storefront_product_url', $url ); 206 222 } 207 223 -
mag-products-integration/trunk/readme.txt
r2595750 r2595933 4 4 Requires at least: 4.6 5 5 Tested up to: 5.8 6 Stable tag: 2.0. 06 Stable tag: 2.0.1 7 7 License: GPLv2 or later 8 8 9 Drive more visitors to your online store with WP Storefront. A product showcase for Magento 2 that let you show your products into your contents.9 Drive more visitors to your online store with WP Storefront. A product showcase for Magento 2 that let you show your products into your articles and page. 10 10 11 11 == Description == … … 46 46 47 47 == Changelog == 48 49 = 2.0.1 = 50 * NEW You can now show multiple products by separating multiple SKUs by commas 51 * FIX New shortcode attribute to remove the ".html" suffix to the URL: url_html_suffix="false" 52 * FIX You can now hide the description with the show_description attribute 53 * FIX Cached products not working 48 54 49 55 = 2.0.0 = -
mag-products-integration/trunk/wp-storefront.php
r2595518 r2595933 12 12 Plugin URI: https://wordpress.org/plugins/mag-products-integration/ 13 13 Description: Display products from your favourite Ecommerce platform directly into your articles and pages. 14 Version: 2.0. 014 Version: 2.0.1 15 15 Requires at least: 4.6 16 16 Author: Francis Santerre … … 20 20 */ 21 21 22 define( 'WP_STOREFRONT_PLUGIN_VERSION', '2.0. 0' );22 define( 'WP_STOREFRONT_PLUGIN_VERSION', '2.0.1' ); 23 23 define( 'WP_STOREFRONT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); 24 24
Note: See TracChangeset
for help on using the changeset viewer.