0

I have 3 languages in my site. I need to add different titles based on language. If I add through back end it will take much time (more than 300 values I have to enter for each product). If I get the store id I can add it through the script. But I am not getting the store ID. Can anyone help to get this?

Here is my code:

<?php 
require dirname(FILE) . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
ini_set("display_errors", "1");
error_reporting(E_ALL);
header('Content-type: text/html; charset=UTF-8');
class customOptions extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{ 
    public function launch() 
    { 
        state = $this->_objectManager->get('\Magento\Framework\App\State'); //instance of state 
        $state->setAreaCode('frontend'); 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of Object manager 
        $productId = 17; 
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId); 
        $file = fopen("test.csv","r");
        $valuesArray = fgetcsv($file); 
        for($i =0;$i<count($valuesArray) ;$i++)
        { 
            $values = [ 'record_id'=>$i, 'title'=>$valuesArray[$i], 
            'price'=>0, 'sku'=>$valuesArray[$i], 'price_type'=>"fixed", 
            'sort_order'=>$i, 'is_delete'=>0 ]; $finalArray[] =$values;
        } 
        fclose($file); 
        foreach ($product->getOptions() as $custom_option)
        { 
            if ('test' == $custom_option->getTitle())
            {
                $custom_option_values = $custom_option->getValues();
                $custom_option->setValues($finalArray); $product->save();
            }
        } 
        return $this->_response;
    }
}

Thanks in advance.

  • 1
    please upload your code which you used. – Rohan Hapani Mar 20 at 9:00
  • <?php require dirname(FILE) . '/../../app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); ini_set("display_errors", "1"); error_reporting(E_ALL); header('Content-type: text/html; charset=UTF-8'); – Dilip Mar 20 at 9:13
  • class customOptions extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface { public function launch() { $state = $this->_objectManager->get('\Magento\Framework\App\State'); //instance of state $state->setAreaCode('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of Object manager $productId = 17; $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId); $file = fopen("test.csv","r"); – Dilip Mar 20 at 9:13
  • $valuesArray = fgetcsv($file); for($i =0;$i<count($valuesArray) ;$i++){ $values = [ 'record_id'=>$i, 'title'=>$valuesArray[$i], 'price'=>0, 'sku'=>$valuesArray[$i], 'price_type'=>"fixed", 'sort_order'=>$i, 'is_delete'=>0 ]; $finalArray[] =$values; – Dilip Mar 20 at 9:14
  • 1
    You can edit your question and upload your code :) – Rohan Hapani Mar 20 at 9:22
0

You can use this below code :

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeID       = $storeManager->getStore()->getStoreId();
  • Hi I am setting the store id by $product = $objectManager->create('\Magento\Catalog\Model\Product')->setStoreId(6)->load($productId); But still entries are creating in all stores. – Dilip Mar 21 at 12:54
  • Try to use this code : $product = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId,false,$storeId); – Rohan Hapani Mar 21 at 13:00

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.