如果购物车中有特定商品,XCart 会禁用结帐按钮

XCart Disable Checkout Button If Specific Item Is In Cart

我在 XCart 4.6 中使用这段代码来隐藏结帐按钮。

{section name=product loop=$products}
    {if $products[product].productid eq 3065}
     
    {else}
  <a href="http://academyprohair.com/cart.php" style="margin-top:5px;"><img src="{$AltImagesDir}/button_checkout.jpg" alt="" /></a>
    {/if}
    {/section}

它在购物车页面上运行完美,但在其他所有页面上都会中断,并且无论购物车中有什么商品,它都会隐藏结帐按钮。

你的问题有几个问题:

包含购物车中商品的数组 {$products} 仅在购物车和结帐页面上可用。您不能在其他页面上使用它,因为它不存在。

首先,您检查特定商品是否在购物车中的方式是错误的。如果您的购物车中有 5 件没有 id=3065.

的产品,您提供的代码将打印例如 5 link 到购物车

所以,让我们找到解决方案。由于您需要在每个页面上检查一个产品是否在购物车中(以隐藏 Checkout link),因此您需要创建 PHP 脚本来检查项目是否存在。我们将设置全局 smarty 变量 is_product_in_cart,您可以在 TPL 文件中的任何地方使用它。我们要更改的 xcart 核心文件是 home.php 根目录,我们将在显示模板之前添加代码(在结束 func_display('customer/home.tpl', $smarty); 之前):

/* academyprohair.com custom code */
$my_products = func_products_in_cart($cart);
$is_product_in_cart = 'N';
foreach( $my_products as $product ) {
    if ( $product['productid']==3065 ) {
        $is_product_in_cart = 'Y';
        break;
    };
};
$smarty->assign('is_product_in_cart', $is_product_in_cart);

在您的模板中,除了购物车和结帐页面之外的任何地方,您都可以通过以下方式轻松显示购物车 link:

{if $is_product_in_cart neq 'Y' }
    <a href="http://academyprohair.com/cart.php" style="margin-top:5px;"><img src="{$AltImagesDir}/button_checkout.jpg" alt="" /></a>
{/if}