简体   繁体   English

如何在 woocommerce 上的 php 代码上添加更多 id 以用于产品的自定义内容?

[英]how can i add more ids on php code on woocommerce for custom content for products?

I'm new to the PHP world, I wanted to ask, how can I add more product ids to this code?我是 PHP 世界的新手,我想问一下,如何在这段代码中添加更多产品 ID?

Unfortunately I don't know how to separate multiple IDs.不幸的是,我不知道如何分隔多个 ID。

I would like to enter multiple ids so that specific content is only displayed for certain products.我想输入多个 ID,以便仅针对某些产品显示特定内容。

add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
    global $product;

    // Limit to a specific product ID only (Set your product ID below )
    if( $product->get_id() != 37 ) return;

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    // End of content
}

If you want to the content to show on specific products, you can use in_array() :如果要在特定产品上显示内容,可以使用in_array()

add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
    global $product;

    /* Add the product ids to the array separated by commas that you want the content below to display on
     * in_array() arguments:
     * $product->get_id() is the needle - what you're looking for in the array
     * [1,2,3,4,5] are the product ids you want to show the content on. Replace those with your actual ids
     * TRUE just makes sure it's the same type of data. In this case we set it to TRUE, since $product->get_id() is an integer and so is the data in the array.
    */ 
    if( in_array( $product->get_id(), [37,1,2,3,6,7], TRUE ) :

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    /* If the ids are not in the array above, return the function without output. */
    else :
        return;
    endif;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 特定 ID 产品页面上的多个产品中添加自定义内容 - Add custom content into multiple products on WooCommerce specific IDs product pages 如何在WooCommerce平台上为我的“最新产品”添加滚动/查看更多功能? - How can I add a scroll/see more feature to my 'Latest Products' on the WooCommerce platform? WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart 使用PHP代码以编程方式将多个新产品添加到网站woocommerce - Add multiple New products to website programmatically woocommerce Using PHP code 如何使用自定义 PHP 脚本添加产品? - How do I add products with custom PHP script? 如何将自定义字段添加到 WooCommerce 设置中的“产品库存”选项卡? - How do I add a custom field to the "Products Inventory" tab in the WooCommerce settings? WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 如何向 WooCommerce 产品添加(第二个)自定义 sku 字段? - How to add a (second) custom sku field to WooCommerce products? 如何在 WooCommerce 4+ 中为产品添加自定义库存状态 - How to add custom stock status to products in WooCommerce 4+ 如何为该代码添加更多类? - How can I add more classes for this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM