我需要在自定义页面(不是产品页面)上有一个"添加到购物车"按钮,基本上是一个按钮,我可以传递某个产品ID并调用ajax-cart.js函数"Add "。
现在,我已经尝试了这个代码:
<a class="exclusive_small ajax_add_to_cart_button" title="{l s='Add to cart'}" onclick="ajaxCart.add(26, null, false, null, 1, null)">{l s='Add to cart'}</a>
但是,尽管它确实将具有指定ID的产品(在本例中为26)添加到购物车中,但它也触发一个带有2个错误的警报框:产品未找到和此产品已不再可用。
我已经设法找到了处理这些错误的代码,在CartController.php:
protected function processChangeProductInCart()
{
$mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';
if ($this->qty == 0)
$this->errors[] = Tools::displayError('Null quantity.', !Tools::getValue('ajax'));
elseif (!$this->id_product)
$this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));
$product = new Product($this->id_product, true, $this->context->language->id);
if (!$product->id || !$product->active)
{
$this->errors[] = Tools::displayError('This product is no longer available.', !Tools::getValue('ajax'));
return;
}
但是我不明白它是如何工作的,以及为什么当它正常工作时错误显示。
我做错了什么,这是一个bug吗?
在您的自定义页面中,首先您必须获得产品id和产品属性id,然后在单击按钮时,您必须将这些值提交给您的前端控制器(您制作的),并在该控制器中获得这些id并添加到新购物车中。您必须编写如下代码:
创建tpl文件->
abc.tpl
<input type="button" name="vss_add_to_cart" value="{l s='Add to cart' mod='your_module_name'}" class="vss_btn1" id="vss_add_to_cart" style="background:{$background|escape:'htmlall':'UTF-8'};color:{$text|escape:'htmlall':'UTF-8'};border:{$border|escape:'htmlall':'UTF-8'};"/>
现在你必须在同一个文件中为这个按钮的点击编写一些脚本:
</script>
$( "#vss_add_to_cart" ).one( "click", function() {
var id_product_attribute = document.getElementById('idCombination').value;
var id_product = document.getElementById('product_page_product_id').value;
var quantity = document.getElementById('quantity_wanted').value;
$.post(
'{$link|escape:'quotes':'UTF-8'}',
{
id: id_product,
ipa: id_product_attribute,
qty: quantity
},
function (data) {
window.location.replace(data);
}
);
});
</script>
其中{$link}是你想要获取值的地方,实际上你必须在模块文件夹中创建一个控制器并在其中创建一个前端文件夹,例如:
C:'wamp'www'prestashop'modules'oneclickcheckout'controllers'front
现在,您必须创建一个文件your_module_name.php并编写以下代码:
your_module_name.php
class OneclickcheckoutOneclickcheckoutModuleFrontController extends ModuleFrontController
{
public function initContent()
{
$qty = Tools::getValue('qty');
$id_product = Tools::getValue('id');
$id_product_attribute = Tools::getValue('ipa');
$this->context->cart->add();
if ($this->context->cart->id) {
$this->context->cookie->id_cart = (int) $this->context->cart->id;
}
$this->context->cart->updateQty($qty, $id_product, $id_product_attribute);
$link = $this->context->link->getPageLink('order');
echo $link;
die;
}
}
然后在数据库中添加一个新的购物车,并插入与该购物车相对应的产品详细信息。我希望这对你有帮助。