有人知道如何在 prestashop 1.7.6 的添加到购物车模式中添加相关产品吗?
我的意思是:
prestashop有原生相关产品,可以在后台产品页面添加。
在主题product.tpl中,它们包含在一个块中:
{block name='product_accessories'}
{if $accessories}
{foreach from=$accessories item="product_accessory"}
{block name='product_miniature'}
{include file='catalog/_partials/miniatures/product-related.tpl' product=$product_accessory}
{/block}
{/foreach}
{/if}
{/block}
你知道怎么做吗?
谢谢你
这比看起来更棘手。修改模板很简单,因为它只是覆盖 ps_shoppingcart 模块中的文件“modal.tpl”,即在您的主题中,创建文件“modules/ps_shoppingcart/modal.tpl”,然后从模块并对其进行定制。在“modal-body”div 中添加类似的内容:
<div class="row">
{if isset($accessories) && $accessories}
{foreach from=$accessories item="product_accessory"}
{include file="$theme_tpl_path" product=$product_accessory}
{/foreach}
{/if}
</div>
我们稍后会回到 $theme_tpl_path。然而,当涉及到模板变量时,就困难得多......
您需要获取配件,这必须在您自己的 ps_shoppingcart 模块版本中完成(它们必须在 Ps_Shoppingcart::renderModal() 成员函数中分配)。它可以通过重写 FrontController 类来完成,但这并没有真正更好。无论您需要在您的模块版本中复制 ProductController 类中的内容(或者修改模块并承受记住您无法在不丢失更改的情况下更新它的后果):
public function renderModal($id_product, $id_product_attribute, $id_customization)
{
$data = $this->getPresentedCart();
$product = null;
foreach ($data['products'] as $p) {
if ((int) $p['id_product'] == $id_product &&
(int) $p['id_product_attribute'] == $id_product_attribute &&
(int) $p['id_customization'] == $id_customization) {
$product = $p;
break;
}
}
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new \PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingPresenter(
new ImageRetriever(
$this->context->link
),
$this->context->link,
new PriceFormatter(),
new ProductColorsRetriever(),
$this->context->getTranslator()
);
$tempProduct = new Product($product->id);
$accessories = $tempProduct->getAccessories($this->context->language->id);
if (is_array($accessories)) {
foreach ($accessories as &$accessory) {
$accessory = $presenter->present(
$presentationSettings,
Product::getProductProperties($this->context->language->id, $accessory, $this->context),
$this->context->language
);
}
unset($accessory);
}
$this->smarty->assign([
'accessories' => $accessories,
'theme_tpl_path'=> _PS_THEME_DIR_.'templates/catalog/_partials/miniatures/product.tpl',
'product' => $product,
'cart' => $data,
'cart_url' => $this->getCartSummaryURL(),
]);
return $this->fetch('module:ps_shoppingcart/modal.tpl');
}
上面将使用主题中的产品微型模型的模板文件,并且模板必须通过该变量传递,否则它会抱怨。可能有更好的方法,但老实说,这不是我经常做的事情,而且这个方法有效。
请注意,您还需要在模块类定义上方包含以下内容:
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;