gpt4 book ai didi

php - Prestashop:将定制产品添加到购物车

转载 作者:可可西里 更新时间:2023-11-01 00:32:15 25 4
gpt4 key购买 nike

我正在为 Prestashop 编写自定义 Controller 。它应该做一个简单的任务:1.如果没有创建一个新的购物车(工作正常)2. 从数据库中获取属性 ID(工作正常)3.分配自定义(一个文本字段)4. 将此产品添加到购物车。

我当前的代码:

$idProduct = 1; // for me it's always one
$qty= 1; // always add one item
$text = (string)Tools::getValue('textField9'); // string of text customization
$attribute = (int)Tools::getValue('sel_combination'); // atribute combination ID

// get cart id if exists
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}

// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart();
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
$cart->update();
}



// get product to add into cart
$productToAdd = new Product(1, true, (int)($this->context->cookie->id_lang));

// default attributes
// $default_id_product_attribute = Product::getDefaultAttribute($productToAdd->id, $minimumQuantity);

// assign real attributes
$attributes = $attribute;
$cart->update();
// add customizatated text
$customization = $this->context->cart->addTextFieldToProduct((int)($idProduct), 9, Product::CUSTOMIZE_TEXTFIELD, $text );

$exising_customization = Db::getInstance()->executeS('SELECT id_customization FROM '._DB_PREFIX_.'customized_data ORDER BY id_customization DESC LIMIT 0,1');


$customization = $exising_customization[0]['id_customization'];



Db::getInstance()->execute('UPDATE ps_customization SET in_cart = 1, id_product_attribute = '.$attributes.' WHERE id_customization = ' .$customization);

// add product to cart
$cart->updateQty($qty, (int)($idProduct), (int)($attributes), (int)($customization), Tools::getValue('op', 'up'));
$prods = $cart->getProducts(true);
print_r ($prods[0]['id_customization']);
// update cart
$cart->update();

购物车已创建并向其中添加了产品。购物车显示,只有一种产品 - 但未显示。重新加载上述代码后,购物车会显示 2 个具有适当自定义数据的项目。

据我所知,问题出在定制上。第一次未分配($cart->getProducts(true); 显示 ['id_customization'] 为空),但第二次一切正常。即使是第一个产品也是固定的。

最佳答案

我设法解决了这个问题。

  1. 创建购物车的正确方法:

    if (!$this->context->cart->id)
    {
    if (Context::getContext()->cookie->id_guest)
    {
    $guest = new Guest(Context::getContext()->cookie->id_guest);
    $this->context->cart->mobile_theme = $guest->mobile_theme;
    }
    $this->context->cart->add();
    if ($this->context->cart->id)
    $this->context->cookie->id_cart = (int)$this->context->cart->id;
    }
  2. 添加自定义

    $this->product = new Product(1, true, (int)($this->context->cookie->id_lang));

    $authorized_text_fields = array();

    if (!$field_ids = $this->product->getCustomizationFieldIds())
    return false;

    foreach ($field_ids as $field_id)
    if ($field_id['type'] == Product::CUSTOMIZE_TEXTFIELD)
    $authorized_text_fields[(int)$field_id['id_customization_field']] = 'textField'.(int)$field_id['id_customization_field'];

    $indexes = array_flip($authorized_text_fields);
    foreach ($_POST as $field_name => $value)
    if (in_array($field_name, $authorized_text_fields) && !empty($value))
    {
    if (!Validate::isMessage($value))
    $this->errors[] = Tools::displayError('Invalid message');
    else
    $this->context->cart->addTextFieldToProduct($this->product->id, $indexes[$field_name], Product::CUSTOMIZE_TEXTFIELD, $value);
    }
    else if (in_array($field_name, $authorized_text_fields) && empty($value))
    $this->context->cart->deleteCustomizationToProduct((int)$this->product->id, $indexes[$field_name]);
  3. 获取定制

    $texts = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_TEXTFIELD, true);
    $text_fields = array();
    foreach ($texts as $text_field)
    $text_fields['textFields_'.$this->product->id.'_'.$text_field['index']] = str_replace('<br />', "\n", $text_field['value']);

关于php - Prestashop:将定制产品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24610116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com