{"id":55020,"date":"2020-04-04T16:01:45","date_gmt":"2020-04-04T14:01:45","guid":{"rendered":"https:\/\/samuelchopard.ch\/wiki\/?p=55020"},"modified":"2020-04-04T19:46:24","modified_gmt":"2020-04-04T17:46:24","slug":"display-product-quantity-in-cart-and-checkout","status":"publish","type":"post","link":"https:\/\/samuelchopard.ch\/wiki\/library\/display-product-quantity-in-cart-and-checkout\/","title":{"rendered":"Display product quantity in cart and checkout"},"content":{"rendered":"<p><!--more--><br \/>\nPaste the following code in the active theme&#8217;s <em>functions.php<\/em>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\r\n\/* if the product quantity is between X and X, display \"plus que X en stock\" in cart and checkout *\/\r\nadd_filter( 'woocommerce_cart_item_name', 'showing_stock_in_cart_items', 99, 3 );\r\nfunction showing_stock_in_cart_items( $item_name, $cart_item, $cart_item_key  ) {\r\n    \/\/ The WC_Product object\r\n    $product = $cart_item['data'];\r\n\r\n    if (empty($product)) {\r\n        return $item_name;\r\n    }\r\n\r\n    \/\/ Get the stock\r\n    $stock = $product->get_stock_quantity();\r\n\r\n    \/\/ When stock doesn't exist\r\n    if (empty($stock)) {\r\n        return $item_name;\r\n    }\r\n\r\n   \/\/ display the stock\r\n   if ($stock >= '1' && $stock <= '3') {     \/* was ($stock <= '3') *\/\r\n      $item_name .=\" <div class='function-product-quantity' style='font-weight:400;display:inline-block;'>\u00d7 \" . $cart_item['quantity'] . \"<\/div>\" . '<br><p style=\"color:orange;margin-bottom:0px;\" class=\"product-stock\">'.__( \"Plus que \" .$stock. \" en stock\",\"woocommerce\").'<\/p>';\r\n   } else  {\r\n       $item_name .=\" <div class='function-product-quantity' style='font-weight:400;display:inline-block;'>\u00d7 \" . $cart_item['quantity'] . \"<\/div>\";\r\n   }\r\n\r\n    return $item_name;\r\n}<\/pre>\n<p>Edit quantity (range) and text as needed.<\/p>\n<p>Then, paste the following code in Appearance &gt; Customize &gt; Additional CSS:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* remove native item quantity in checkout *\/\r\n.woocommerce-checkout #order_review .product-quantity {\r\n\tdisplay: none;\r\n}\r\n\r\n\/* remove stock function quantity in cart and mini-cart*\/\r\n.woocommerce-cart .cart_item .function-product-quantity,\r\n.woocommerce-mini-cart .function-product-quantity {\r\n\tdisplay: none !important;\r\n}<\/pre>\n<p>Source: <a target=\"_blank\" href=\"https:\/\/stackoverflow.com\/questions\/59001052\/woocommerce-show-stock-status-on-cart-page\">https:\/\/stackoverflow.com\/questions\/59001052\/woocommerce-show-stock-status-on-cart-page<\/a> (+ additional edits made by myself)<\/p>\n","protected":false},"excerpt":{"rendered":"Paste the following code in the active theme's functions.php: \/* if the product quantity is between X and X, display [...]","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[52,58,232,382,327],"class_list":["post-55020","post","type-post","status-publish","format-standard","hentry","category-woocommerce","tag-cart","tag-checkout","tag-product","tag-quantity","tag-woocommerce"],"_links":{"self":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/comments?post=55020"}],"version-history":[{"count":6,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55020\/revisions"}],"predecessor-version":[{"id":55032,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55020\/revisions\/55032"}],"wp:attachment":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/media?parent=55020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/categories?post=55020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/tags?post=55020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}