Add as shortcode in editor
-
We tried adding the default WooCommerce Shortcode to show a single product outside the WooCommerce loop – adding it to the Default Page Template in our theme, but it doesn’t work.
Also tried using a custom function to create our own shortcode https://stackoverflow.com/questions/55387735/how-to-use-variation-and-add-to-cart-button-outside-woocommerce-loop
and it didn’t work either.
The color swatches appear as a blank bullet list. The javascript and css are not loading in the page template, is there any way to use shortcode to show a product using swatches?
We only want the Price, Color, and Quantity + Add To Cart button.
This is the code we’re using now, it’s in the functions.php file:
// Product Embed Shortcode function add_to_cart_form_shortcode( $atts ) { if ( empty( $atts ) ) { return ''; } if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) { return ''; } $args = array( 'posts_per_page' => 1, 'post_type' => 'product', 'post_status' => 'publish', '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'] ); } $single_product = new WP_Query( $args ); $preselected_id = '0'; if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) { $variation = new WC_Product_Variation( $single_product->post->ID ); $attributes = $variation->get_attributes(); $preselected_id = $single_product->post->ID; $args = array( 'posts_per_page' => 1, 'post_type' => 'product', 'post_status' => '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( document ).ready( 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 } $single_product->is_single = true; ob_start(); global $wp_query; $previous_wp_query = $wp_query; $wp_query = $single_product; 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 woocommerce_template_single_add_to_cart(); ?> </div> <?php } $wp_query = $previous_wp_query; wp_reset_postdata(); return '<div class="woocommerce">' . ob_get_clean() . '</div>'; } add_shortcode( 'add_to_cart_form', 'add_to_cart_form_shortcode' ); /*Example Usage [add_to_cart_form id=147]*/ // End Product Embed Shortcode
Thanks!
- The topic ‘Add as shortcode in editor’ is closed to new replies.