opencartopencart-3opencart-module

Product is listed on database is not listed in Admin > Products List


As an Opencart newbie, I'm adding the product with a static array just for testing the addProduct function but I could not get it to work.

Static array:

    $data['pid'] = $this->model_catalog_product->addProduct([
        'product_description' => [['name' => 'bakbuişte', 'description' => '', 'meta_title' => 'metaraba', 'meta_description' => '', 'meta_keyword' => '', 'tag' => '']],
        'model' => 'amabigun',
        'sku' => '',
        'upc' => '',
        'ean' => '',
        'jan' => '',
        'isbn' => '',
        'mpn' => '',
        'location' => '',
        'price' => '',
        'tax_class_id' => '0',
        'quantity' => '1',
        'minimum' => '1',
        'subtract' => '1',
        'stock_status_id' => '6',
        'shipping' => '1',
        'date_available' => '2019-12-31',
        'length' => '',
        'width' => '',
        'height' => '',
        'length_class_id' => '1',
        'weight' => '',
        'weight_class_id' => '1',
        'status' => '1',
        'sort_order' => '1',
        'manufacturer' => '',
        'manufacturer_id' => '',
        'category' => '',
        'filter' => '',
        'product_store' => array(0 => '0',),
        'download' => '',
        'related' => '',
        'option' => '',
        'image' => '',
        'points' => '',
        'product_reward' => array(1 => array('points' => '',),),
        'product_seo_url' => array(0 => array(1 => '',),),
        'product_layout' => array(0 => '',)
    ]);

After this block, I see the entry in oc_product table just like others but not in Admin > Catalog > Products list. Is there another little stuff that I should prepare?


Solution

  • Take a look at the addProduct() function and you will see some code which performs inserts to the description table like this:

    foreach ($data['product_description'] as $language_id => $value) {
        ...
    }
    

    According to that logic, the array key is the language_id. In your array you have:

    'product_description' => [['name' => 'bakbuişte', 'description' => '', 'meta_title' => 'metaraba', 'meta_description' => '', 'meta_keyword' => '', 'tag' => '']],
    

    Since there is no array key defined, your description is assigned to key 0. Do you have a language_id of 0 in your language table? Probably not. A typical (default) opencart installation has one language with language_id of 1. With that in mind, your array should look like this:

    'product_description' => [1 => ['name' => 'bakbuişte', 'description' => '', 'meta_title' => 'metaraba', 'meta_description' => '', 'meta_keyword' => '', 'tag' => '']],
    

    The reason that things to fail when you write description rows with the wrong language_id is that the product list is retrieved with a query like this:

    SELECT *
    FROM product p
        LEFT JOIN product_description pd ON (p.product_id = pd.product_id)
    WHERE pd.language_id = '1';
    

    Notice the where condition at the end? If your description table is missing the corresponding row with language_id 1, the query will ignore your product row.