Ecommerce Shopify WordPress Discussion

Show two items ,with preventing the display of duplicate products in the WordPress shortcode

I would like to create something similar to the question in this link Preventing the display of duplicate products in the WordPress shortcode and requesting code correction, with a slight variation. Instead of displaying $output for one product, I want to display 2 or more products with the same original conditions, meaning the products should not be duplicates. An additional request (if possible, please provide an answer) My source code: <?php function get_products_with_ids() { $args = array( 'limit' => -1, 'status' => 'publish', 'return' => 'ids', ); $all_products = wc_get_products( $args ); $post_id = get_the_ID(); $used_products = isset($GLOBALS['my_shortcode_used'][$post_id]) ? $GLOBALS['my_shortcode_used'][$post_id] : []; $unused_products = array_diff($all_products, $used_products); if(count($unused_products) < 2) { return null; } $keys = array_rand($unused_products, 2); $product_ids = array(); $product_ids[] = $unused_products[$keys[0]]; $product_ids[] = $unused_products[$keys[1]]; $GLOBALS['my_shortcode_used'][$post_id] = array_merge($GLOBALS['my_shortcode_used'][$post_id], $product_ids); return $product_ids; } function my_shortcode_sample() { $products_ids = get_products_with_ids(); if (empty($products_ids)) { return "No products found"; } $output = ''; foreach ($products_ids as $product_id) { $product = wc_get_product($product_id); $product_price = $product->get_price(); $product_sale_price = $product->get_sale_price(); $product_regular_price = $product->get_regular_price(); if (get_woocommerce_currency() == 'USD') { //do something } $product_price_currency_symbol = get_woocommerce_currency_symbol(); $image_id = $product->get_image_id(); $product_image = wp_get_attachment_image_url($image_id, 'medium'); $product_title = $product->get_title(); $product_link = $product->get_permalink(); $discount_sample = ''; if ($product->is_on_sale()) { $max_percentage = 0; $percentage = 0; if ($product->is_type('simple')) { if ($product->get_sale_price() > $product->get_price()) { $sale = $product->get_sale_price(); } else { $sale = $product->get_price(); } $price = $product->get_regular_price(); } elseif ($product->is_type('variable')) { $sale = $product->get_variation_sale_price('min', true); $price = $product->get_variation_regular_price('max', true); $product_regular_price = $price; } if ($price != 0 && !empty($sale)) { $percentage = ($price - $sale) / $price * 100; } if ($percentage > $max_percentage) { $max_percentage = $percentage; } if ($max_percentage <> 0) { $discount_sample = '<span>' . round($max_percentage) . '%' . '</span>'; $product_regular_price = '<div>' . $product_regular_price . '</div>'; } } else { $product_regular_price = ''; } $output .= ' <section> <div> <a href="' . $product_link1 . '" title="' . $product_title1 . '" target="_blank"> <img src="' . $product_image1 . '" alt="' . $product_title1 . '"> </a> </div> <div> <h4> <a href="' . $product_link1 . '" title="' . $product_title1 . '" target="_blank"> ' . $product_title1 . ' </a> </h4> <div> ' . $discount_sample1 . $product_regular_price1 . ' <div>' . $product_price1 . '<span>' . ' ' . $product_price_currency_symbol1 . '</span> </div> </div> <div> <a href="' . $product_link1 . '" title="' . $product_title1 . '" target="_blank">View sample</a> </div> </div> </section> '; $output .= ' <section> <div> <a href="' . $product_link2 . '" title="' . $product_title2 . '" target="_blank"> <img src="' . $product_image2 . '" alt="' . $product_title2 . '"> </a> </div> <div> <h4> <a href="' . $product_link2 . '" title="' . $product_title2 . '" target="_blank"> ' . $product_title2 . ' </a> </h4> <div> ' . $discount_sample2 . $product_regular_price2 . ' <div>' . $product_price2 . '<span>' . ' ' . $product_price_currency_symbol2 . '</span> </div> </div> <div> <a href="' . $product_link2 . '" title="' . $product_title2 . '" target="_blank">View sample</a> </div> </div> </section> '; } if (!isset($GLOBALS['my_shortcode_used'])) { $GLOBALS['my_shortcode_used'] = []; } $post_id = get_the_ID(); if (!isset($GLOBALS['my_shortcode_used'][$post_id]) || !is_array($GLOBALS['my_shortcode_used'][$post_id])) { $GLOBALS['my_shortcode_used'][$post_id] = []; } $values = ['1' => $output]; $unused_values = array_diff($values, $GLOBALS['my_shortcode_used'][$post_id]); if (empty($unused_values)) { return "Alternative text"; } $key = array_rand($unused_values); $GLOBALS['my_shortcode_used'][$post_id][] = $unused_values[$key]; return $unused_values[$key]; } add_shortcode('shortcode_inside_content', 'my_shortcode_sample');
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.