abstracts
3 years ago
admin
3 years ago
blocks
5 years ago
cli
4 years ago
customizer
4 years ago
data-stores
4 years ago
emails
4 years ago
export
4 years ago
gateways
3 years ago
import
4 years ago
integrations
4 years ago
interfaces
4 years ago
legacy
4 years ago
libraries
6 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
3 years ago
rest-api
4 years ago
shipping
4 years ago
shortcodes
4 years ago
theme-support
4 years ago
tracks
4 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
4 years ago
class-wc-ajax.php
4 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
4 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
4 years ago
class-wc-cart-totals.php
4 years ago
class-wc-cart.php
4 years ago
class-wc-checkout.php
4 years ago
class-wc-cli.php
4 years ago
class-wc-comments.php
4 years ago
class-wc-countries.php
4 years ago
class-wc-coupon.php
4 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
4 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
4 years ago
class-wc-discounts.php
4 years ago
class-wc-download-handler.php
4 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
4 years ago
class-wc-frontend-scripts.php
4 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
4 years ago
class-wc-https.php
4 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
4 years ago
class-wc-order.php
4 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
4 years ago
class-wc-post-types.php
4 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
4 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
4 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
4 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
4 years ago
class-wc-shipping-rate.php
4 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
4 years ago
class-wc-template-loader.php
4 years ago
class-wc-tracker.php
4 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
4 years ago
wc-cart-functions.php
4 years ago
wc-conditional-functions.php
4 years ago
wc-core-functions.php
4 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
4 years ago
wc-formatting-functions.php
4 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
4 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
4 years ago
wc-rest-functions.php
4 years ago
wc-stock-functions.php
4 years ago
wc-template-functions.php
4 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
4 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
4 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-shortcodes.php
700 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shortcodes |
| 4 | * |
| 5 | * @package WooCommerce\Classes |
| 6 | * @version 3.2.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * WooCommerce Shortcodes class. |
| 13 | */ |
| 14 | class WC_Shortcodes { |
| 15 | |
| 16 | /** |
| 17 | * Init shortcodes. |
| 18 | */ |
| 19 | public static function init() { |
| 20 | $shortcodes = array( |
| 21 | 'product' => __CLASS__ . '::product', |
| 22 | 'product_page' => __CLASS__ . '::product_page', |
| 23 | 'product_category' => __CLASS__ . '::product_category', |
| 24 | 'product_categories' => __CLASS__ . '::product_categories', |
| 25 | 'add_to_cart' => __CLASS__ . '::product_add_to_cart', |
| 26 | 'add_to_cart_url' => __CLASS__ . '::product_add_to_cart_url', |
| 27 | 'products' => __CLASS__ . '::products', |
| 28 | 'recent_products' => __CLASS__ . '::recent_products', |
| 29 | 'sale_products' => __CLASS__ . '::sale_products', |
| 30 | 'best_selling_products' => __CLASS__ . '::best_selling_products', |
| 31 | 'top_rated_products' => __CLASS__ . '::top_rated_products', |
| 32 | 'featured_products' => __CLASS__ . '::featured_products', |
| 33 | 'product_attribute' => __CLASS__ . '::product_attribute', |
| 34 | 'related_products' => __CLASS__ . '::related_products', |
| 35 | 'shop_messages' => __CLASS__ . '::shop_messages', |
| 36 | 'woocommerce_order_tracking' => __CLASS__ . '::order_tracking', |
| 37 | 'woocommerce_cart' => __CLASS__ . '::cart', |
| 38 | 'woocommerce_checkout' => __CLASS__ . '::checkout', |
| 39 | 'woocommerce_my_account' => __CLASS__ . '::my_account', |
| 40 | ); |
| 41 | |
| 42 | foreach ( $shortcodes as $shortcode => $function ) { |
| 43 | add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function ); |
| 44 | } |
| 45 | |
| 46 | // Alias for pre 2.1 compatibility. |
| 47 | add_shortcode( 'woocommerce_messages', __CLASS__ . '::shop_messages' ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Shortcode Wrapper. |
| 52 | * |
| 53 | * @param string[] $function Callback function. |
| 54 | * @param array $atts Attributes. Default to empty array. |
| 55 | * @param array $wrapper Customer wrapper data. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function shortcode_wrapper( |
| 60 | $function, |
| 61 | $atts = array(), |
| 62 | $wrapper = array( |
| 63 | 'class' => 'woocommerce', |
| 64 | 'before' => null, |
| 65 | 'after' => null, |
| 66 | ) |
| 67 | ) { |
| 68 | ob_start(); |
| 69 | |
| 70 | // @codingStandardsIgnoreStart |
| 71 | echo empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '">' : $wrapper['before']; |
| 72 | call_user_func( $function, $atts ); |
| 73 | echo empty( $wrapper['after'] ) ? '</div>' : $wrapper['after']; |
| 74 | // @codingStandardsIgnoreEnd |
| 75 | |
| 76 | return ob_get_clean(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Cart page shortcode. |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public static function cart() { |
| 85 | return is_null( WC()->cart ) ? '' : self::shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ) ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Checkout page shortcode. |
| 90 | * |
| 91 | * @param array $atts Attributes. |
| 92 | * @return string |
| 93 | */ |
| 94 | public static function checkout( $atts ) { |
| 95 | return self::shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Order tracking page shortcode. |
| 100 | * |
| 101 | * @param array $atts Attributes. |
| 102 | * @return string |
| 103 | */ |
| 104 | public static function order_tracking( $atts ) { |
| 105 | return self::shortcode_wrapper( array( 'WC_Shortcode_Order_Tracking', 'output' ), $atts ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * My account page shortcode. |
| 110 | * |
| 111 | * @param array $atts Attributes. |
| 112 | * @return string |
| 113 | */ |
| 114 | public static function my_account( $atts ) { |
| 115 | return self::shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * List products in a category shortcode. |
| 120 | * |
| 121 | * @param array $atts Attributes. |
| 122 | * @return string |
| 123 | */ |
| 124 | public static function product_category( $atts ) { |
| 125 | if ( empty( $atts['category'] ) ) { |
| 126 | return ''; |
| 127 | } |
| 128 | |
| 129 | $atts = array_merge( |
| 130 | array( |
| 131 | 'limit' => '12', |
| 132 | 'columns' => '4', |
| 133 | 'orderby' => 'menu_order title', |
| 134 | 'order' => 'ASC', |
| 135 | 'category' => '', |
| 136 | 'cat_operator' => 'IN', |
| 137 | ), |
| 138 | (array) $atts |
| 139 | ); |
| 140 | |
| 141 | $shortcode = new WC_Shortcode_Products( $atts, 'product_category' ); |
| 142 | |
| 143 | return $shortcode->get_content(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * List all (or limited) product categories. |
| 148 | * |
| 149 | * @param array $atts Attributes. |
| 150 | * @return string |
| 151 | */ |
| 152 | public static function product_categories( $atts ) { |
| 153 | if ( isset( $atts['number'] ) ) { |
| 154 | $atts['limit'] = $atts['number']; |
| 155 | } |
| 156 | |
| 157 | $atts = shortcode_atts( |
| 158 | array( |
| 159 | 'limit' => '-1', |
| 160 | 'orderby' => 'name', |
| 161 | 'order' => 'ASC', |
| 162 | 'columns' => '4', |
| 163 | 'hide_empty' => 1, |
| 164 | 'parent' => '', |
| 165 | 'ids' => '', |
| 166 | ), |
| 167 | $atts, |
| 168 | 'product_categories' |
| 169 | ); |
| 170 | |
| 171 | $ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) ); |
| 172 | $hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0; |
| 173 | |
| 174 | // Get terms and workaround WP bug with parents/pad counts. |
| 175 | $args = array( |
| 176 | 'orderby' => $atts['orderby'], |
| 177 | 'order' => $atts['order'], |
| 178 | 'hide_empty' => $hide_empty, |
| 179 | 'include' => $ids, |
| 180 | 'pad_counts' => true, |
| 181 | 'child_of' => $atts['parent'], |
| 182 | ); |
| 183 | |
| 184 | $product_categories = apply_filters( |
| 185 | 'woocommerce_product_categories', |
| 186 | get_terms( 'product_cat', $args ) |
| 187 | ); |
| 188 | |
| 189 | if ( '' !== $atts['parent'] ) { |
| 190 | $product_categories = wp_list_filter( |
| 191 | $product_categories, |
| 192 | array( |
| 193 | 'parent' => $atts['parent'], |
| 194 | ) |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | if ( $hide_empty ) { |
| 199 | foreach ( $product_categories as $key => $category ) { |
| 200 | if ( 0 === $category->count ) { |
| 201 | unset( $product_categories[ $key ] ); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | $atts['limit'] = '-1' === $atts['limit'] ? null : intval( $atts['limit'] ); |
| 207 | if ( $atts['limit'] ) { |
| 208 | $product_categories = array_slice( $product_categories, 0, $atts['limit'] ); |
| 209 | } |
| 210 | |
| 211 | $columns = absint( $atts['columns'] ); |
| 212 | |
| 213 | wc_set_loop_prop( 'columns', $columns ); |
| 214 | wc_set_loop_prop( 'is_shortcode', true ); |
| 215 | |
| 216 | ob_start(); |
| 217 | |
| 218 | if ( $product_categories ) { |
| 219 | woocommerce_product_loop_start(); |
| 220 | |
| 221 | foreach ( $product_categories as $category ) { |
| 222 | wc_get_template( |
| 223 | 'content-product_cat.php', |
| 224 | array( |
| 225 | 'category' => $category, |
| 226 | ) |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | woocommerce_product_loop_end(); |
| 231 | } |
| 232 | |
| 233 | wc_reset_loop(); |
| 234 | |
| 235 | return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>'; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Recent Products shortcode. |
| 240 | * |
| 241 | * @param array $atts Attributes. |
| 242 | * @return string |
| 243 | */ |
| 244 | public static function recent_products( $atts ) { |
| 245 | $atts = array_merge( |
| 246 | array( |
| 247 | 'limit' => '12', |
| 248 | 'columns' => '4', |
| 249 | 'orderby' => 'date', |
| 250 | 'order' => 'DESC', |
| 251 | 'category' => '', |
| 252 | 'cat_operator' => 'IN', |
| 253 | ), |
| 254 | (array) $atts |
| 255 | ); |
| 256 | |
| 257 | $shortcode = new WC_Shortcode_Products( $atts, 'recent_products' ); |
| 258 | |
| 259 | return $shortcode->get_content(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * List multiple products shortcode. |
| 264 | * |
| 265 | * @param array $atts Attributes. |
| 266 | * @return string |
| 267 | */ |
| 268 | public static function products( $atts ) { |
| 269 | $atts = (array) $atts; |
| 270 | $type = 'products'; |
| 271 | |
| 272 | // Allow list product based on specific cases. |
| 273 | if ( isset( $atts['on_sale'] ) && wc_string_to_bool( $atts['on_sale'] ) ) { |
| 274 | $type = 'sale_products'; |
| 275 | } elseif ( isset( $atts['best_selling'] ) && wc_string_to_bool( $atts['best_selling'] ) ) { |
| 276 | $type = 'best_selling_products'; |
| 277 | } elseif ( isset( $atts['top_rated'] ) && wc_string_to_bool( $atts['top_rated'] ) ) { |
| 278 | $type = 'top_rated_products'; |
| 279 | } |
| 280 | |
| 281 | $shortcode = new WC_Shortcode_Products( $atts, $type ); |
| 282 | |
| 283 | return $shortcode->get_content(); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Display a single product. |
| 288 | * |
| 289 | * @param array $atts Attributes. |
| 290 | * @return string |
| 291 | */ |
| 292 | public static function product( $atts ) { |
| 293 | if ( empty( $atts ) ) { |
| 294 | return ''; |
| 295 | } |
| 296 | |
| 297 | $atts['skus'] = isset( $atts['sku'] ) ? $atts['sku'] : ''; |
| 298 | $atts['ids'] = isset( $atts['id'] ) ? $atts['id'] : ''; |
| 299 | $atts['limit'] = '1'; |
| 300 | $shortcode = new WC_Shortcode_Products( (array) $atts, 'product' ); |
| 301 | |
| 302 | return $shortcode->get_content(); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Display a single product price + cart button. |
| 307 | * |
| 308 | * @param array $atts Attributes. |
| 309 | * @return string |
| 310 | */ |
| 311 | public static function product_add_to_cart( $atts ) { |
| 312 | global $post; |
| 313 | |
| 314 | if ( empty( $atts ) ) { |
| 315 | return ''; |
| 316 | } |
| 317 | |
| 318 | $atts = shortcode_atts( |
| 319 | array( |
| 320 | 'id' => '', |
| 321 | 'class' => '', |
| 322 | 'quantity' => '1', |
| 323 | 'sku' => '', |
| 324 | 'style' => 'border:4px solid #ccc; padding: 12px;', |
| 325 | 'show_price' => 'true', |
| 326 | ), |
| 327 | $atts, |
| 328 | 'product_add_to_cart' |
| 329 | ); |
| 330 | |
| 331 | if ( ! empty( $atts['id'] ) ) { |
| 332 | $product_data = get_post( $atts['id'] ); |
| 333 | } elseif ( ! empty( $atts['sku'] ) ) { |
| 334 | $product_id = wc_get_product_id_by_sku( $atts['sku'] ); |
| 335 | $product_data = get_post( $product_id ); |
| 336 | } else { |
| 337 | return ''; |
| 338 | } |
| 339 | |
| 340 | $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false; |
| 341 | |
| 342 | if ( ! $product ) { |
| 343 | return ''; |
| 344 | } |
| 345 | |
| 346 | ob_start(); |
| 347 | |
| 348 | echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr( $atts['class'] ) . '" style="' . ( empty( $atts['style'] ) ? '' : esc_attr( $atts['style'] ) ) . '">'; |
| 349 | |
| 350 | if ( wc_string_to_bool( $atts['show_price'] ) ) { |
| 351 | // @codingStandardsIgnoreStart |
| 352 | echo $product->get_price_html(); |
| 353 | // @codingStandardsIgnoreEnd |
| 354 | } |
| 355 | |
| 356 | woocommerce_template_loop_add_to_cart( |
| 357 | array( |
| 358 | 'quantity' => $atts['quantity'], |
| 359 | ) |
| 360 | ); |
| 361 | |
| 362 | echo '</p>'; |
| 363 | |
| 364 | // Restore Product global in case this is shown inside a product post. |
| 365 | wc_setup_product_data( $post ); |
| 366 | |
| 367 | return ob_get_clean(); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Get the add to cart URL for a product. |
| 372 | * |
| 373 | * @param array $atts Attributes. |
| 374 | * @return string |
| 375 | */ |
| 376 | public static function product_add_to_cart_url( $atts ) { |
| 377 | if ( empty( $atts ) ) { |
| 378 | return ''; |
| 379 | } |
| 380 | |
| 381 | if ( isset( $atts['id'] ) ) { |
| 382 | $product_data = get_post( $atts['id'] ); |
| 383 | } elseif ( isset( $atts['sku'] ) ) { |
| 384 | $product_id = wc_get_product_id_by_sku( $atts['sku'] ); |
| 385 | $product_data = get_post( $product_id ); |
| 386 | } else { |
| 387 | return ''; |
| 388 | } |
| 389 | |
| 390 | $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false; |
| 391 | |
| 392 | if ( ! $product ) { |
| 393 | return ''; |
| 394 | } |
| 395 | |
| 396 | $_product = wc_get_product( $product_data ); |
| 397 | |
| 398 | return esc_url( $_product->add_to_cart_url() ); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * List all products on sale. |
| 403 | * |
| 404 | * @param array $atts Attributes. |
| 405 | * @return string |
| 406 | */ |
| 407 | public static function sale_products( $atts ) { |
| 408 | $atts = array_merge( |
| 409 | array( |
| 410 | 'limit' => '12', |
| 411 | 'columns' => '4', |
| 412 | 'orderby' => 'title', |
| 413 | 'order' => 'ASC', |
| 414 | 'category' => '', |
| 415 | 'cat_operator' => 'IN', |
| 416 | ), |
| 417 | (array) $atts |
| 418 | ); |
| 419 | |
| 420 | $shortcode = new WC_Shortcode_Products( $atts, 'sale_products' ); |
| 421 | |
| 422 | return $shortcode->get_content(); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * List best selling products on sale. |
| 427 | * |
| 428 | * @param array $atts Attributes. |
| 429 | * @return string |
| 430 | */ |
| 431 | public static function best_selling_products( $atts ) { |
| 432 | $atts = array_merge( |
| 433 | array( |
| 434 | 'limit' => '12', |
| 435 | 'columns' => '4', |
| 436 | 'category' => '', |
| 437 | 'cat_operator' => 'IN', |
| 438 | ), |
| 439 | (array) $atts |
| 440 | ); |
| 441 | |
| 442 | $shortcode = new WC_Shortcode_Products( $atts, 'best_selling_products' ); |
| 443 | |
| 444 | return $shortcode->get_content(); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * List top rated products on sale. |
| 449 | * |
| 450 | * @param array $atts Attributes. |
| 451 | * @return string |
| 452 | */ |
| 453 | public static function top_rated_products( $atts ) { |
| 454 | $atts = array_merge( |
| 455 | array( |
| 456 | 'limit' => '12', |
| 457 | 'columns' => '4', |
| 458 | 'orderby' => 'title', |
| 459 | 'order' => 'ASC', |
| 460 | 'category' => '', |
| 461 | 'cat_operator' => 'IN', |
| 462 | ), |
| 463 | (array) $atts |
| 464 | ); |
| 465 | |
| 466 | $shortcode = new WC_Shortcode_Products( $atts, 'top_rated_products' ); |
| 467 | |
| 468 | return $shortcode->get_content(); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Output featured products. |
| 473 | * |
| 474 | * @param array $atts Attributes. |
| 475 | * @return string |
| 476 | */ |
| 477 | public static function featured_products( $atts ) { |
| 478 | $atts = array_merge( |
| 479 | array( |
| 480 | 'limit' => '12', |
| 481 | 'columns' => '4', |
| 482 | 'orderby' => 'date', |
| 483 | 'order' => 'DESC', |
| 484 | 'category' => '', |
| 485 | 'cat_operator' => 'IN', |
| 486 | ), |
| 487 | (array) $atts |
| 488 | ); |
| 489 | |
| 490 | $atts['visibility'] = 'featured'; |
| 491 | |
| 492 | $shortcode = new WC_Shortcode_Products( $atts, 'featured_products' ); |
| 493 | |
| 494 | return $shortcode->get_content(); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Show a single product page. |
| 499 | * |
| 500 | * @param array $atts Attributes. |
| 501 | * @return string |
| 502 | */ |
| 503 | public static function product_page( $atts ) { |
| 504 | if ( empty( $atts ) ) { |
| 505 | return ''; |
| 506 | } |
| 507 | |
| 508 | if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) { |
| 509 | return ''; |
| 510 | } |
| 511 | |
| 512 | $args = array( |
| 513 | 'posts_per_page' => 1, |
| 514 | 'post_type' => 'product', |
| 515 | 'post_status' => ( ! empty( $atts['status'] ) ) ? $atts['status'] : 'publish', |
| 516 | 'ignore_sticky_posts' => 1, |
| 517 | 'no_found_rows' => 1, |
| 518 | ); |
| 519 | |
| 520 | if ( isset( $atts['sku'] ) ) { |
| 521 | $args['meta_query'][] = array( |
| 522 | 'key' => '_sku', |
| 523 | 'value' => sanitize_text_field( $atts['sku'] ), |
| 524 | 'compare' => '=', |
| 525 | ); |
| 526 | |
| 527 | $args['post_type'] = array( 'product', 'product_variation' ); |
| 528 | } |
| 529 | |
| 530 | if ( isset( $atts['id'] ) ) { |
| 531 | $args['p'] = absint( $atts['id'] ); |
| 532 | } |
| 533 | |
| 534 | // Don't render titles if desired. |
| 535 | if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) { |
| 536 | remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); |
| 537 | } |
| 538 | |
| 539 | // Change form action to avoid redirect. |
| 540 | add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' ); |
| 541 | |
| 542 | $single_product = new WP_Query( $args ); |
| 543 | |
| 544 | $preselected_id = '0'; |
| 545 | |
| 546 | // Check if sku is a variation. |
| 547 | if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) { |
| 548 | |
| 549 | $variation = wc_get_product_object( 'variation', $single_product->post->ID ); |
| 550 | $attributes = $variation->get_attributes(); |
| 551 | |
| 552 | // Set preselected id to be used by JS to provide context. |
| 553 | $preselected_id = $single_product->post->ID; |
| 554 | |
| 555 | // Get the parent product object. |
| 556 | $args = array( |
| 557 | 'posts_per_page' => 1, |
| 558 | 'post_type' => 'product', |
| 559 | 'post_status' => 'publish', |
| 560 | 'ignore_sticky_posts' => 1, |
| 561 | 'no_found_rows' => 1, |
| 562 | 'p' => $single_product->post->post_parent, |
| 563 | ); |
| 564 | |
| 565 | $single_product = new WP_Query( $args ); |
| 566 | ?> |
| 567 | <script type="text/javascript"> |
| 568 | jQuery( function( $ ) { |
| 569 | var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' ); |
| 570 | |
| 571 | <?php foreach ( $attributes as $attr => $value ) { ?> |
| 572 | $variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' ); |
| 573 | <?php } ?> |
| 574 | }); |
| 575 | </script> |
| 576 | <?php |
| 577 | } |
| 578 | |
| 579 | // For "is_single" to always make load comments_template() for reviews. |
| 580 | $single_product->is_single = true; |
| 581 | |
| 582 | ob_start(); |
| 583 | |
| 584 | global $wp_query; |
| 585 | |
| 586 | // Backup query object so following loops think this is a product page. |
| 587 | $previous_wp_query = $wp_query; |
| 588 | // @codingStandardsIgnoreStart |
| 589 | $wp_query = $single_product; |
| 590 | // @codingStandardsIgnoreEnd |
| 591 | |
| 592 | wp_enqueue_script( 'wc-single-product' ); |
| 593 | |
| 594 | while ( $single_product->have_posts() ) { |
| 595 | $single_product->the_post() |
| 596 | ?> |
| 597 | <div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"> |
| 598 | <?php wc_get_template_part( 'content', 'single-product' ); ?> |
| 599 | </div> |
| 600 | <?php |
| 601 | } |
| 602 | |
| 603 | // Restore $previous_wp_query and reset post data. |
| 604 | // @codingStandardsIgnoreStart |
| 605 | $wp_query = $previous_wp_query; |
| 606 | // @codingStandardsIgnoreEnd |
| 607 | wp_reset_postdata(); |
| 608 | |
| 609 | // Re-enable titles if they were removed. |
| 610 | if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) { |
| 611 | add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); |
| 612 | } |
| 613 | |
| 614 | remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' ); |
| 615 | |
| 616 | return '<div class="woocommerce">' . ob_get_clean() . '</div>'; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Show messages. |
| 621 | * |
| 622 | * @return string |
| 623 | */ |
| 624 | public static function shop_messages() { |
| 625 | if ( ! function_exists( 'wc_print_notices' ) ) { |
| 626 | return ''; |
| 627 | } |
| 628 | return '<div class="woocommerce">' . wc_print_notices( true ) . '</div>'; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Order by rating. |
| 633 | * |
| 634 | * @deprecated 3.2.0 Use WC_Shortcode_Products::order_by_rating_post_clauses(). |
| 635 | * @param array $args Query args. |
| 636 | * @return array |
| 637 | */ |
| 638 | public static function order_by_rating_post_clauses( $args ) { |
| 639 | return WC_Shortcode_Products::order_by_rating_post_clauses( $args ); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * List products with an attribute shortcode. |
| 644 | * Example [product_attribute attribute="color" filter="black"]. |
| 645 | * |
| 646 | * @param array $atts Attributes. |
| 647 | * @return string |
| 648 | */ |
| 649 | public static function product_attribute( $atts ) { |
| 650 | $atts = array_merge( |
| 651 | array( |
| 652 | 'limit' => '12', |
| 653 | 'columns' => '4', |
| 654 | 'orderby' => 'title', |
| 655 | 'order' => 'ASC', |
| 656 | 'attribute' => '', |
| 657 | 'terms' => '', |
| 658 | ), |
| 659 | (array) $atts |
| 660 | ); |
| 661 | |
| 662 | if ( empty( $atts['attribute'] ) ) { |
| 663 | return ''; |
| 664 | } |
| 665 | |
| 666 | $shortcode = new WC_Shortcode_Products( $atts, 'product_attribute' ); |
| 667 | |
| 668 | return $shortcode->get_content(); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * List related products. |
| 673 | * |
| 674 | * @param array $atts Attributes. |
| 675 | * @return string |
| 676 | */ |
| 677 | public static function related_products( $atts ) { |
| 678 | if ( isset( $atts['per_page'] ) ) { |
| 679 | $atts['limit'] = $atts['per_page']; |
| 680 | } |
| 681 | |
| 682 | // @codingStandardsIgnoreStart |
| 683 | $atts = shortcode_atts( array( |
| 684 | 'limit' => '4', |
| 685 | 'columns' => '4', |
| 686 | 'orderby' => 'rand', |
| 687 | ), $atts, 'related_products' ); |
| 688 | // @codingStandardsIgnoreEnd |
| 689 | |
| 690 | ob_start(); |
| 691 | |
| 692 | // Rename arg. |
| 693 | $atts['posts_per_page'] = absint( $atts['limit'] ); |
| 694 | |
| 695 | woocommerce_related_products( $atts ); |
| 696 | |
| 697 | return ob_get_clean(); |
| 698 | } |
| 699 | } |
| 700 |