在 prestashop 1.7 中创建产品组合以编程方式重复 id 问题

Create product combinations in prestashop 1.7 programmatically duplicate id issue

我正在尝试添加带有 php 代码组合的产品。当 运行 脚本在 ps_product_attribute_combination table 中发生重复主 ID 错误时会怎样?我正在使用来自颜色和尺寸组的多个属性。此代码放在 $product->save() 调用之后。

        $combinationAttributes[] = array(1,2);
        $combinationAttributes[] = array(5,6,7);
        

        if(!$product->productAttributeExists($combinationAttributes,false, null, true, true)){
            $price = $price;
            $weight = '';
            $ecotax = '';
            $unit_price_impact="";
            $quantity = 100;
            $reference = $reference;
            $supplier_reference = "";
            $ean13 = "";

            
            $default = '0';


            $idProductAttribute = $product->addProductAttribute((float)$price, (float)$weight, $unit_price_impact, (float)$ecotax, (int)$quantity, "", strval($reference), strval($supplier_reference), strval($ean13), $default, NULL, NULL,'','','','');
    
            
            $product->addAttributeCombinaison($idProductAttribute, $combinationAttributes);

以编程方式插入产品可能会很痛苦。我使用 Prestashop 默认值的一部分和使用 sql.

插入值的一部分编写了自己的代码

希望这段代码对您的进步有所帮助。您可以将此文件添加到您的管理文件夹,然后 运行 它。

独立PHP文件

<?php
if ( !defined( '_PS_ADMIN_DIR_' ) ) {
    define( '_PS_ADMIN_DIR_', getcwd() );
}
include( _PS_ADMIN_DIR_ . '/../config/config.inc.php' );

addProduct(
    '1234567891234',                         // Product EAN13
    'Whosebug',                         // Product reference
    'Crezzur',                               // Product name
    5,                                       // Product quantity
    'Code by Crezzur (https://crezzur.com)', // Product description
    array(                                   // Product features (array)
        array( "name" => "Color", "value" => "Red" ),
        array( "name" => "Height", "value" => "200cm" ),
    ),
    '999.95',                                // Product price
    'https://i.imgur.com/yOp1xt3.jpg',       // Product image
    1,                                       // Product default category
    array(1, 5)                              // All categorys for product (array)
);

function addProduct( $ean13, $ref, $name, $qty, $text, $features, $price, $imgUrl, $catDef, $catAll ) {
    $product = new Product();              // Create new product in prestashop
    $product->ean13 = $ean13;
    $product->reference = $ref;
    $product->name = createMultiLangField( $name );
    $product->description = htmlspecialchars($text);
    //$product->link_rewrite = createMultiLangField($linkrewrite);
    $product->id_category_default = $catDef;
    $product->redirect_type = '301';
    $product->price = $price;
    $product->quantity = $qty;
    $product->minimal_quantity = 1;
    $product->show_price = 1;
    $product->on_sale = 0;
    $product->online_only = 0;
    $product->meta_description = '';
    $product->add();
    $product->addToCategories($catAll);

    // Insert "feature name" and "feature value"
    if ( is_array( $features ) ) {
        foreach ( $features as $feature ) {
            $attributeName = $feature[ 'name' ];
            $attributeValue = $feature[ 'value' ];

            // 1. Check if 'feature name' exist already in database
            $FeatureNameId = Db::getInstance()->getValue( 'SELECT id_feature FROM ' . _DB_PREFIX_ . 'feature_lang WHERE name = "' . pSQL( $attributeName ) . '"' );
            // If 'feature name' does not exist, insert new.
            if ( empty( $getFeatureName ) ) {
                Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature` (`id_feature`,`position`) VALUES (0, 0)' );
                $FeatureNameId = Db::getInstance()->Insert_ID(); // Get id of "feature name" for insert in product
                Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature_shop` (`id_feature`,`id_shop`) VALUES (' . $FeatureNameId . ', 1)' );
                Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature_lang` (`id_feature`,`id_lang`, `name`) VALUES (' . $FeatureNameId . ', ' . Context::getContext()->language->id . ', "' . pSQL( $attributeName ) . '")' );
            }

            // 1. Check if 'feature value name' exist already in database
            $FeatureValueId = Db::getInstance()->getValue( 'SELECT id_feature_value FROM webshop_feature_value WHERE id_feature_value IN (SELECT id_feature_value FROM webshop_feature_value_lang WHERE value = "' . pSQL( $attributeValue ) . '") AND id_feature = ' . $FeatureNameId );
            // If 'feature value name' does not exist, insert new.
            if ( empty( $FeatureValueId ) ) {
                Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature_value` (`id_feature_value`,`id_feature`,`custom`) VALUES (0, ' . $FeatureNameId . ', 0)' );
                $FeatureValueId = Db::getInstance()->Insert_ID();
                Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature_value_lang` (`id_feature_value`,`id_lang`,`value`) VALUES (' . $FeatureValueId . ', ' . Context::getContext()->language->id . ', "' . pSQL( $attributeValue ) . '")' );
            }
            Db::getInstance()->execute( 'INSERT INTO `' . _DB_PREFIX_ . 'feature_product` (`id_feature`, `id_product`, `id_feature_value`) VALUES (' . $FeatureNameId . ', ' . $product->id . ', ' . $FeatureValueId . ')' );
        }
    }

    $shops = Shop::getShops( true, null, true );
    $image = new Image();
    $image->id_product = $product->id;
    $image->position = Image::getHighestPosition( $product->id ) + 1;
    $image->cover = true;
    if ( ( $image->validateFields( false, true ) ) === true && ( $image->validateFieldsLang( false, true ) ) === true && $image->add() ) {
        $image->associateTo( $shops );
        if ( !uploadImage( $product->id, $image->id, $imgUrl ) ) {
            $image->delete();
        }
    }
    echo 'Product added successfully (ID: ' . $product->id . ')';
}

function uploadImage( $id_entity, $id_image = null, $imgUrl ) {
    $tmpfile = tempnam( _PS_TMP_IMG_DIR_, 'ps_import' );
    $watermark_types = explode( ',', Configuration::get( 'WATERMARK_TYPES' ) );
    $image_obj = new Image( $id_image );
    $path = $image_obj->getPathForCreation();
    $imgUrl = str_replace( ' ', '%20', trim( $imgUrl ) );
    // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
    if ( !ImageManager::checkImageMemoryLimit( $imgUrl ) )
        return false;
    if ( @copy( $imgUrl, $tmpfile ) ) {
        ImageManager::resize( $tmpfile, $path . '.jpg' );
        $images_types = ImageType::getImagesTypes( 'products' );
        foreach ( $images_types as $image_type ) {
            ImageManager::resize( $tmpfile, $path . '-' . stripslashes( $image_type[ 'name' ] ) . '.jpg', $image_type[ 'width' ], $image_type[ 'height' ] );
            if ( in_array( $image_type[ 'id_image_type' ], $watermark_types ) ) {
                Hook::exec( 'actionWatermark', array( 'id_image' => $id_image, 'id_product' => $id_entity ) );
            }
        }
    } else {
        unlink( $tmpfile );
        return false;
    }
    unlink( $tmpfile );
    return true;
}

function createMultiLangField( $field ) {
    $res = array();
    foreach ( Language::getIDs( false ) as $id_lang ) {
        $res[ $id_lang ] = $field;
    }
    return $res;
}

在代码部分部分产品功能插入中存在一些错误 Crezzur 建议检查 getfeaturename 变量是否存在必须是 featurenameid 和查询检查 'feature value name' 是否已存在于数据库中是不正确的。这是我的修复 // 插入“特征名称”和“特征值”

if (is_array($features)) {
    foreach ($features as $feature) {
        $attributeName = $feature['name'];
        $attributeValue = $feature['value'];

        // 1. Check if 'feature name' exist already in database
        $FeatureNameId = Db::getInstance()->getValue('SELECT id_feature FROM ' . _DB_PREFIX_ . 'feature_lang WHERE name = "' . pSQL($attributeName) . '"');

        // If 'feature name' does not exist, insert new.
        if (empty($FeatureNameId)) {
            Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature` (`id_feature`,`position`) VALUES (0, 0)');
            $FeatureNameId = Db::getInstance()->Insert_ID(); // Get id of "feature name" for insert in product
            Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature_shop` (`id_feature`,`id_shop`) VALUES (' . $FeatureNameId . ', 1)');
            Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature_lang` (`id_feature`,`id_lang`, `name`) VALUES (' . $FeatureNameId . ', ' . Context::getContext()->language->id . ', "' . pSQL($attributeName) . '")');
        }

        // 1. Check if 'feature value name' exist already in database
        $FeatureValueId = Db::getInstance()->getValue('SELECT id_feature_value FROM ' . _DB_PREFIX_ . 'feature_value WHERE id_feature_value IN (SELECT id_feature_value FROM ' . _DB_PREFIX_ . 'feature_value_lang WHERE value = "' . pSQL($attributeValue) . '") AND id_feature = ' . $FeatureNameId);
        //var_dump($FeatureValueId);
        // If 'feature value name' does not exist, insert new.
        if (empty($FeatureValueId)) {
            Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature_value` (`id_feature_value`,`id_feature`,`custom`) VALUES (0, ' . $FeatureNameId . ', 0)');
            $FeatureValueId = Db::getInstance()->Insert_ID();
            Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature_value_lang` (`id_feature_value`,`id_lang`,`value`) VALUES (' . $FeatureValueId . ', ' . Context::getContext()->language->id . ', "' . pSQL($attributeValue) . '")');

        }
        Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'feature_product` (`id_feature`, `id_product`, `id_feature_value`) VALUES (' . $FeatureNameId . ', ' . $product->id . ', ' . $FeatureValueId . ')');
    }
}  The other part of the code provided by Crezzur is working and tested 10x Crezzur