get_product_obj( $product_id ); if ( ! $product ) { return ''; } return $product->get_title(); } /** * Show a single product add to card. * * @param array $atts Attributes. */ public function add_to_cart( $atts ) { if ( empty( $atts['id'] ) ) { return ''; } $args = array( 'posts_per_page' => 1, 'post_type' => 'product', 'post_status' => ( ! empty( $atts['status'] ) ) ? $atts['status'] : 'publish', 'ignore_sticky_posts' => 1, 'no_found_rows' => 1, ); if ( isset( $atts['id'] ) ) { $args['p'] = absint( $atts['id'] ); } // Update the add to cart button text. if ( isset( $atts['text'] ) && ! empty( $atts['text'] ) ) { self::$add_to_cart_text = sanitize_text_field( $atts['text'] ); add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'replace_add_to_cart_text' ), 40, 2 ); } // 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 ); // For "is_single" to always make load comments_template() for reviews. $single_product->is_single = true; ob_start(); global $product; global $wp_query; // Backup query object so following loops think this is a product page. $previous_wp_query = $wp_query; $wp_query = $single_product; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited wp_enqueue_script( 'wc-single-product' ); while ( $single_product->have_posts() ) { $single_product->the_post(); woocommerce_template_single_add_to_cart(); /* ?>
' . ob_get_clean() . ''; } /** * Show a single product add to card. * * @param string $add_to_cart_text Button text. * @param obj $product Product object. * * @return string Add to cart button text. */ public function replace_add_to_cart_text( $add_to_cart_text, $product ) { if ( ! empty( self::$add_to_cart_text ) ) { $add_to_cart_text = self::$add_to_cart_text; } return $add_to_cart_text; } } /** * Kicking this off by calling 'get_instance()' method */ Cartflows_Wd_Flow_Shortcodes::get_instance();