WC_Shortcodes::product_page │ public static │ WC 1.0
Show a single product page.
Method of the class: WC_Shortcodes{}
Hooks from the method
Returns
String.
Usage
$result = WC_Shortcodes::product_page( $atts );
- $atts(array) (required)
- Attributes.
WC_Shortcodes::product_page() WC Shortcodes::product page code WC 10.3.5
<?php
public static function product_page( $atts ) {
if ( empty( $atts ) ) {
return '';
}
if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
return '';
}
$product_id = isset( $atts['id'] ) ? absint( $atts['id'] ) : 0;
if ( ! $product_id && isset( $atts['sku'] ) ) {
$product_id = wc_get_product_id_by_sku( $atts['sku'] );
}
$product_status = empty( $atts['status'] ) ? ProductStatus::PUBLISH : $atts['status'];
/**
* Filters the list of invalid statuses for the `product_page` shortcode.
*
* @since 8.6.0
* @param array $invalid_statuses List of invalid statuses.
* @param int $product_id Product ID.
* @return array
*/
$invalid_statuses = apply_filters( 'woocommerce_shortcode_product_page_invalid_statuses', array( ProductStatus::TRASH ), $product_id );
if ( in_array( $product_status, $invalid_statuses, true ) ) {
return '';
}
/**
* Filters whether to override read permissions for unpublished products.
*
* @since 8.6.0
* @param bool $force_rendering Whether to override read permissions for unpublished products. `true` to force rendering the product page, `false` to block rendering, or `null` to use the default behavior.
* @param int $product_id Product ID.
* @return bool
*/
$force_rendering = apply_filters( 'woocommerce_shortcode_product_page_force_rendering', null, $product_id );
if ( isset( $force_rendering ) && ! $force_rendering ) {
return '';
}
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => $product_status,
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
);
if ( isset( $atts['sku'] ) ) {
$args['meta_query'][] = array(
'key' => '_sku',
'value' => sanitize_text_field( $atts['sku'] ),
'compare' => '=',
);
$args['post_type'] = array( 'product', 'product_variation' );
}
if ( isset( $atts['id'] ) ) {
$args['p'] = absint( $atts['id'] );
}
// Don't render titles if desired.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
// Change form action to avoid redirect.
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
$single_product = new WP_Query( $args );
if (
! isset( $force_rendering ) &&
$single_product->have_posts() &&
ProductStatus::PUBLISH !== $single_product->post->post_status &&
! current_user_can( 'read_product', $single_product->post->ID )
) {
return '';
}
$preselected_id = '0';
// Check if sku is a variation.
if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
$variation = wc_get_product_object( ProductType::VARIATION, $single_product->post->ID );
$attributes = $variation->get_attributes();
// Set preselected id to be used by JS to provide context.
$preselected_id = $single_product->post->ID;
// Get the parent product object.
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => ProductStatus::PUBLISH,
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'p' => $single_product->post->post_parent,
);
$single_product = new WP_Query( $args );
?>
<script type="text/javascript">
jQuery( function( $ ) {
var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
<?php foreach ( $attributes as $attr => $value ) { ?>
$variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
<?php } ?>
});
</script>
<?php
}
// For "is_single" to always make load comments_template() for reviews.
$single_product->is_single = true;
ob_start();
global $wp_query;
// Backup query object so following loops think this is a product page.
$previous_wp_query = $wp_query;
// @codingStandardsIgnoreStart
$wp_query = $single_product;
// @codingStandardsIgnoreEnd
wp_enqueue_script( 'wc-single-product' );
while ( $single_product->have_posts() ) {
$single_product->the_post()
?>
<div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php
}
// Restore $previous_wp_query and reset post data.
// @codingStandardsIgnoreStart
$wp_query = $previous_wp_query;
// @codingStandardsIgnoreEnd
wp_reset_postdata();
// Re-enable titles if they were removed.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}