在我之前的问题之后,我有一个问题:Problem in displaying the WordPress shortcode function randomlyCalling multiple variables integrated with HTML in PHP arrays

正如我在上一个问题中所问的,我想防止重复的短码内容出现在我的内容页面上(例如,我可能在一篇文章中使用这个短码5次,我不想在那5次中有重复的内容.即使我总共有4个产品,并且使用了这个短码5次,少了一个产品会显示替代文本而不是显示重复的产品),并且我上一个问题的代码是正确的,但是现在我想将产品的值整合到我之前的代码中,而我在下面写的代码是不正确的,因为有时它会显示重复的产品(我在WordPress帖子中使用这些短码)

我的代码是:

function get_product_id() {
    $args = array(
        'limit' => -1,
        'status' => 'publish',
        'return' => 'ids',
    );
    $all_products = wc_get_products( $args );
    $key = array_rand($all_products);
    $products_id = $all_products[$key];
    return $products_id;
}

function my_shortcode() {
    $products_id = get_product_id();
    $product = wc_get_product( $products_id );
    $product_price = $product->get_price();           
    $product_sale_price = $product->get_sale_price(); 
    $product_regular_price = $product->get_regular_price();
    if (isset($product_price) && $product_price > 0) {
        $product_price = number_format($product_price);  
    }
    if (isset($product_sale_price) && $product_sale_price > 0) {
        $product_sale_price = number_format($product_sale_price);  
    }
    if (isset($product_regular_price) && $product_regular_price > 0) {
        $product_regular_price = number_format($product_regular_price); 
    }
    $product_price_currency_symbol = get_woocommerce_currency_symbol();
    $image_id  = $product->get_image_id();
    $product_image = wp_get_attachment_image_url( $image_id, 'full' );  
    $product_title = $product->get_title();
    $product_link = $product->get_permalink();
    $discount = '';
    if ( $product->is_on_sale() ) {
        $max_percentage = 0;

        $percentage = 0;

        $price = $product->get_regular_price();
        $sale = $product->get_sale_price();

        if ( $price != 0 && ! empty( $sale ) ) {
            $percentage = ( $price - $sale ) / $price * 100;
        }
        if ( $percentage > $max_percentage ) {
            $max_percentage = $percentage;
        }
        
        if ($max_percentage <> 0) {
            $discount = '<div class="saved-sale">-' . round($max_percentage) . '% Off</div>';
        }
    } else {
        $product_regular_price = '';
    }
    
    
    $values = [
        '1' => '
            <a href="' . $product_link . '">
                <img src="' . $product_image . '">
                <span> '. $discount .' </span>
                <span> ' . $product_title . ' </span>
            </a>    
        ',
    ];

    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 ] = [];
    }

    $unused_values = array_diff( $values, $GLOBALS['my_shortcode_used'][ $post_id ] );

    if ( empty( $unused_values ) ) {
        $GLOBALS['my_shortcode_used'][ $post_id ] = [];
        $unused_values = $values;
    }

    $key = array_rand( $unused_values );

    $GLOBALS['my_shortcode_used'][ $post_id ][] = $unused_values[ $key ];

    return $unused_values[ $key ];
}
add_shortcode('jock', 'my_shortcode');

这个代码是否正确和标准?如果没有,请告诉我问题出在哪里,我应该做哪些更改.

有没有一种解决方案不需要我重复几次$values内的数组?

推荐答案

我审阅了您的代码,您维护了一个全局数组$GLOBALS[‘MY_SHORTCODE_USED’]来跟踪已在帖子中显示的产品.

我已经修改了您的代码,请判断:

function get_product_id() {
    $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(empty($unused_products)) {
        return null;
    }

    $key = array_rand($unused_products);
    $product_id = $unused_products[$key];

    $GLOBALS['my_shortcode_used'][$post_id][] = $product_id;

    return $product_id;
}

function my_shortcode() {
    $products_id = get_product_id();
    
    if(!$products_id) {
        return "All products ";  
    }

    $product = wc_get_product( $products_id );
    $product_price = $product->get_price();           
    $product_sale_price = $product->get_sale_price(); 
    $product_regular_price = $product->get_regular_price();
    
    if (isset($product_price) && $product_price > 0) {
        $product_price = number_format($product_price);  
    }
    if (isset($product_sale_price) && $product_sale_price > 0) {
        $product_sale_price = number_format($product_sale_price);  
    }
    if (isset($product_regular_price) && $product_regular_price > 0) {
        $product_regular_price = number_format($product_regular_price); 
    }
    
    $product_price_currency_symbol = get_woocommerce_currency_symbol();
    $image_id  = $product->get_image_id();
    $product_image = wp_get_attachment_image_url( $image_id, 'full' );  
    $product_title = $product->get_title();
    $product_link = $product->get_permalink();
    $discount = '';
    
    if ($product->is_on_sale()) {
        $max_percentage = 0;
        $percentage = 0;
        $price = $product->get_regular_price();
        $sale = $product->get_sale_price();
        if ($price != 0 && ! empty($sale)) {
            $percentage = ($price - $sale) / $price * 100;
        }
        if ($percentage > $max_percentage) {
            $max_percentage = $percentage;
        }
        if ($max_percentage <> 0) {
            $discount = '<div class="saved-sale">-' . round($max_percentage) . '% Off</div>';
        }
    } else {
        $product_regular_price = '';
    }
    
    $output = '
        <a href="' . $product_link . '">
            <img src="' . $product_image . '">
            <span> '. $discount .' </span>
            <span> ' . $product_title . ' </span>
        </a>';

    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('jock', 'my_shortcode');

Php相关问答推荐

此行动未经授权.升级Laravel时

根据用户组或登录状态显示或隐藏定价后添加的文本

如何在不指定symfony列的情况下从数据库中获取行数组

仅在WooCommerce管理订单视图上显示订单项自定义元数据

使用列入黑名单的单词和自定义业务逻辑的组合将特定的子字符串包装在HTML标记中

将部分产品排除在WooCommerce的计算附加费之外

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

如何配置我的.env文件以在laravel的一个项目中连接两个不同的数据库?

在 Heroku 上部署 Sylius - 路由不正确

Google Drive API:为什么使用服务帐户在下载文件时会将我带到 Google Drive 登录窗口?

如何将文件上传添加到 WooCommerce 结帐?

如何通过帖子自定义元数据进行查询

PHP 将数组值插入到 csv 列

PHP / DOM : 解析 HTML 以提取基于类的数​​据

PHP/Laravel 中的自定义时区

Laravel 查询关系是否为 bool

为什么 ECDSA 384 签名验证在 GO 中失败,但在 PHP 中却没有?

根据数组中的文本文件编号显示星级

标头内容类型:image/jpeg 无效