PrestaShop 1.7:如何为单个商品设置show_price及排查属性异常
$product.show_price in PrestaShop 1.7 Hey there! Let's tackle your questions about $product.show_price in PrestaShop 1.7—this is a common point of confusion, so I’ll break it down clearly.
1. Can You Configure show_price Directly in the Backend?
Short answer: There’s no dedicated toggle for show_price in the default PrestaShop backend, but you can control it indirectly through several built-in settings:
- Disable the product: If a product is marked as Disabled (under Catalog > Products > Status),
show_pricewill automatically befalsesince disabled products don’t display prices. - Leave price fields empty: If neither the regular price nor wholesale price is set for the product (or its combinations, if applicable), PrestaShop hides the price, setting
show_pricetofalse. - Restrict customer group access: If a product is limited to specific customer groups (Catalog > Products > Groups) and the current user isn’t in that group, the price will be hidden.
2. Why Would $product.show_price Be false Unexpectedly?
If you’re seeing show_price set to false for products you expect to display prices, check these common triggers:
- Hidden parent category: If the product’s parent category is set to Hidden (Catalog > Categories > Status), the product may inherit this restriction, leading to a hidden price.
- Third-party modules: Modules like wholesale pricing tools, age verification plugins, or membership access systems often override
show_priceto hide prices for specific users or conditions. Scan your installed modules for anything related to price visibility. - Theme overrides: Custom themes sometimes modify the
Productclass or template logic to forceshow_pricetofalse(e.g., for pre-order items). Check your theme’soverride/classes/Product.phpfile or template files likeproduct.tpl. - Out-of-stock settings: While not universal, some themes hide prices for out-of-stock products if you’ve disabled "Allow ordering out-of-stock products" (Shop Parameters > Product Settings).
3. How to Force show_price for Specific Products
If you need granular control beyond backend settings, here are three reliable methods:
Option 1: Override the Product Class
Create an override to modify show_price based on product ID:
- Create (or edit)
override/classes/Product.php - Add this code:
class Product extends ProductCore { public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) { parent::__construct($id_product, $full, $id_lang, $id_shop, $context); // Example: Force show_price=true for product ID 123, false for 456 if ($this->id === 123) { $this->show_price = true; } elseif ($this->id === 456) { $this->show_price = false; } } }
- Clear your cache (Advanced Parameters > Performance > Clear Cache) to apply the override.
Option 2: Adjust Template Files
If you only need to change display (not the core property), edit your theme’s product template (e.g., themes/your-theme/templates/catalog/product/product.tpl):
{* Force price display for product ID 123 *} {if $product.id == 123} {assign var='product.show_price' value=true} {/if} {* Hide price for product ID 456 *} {if $product.id == 456} {assign var='product.show_price' value=false} {/if} {* Keep your original price display code *} {if $product.show_price} <div class="product-price-container"> {$product.price} </div> {/if}
Option 3: Build a Custom Module
For a non-intrusive solution, create a simple module that hooks into actionProductLoad to modify show_price dynamically:
class CustomPriceVisibility extends Module { public function __construct() { $this->name = 'custompricevisibility'; $this->tab = 'front_office_features'; $this->version = '1.0.0'; $this->author = 'Your Name'; parent::__construct(); } public function install() { return parent::install() && $this->registerHook('actionProductLoad'); } public function hookActionProductLoad($params) { $product = $params['product']; // Adjust based on your criteria (product ID, category, etc.) if ($product->id === 789) { $product->show_price = false; } } }
Quick Troubleshooting Trick
If you’re still stuck, enable Debug Mode (Advanced Parameters > Performance > Debug Mode) and add {var_dump($product)} to your product template. This will show you all product properties, helping you spot if a module or theme is modifying show_price behind the scenes.
内容的提问来源于stack exchange,提问作者kimo

