0

I have created a script for import CSV products everything's working perfect but one issue is product video url not set.

<?php
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');

// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');

// get date 
$today_date = date("m/d/Y");
$added_date = date('m/d/Y',strtotime("+17 day"));

$set_product = $object_Manager->create('\Magento\Catalog\Model\Product');

try{
    $set_product->setWebsiteIds(array(1));
    $set_product->setAttributeSetId(4);
    $set_product->setTypeId('simple');
    $set_product->setCreatedAt(strtotime('now')); 
    $set_product->setName('Test Sample Products'); 
    $set_product->setSku('add-sku-1');
    $set_product->setWeight(1.0000);
    $set_product->setStatus(1);
    $category_id= array('3,4,5');
    $set_product->setCategoryIds($category_id); 
    $set_product->setTaxClassId(0); 
    $set_product->setPrice(100.99) ;

    $set_product->setStockData(
        array(
        'use_config_manage_stock' => 0, 
        // checkbox for 'Use config settings' 
        'manage_stock' => 1, // manage stock
        'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed 
        'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
        'is_in_stock' => 1, // Stock Availability of product
        'qty' => 100 // qty of product
        )
    );

     $set_product->save();
    // get id of product
    $get_product_id = $set_product->getId();
    echo "Upload simple product id :: ".$get_product_id."\n";
}
catch (\Exception $e) {
    echo $e->getMessage();
}

$set_product->setVideoUrl('youtub.com/magento');

But it's not working category save and set product video URL?

2

Please remove single quotes from category id array. So it is look like below.

$category_id = array(3,4,5);
  • Thanx @ Bhavin. you know how to set product video URL? – Magento2 Devloper Aug 25 '17 at 9:43
  • No sorry. I am still not use video URL in product. – Bhavin iFlair Aug 25 '17 at 9:50
  • ok, no problem. thanx for the solution. let me know if you found solution about video. – Magento2 Devloper Aug 25 '17 at 9:56
  • how to remove existing categories and add new categories using setCategoryIds? – jafar pinjar Nov 5 '18 at 9:21
  • @jafar When you use setCategoryIds, Magento automatically replaced existing with new one.. – Bhavin iFlair Nov 5 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.