简体   繁体   English

在 woocommerce 存档中添加产品类别

[英]Add Product category in woocommerce archive

I'm trying to add the product category inside the product card in the woocommerce archive page.我正在尝试在 woocommerce 存档页面的产品卡中添加产品类别。 Right now it shows "Thumbnail" "Title" "Price" and "Add to cart button".现在它显示“缩略图”“标题”“价格”和“添加到购物车按钮”。

I'm using this function which should work if the variable "product" is set to the current product displayed.我正在使用这个 function 如果变量“产品”设置为当前显示的产品,它应该可以工作。

My Question: Is there a way to get the queried Product in this variable?我的问题:有没有办法在这个变量中获取查询的产品?

Any help is appreciated.任何帮助表示赞赏。

<?php 
//Hook after product title on archive page
add_action('woocommerce_after_shop_loop_item_title','add_category');
    function add_category() {
        // set var "cardcategory"
        $cardcategory = $product->get_categories();
        // Show category and change from plural to singular
        echo '<div class="category-carousel">';

        if (strpos($cardcategory, 'Category_A_plural') !== false) {
            echo '<p class="Category_A_class">Category A singular</p>';
        }
        if (strpos($cardcategory, 'Category_B_plural') !== false) {
                echo '<p class="Category_B_class">Category B singular</p>';
        }
        echo '</div>';
    }
<?php 
//Hook after product title on archive page
add_action('woocommerce_after_shop_loop_item_title','add_category');

function add_category() {
    global $product; // You need to add this.

    // set var "cardcategory"
    $cardcategory = $product->get_categories();
    // Show category and change from plural to singular
    echo '<div class="category-carousel">';

    if (strpos($cardcategory, 'Category_A_plural') !== false) {
        echo '<p class="Category_A_class">Category A singular</p>';
    }
    if (strpos($cardcategory, 'Category_B_plural') !== false) {
            echo '<p class="Category_B_class">Category A singular</p>';
    }
    echo '</div>';
}

You need to add the line global $product as shown above.您需要添加行global $product如上所示。 $product is an instance of WC_Product , I believe Woocommerce uses WordPress' the_post() functionality under the hood to set this variable but I'm not 100% sure. $productWC_Product的一个实例,我相信 Woocommerce 在后台使用 WordPress 的the_post()功能来设置这个变量,但我不是 100% 确定。

暂无
暂无

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

相关问题 WooCommerce产品类别存档,添加其他详细信息 - WooCommerce product category archive, add additional details 在 Woocommerce 中为产品类别存档页面添加正文类 - Add a body class for product category archive page in Woocommerce 在特定的 Woocommerce 产品类别存档页面下添加文本 - Add a text under specific Woocommerce product category archive page 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages 为 WooCommerce 类别存档页面添加产品标题标签 - Add product title heading tag for WooCommerce category archive pages 从子类别术语中定位 WooCommerce 产品类别存档页面 - Target WooCommerce product category archive pages from a child category term 如果产品属于 WooCommerce 中的某个类别,则在存档页面的价格下方添加自定义文本 - Add custom text below price on archive pages if product is in a certain category in WooCommerce 根据 Woocommerce 存档页面中的产品类别更改产品按钮 - Change product button based on product category in Woocommerce archive pages Woocommerce - 在商店/存档页面的产品标题下显示产品类别 - Woocommerce - Display the product category under product title in shop/archive page Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM