WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量 [英] WooCommerce: Display stock quantity for a given product ID on normal Pages or Posts

查看:24
本文介绍了WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WoCommerce 页面之外的普通 WordPress 页面或帖子中显示给定产品 ID 的产品数量.

我假设您将一些代码粘贴到 functions.php 中,然后在帖子或页面中放置了一个片段.
我在这里真的很挣扎,到目前为止我发现的答案只有半生不熟的答案,其中没有一个对我有用......

如何在普通 Wordpress 页面或帖子上回显 WooCommerce 产品 ID 的库存数量?

解决方案

最好的方法是制作一个自定义的短代码函数,即输出给定产品 ID 的产品数量.

这个简码功能的代码:

if(!function_exists('show_specific_product_quantity')) {函数 show_specific_product_quantity( $atts ) {//简码属性$atts = shortcode_atts(大批('id' =>'',//产品 ID 参数),美元,'产品数量');如果(空($atts['id']))返回;$stock_quantity = 0;$product_obj = wc_get_product( intval( $atts['id'] ) );$stock_quantity = $product_obj->get_stock_quantity();如果( $stock_quantity > 0 )返回 $stock_quantity;}add_shortcode('product_qty', 'show_specific_product_quantity');}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

<小时>

用法

<块引用>

短代码使用 ID 参数(目标产品 ID).

1) 在 WordPress 页面或帖子内容中,只需将此短代码粘贴到文本编辑器中即可显示给定产品 ID 的库存数量(此处 ID 为 37):

[product_qty id="37"]

2 在任何 PHP 代码中(示例):

echo '

产品数量是:'.do_shortcode('[product_qty id="37"]') .'</p><br>';

3 在 HTML/PHP 页面中(示例):

<p>产品数量为:<?php echo do_shortcode('[product_qty id="37"]');?></p>

<块引用>

类似: 通过 Woocommerce 上的短代码有条件地显示自定义库存数量

I am trying to display the product quantity for a given product ID outside WoCommerce pages, in the normal WordPress Pages or Posts.

I assume you paste some code into functions.php then have a snippet you put in the post or page.
I'm really struggling here and only half-baked answers I've found so far where none of them work for me...

How do I echo the stock quantity of a WooCommerce product ID on a normal Wordpress page or post?

解决方案

The best way is to make a custom shortcode function, that is going to output the product quantity for a given product ID.

The code for this shortcode function:

if( !function_exists('show_specific_product_quantity') ) {

    function show_specific_product_quantity( $atts ) {

        // Shortcode Attributes
        $atts = shortcode_atts(
            array(
                'id' => '', // Product ID argument
            ),
            $atts,
            'product_qty'
        );

        if( empty($atts['id'])) return;

        $stock_quantity = 0;

        $product_obj = wc_get_product( intval( $atts['id'] ) );
        $stock_quantity = $product_obj->get_stock_quantity();

        if( $stock_quantity > 0 ) return $stock_quantity;

    }

    add_shortcode( 'product_qty', 'show_specific_product_quantity' );

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


USAGE

The shortcode works with an ID argument (the targeted product ID).

1) In a WordPress Page or Post content, just paste this shortcode in the text editor to display the stock quantity for a given product ID (Here the ID is 37):

[product_qty id="37"]

2 In any PHP code (example):

echo '<p>Product quantity is: ' . do_shortcode( '[product_qty id="37"]' ) .'</p><br>';

3 In an HTML/PHP page (example):

<p>Product quantity is: <?php echo do_shortcode( '[product_qty id="37"]' ); ?></p>

Similar: Display custom stock quantity conditionally via shortcode on Woocommerce

这篇关于WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆