Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce:在购物车页面上显示库存状态_Php_Wordpress_Woocommerce - Fatal编程技术网

Php WooCommerce:在购物车页面上显示库存状态

Php WooCommerce:在购物车页面上显示库存状态,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想显示购物车中每种产品的库存状态,状态为“库存中很少” 像这样: Productname 尺码:L 只剩下4个请在function.php文件中尝试下面的代码 add_filter( 'woocommerce_cart_item_name', 'showing_stock_in_cart_items', 99, 3 ); function showing_stock_in_cart_items( $item_name, $cart_item, $cart_item_key ) {

我想显示购物车中每种产品的库存状态,状态为“库存中很少”

像这样:

Productname

尺码:L


只剩下4个请在function.php文件中尝试下面的代码

  add_filter( 'woocommerce_cart_item_name', 'showing_stock_in_cart_items', 99, 3 );

function showing_stock_in_cart_items( $item_name, $cart_item, $cart_item_key  ) {
    // The WC_Product object
    $product = $cart_item['data'];

    if (empty($product)) {
        return $item_name;
    }

    // Get the  stock
    $stock = $product->get_stock_quantity();

    // When stock doesn't exist
    if (empty($stock)) {
        return $item_name;
    }

   // display the stock
   if ($stock <= '50') :
      $item_name .='<br><p style="color:green;" class="product-stock">'.__( "only " .$stock. " left","woocommerce").'</p>';
   endif;

    return $item_name;
}
add_filter('woocommerce_cart_item_name','show_stock_in_cart_items',99,3);
显示购物车项目中的库存的函数($item\u name、$cart\u item、$cart\u item\u key){
//WC_产品对象
$product=$cart_项目['data'];
if(空($product)){
返回$item_name;
}
//买股票
$stock=$product->get_stock_quantity();
//当股票不存在时
如果(空($stock)){
返回$item_name;
}
//展示股票

如果($stock请在function.php文件中尝试下面的代码

  add_filter( 'woocommerce_cart_item_name', 'showing_stock_in_cart_items', 99, 3 );

function showing_stock_in_cart_items( $item_name, $cart_item, $cart_item_key  ) {
    // The WC_Product object
    $product = $cart_item['data'];

    if (empty($product)) {
        return $item_name;
    }

    // Get the  stock
    $stock = $product->get_stock_quantity();

    // When stock doesn't exist
    if (empty($stock)) {
        return $item_name;
    }

   // display the stock
   if ($stock <= '50') :
      $item_name .='<br><p style="color:green;" class="product-stock">'.__( "only " .$stock. " left","woocommerce").'</p>';
   endif;

    return $item_name;
}
add_filter('woocommerce_cart_item_name','show_stock_in_cart_items',99,3);
显示购物车项目中的库存的函数($item\u name、$cart\u item、$cart\u item\u key){
//WC_产品对象
$product=$cart_项目['data'];
if(空($product)){
返回$item_name;
}
//买股票
$stock=$product->get_stock_quantity();
//当股票不存在时
如果(空($stock)){
返回$item_name;
}
//展示股票

如果($stock谢谢,我添加了一个额外的库存量检查,仅当库存量低于50时才显示消息。否则,它将始终显示消息。请注意:向functions.php添加代码片段通常不是一个好主意,因为在更新主题时,这些编辑将丢失。此外,出错可能会将您锁定在网站之外。On如果您有子主题,请编辑functions.php,代码片段将在活动子主题中使用。有关子主题的更多信息,请参阅Wordpress文档:谢谢,我添加了一个额外的库存量检查,仅当库存量低于50时才显示消息。否则,它将始终显示消息。请注意:将代码片段添加到functions.php通常不是一个好主意,因为更新主题时,这些编辑将丢失。此外,出错可能会将您锁定在网站之外。如果您有子主题,则仅编辑functions.php,并且代码片段将在活动子主题中使用。有关子主题的详细信息,请参阅Wordpress文档: