Magento 2 – Create Product attribute programmatically without InstallData

We need to create a script that creates product attributes. Product attribute comes from third-party API when creating a product its check product attribute available or not If that is not available then it creates that time product attributes.
Thank you!

process – How to programmatically start new screens

I used to use a supervisor to start multiple processes and it worked just fine. But something has change and now my process doesn’t work well if I start it using supervisor but surprisingly works fine when I manually start the process via the screen.

Process is something like: php artisan my:command.

Is there any way to start multiple screens and then start process inside that screen and then detach.
Steps would include:

  • screen -S mycommand{index}
  • php artisan my:command
  • detach from screen

8 – How to get a view result programmatically without exposed filters respected

I have some views of display page which I do not want to show to public if there would not be any result when accessed without any exposed filters set. I have a custom views access plugin and a custom access checking class which can handle the access granting.

My idea so far is that I would like to run the view a first time during access checking without exposed filters. In this way I could find out if the view would ever deliver any results. I tried it in the following way:

  $view = Views::getView($view_id);
  $view->setDisplay($display_id);
  $view->setArguments(($politician_id));
  $view->execute();
  $result = $view->total_rows;

The problem is: as soon as an user comes to this page with query parameters in the url set by exposed filters these exposed filters are used for this view query as well. I did not find any way to tell the class in $view to execute without exposed filters. I even tried to set these exposed filters data in the following ways but even then the filter data in the query parameters is still used to filter the result set:

  $view = Views::getView($view_id);
  $view->setDisplay($display_id);
  $view->setArguments(($politician_id));
  $view->initDisplay();
  $view->initHandlers();
  $view->setExposedInput(());
  $view->exposed_data = ();
  $view->exposed_raw_input = ();
  $view->execute();
  $result = $view->total_rows;

I want to achieve that in this way because I want to use this logic for several views. It would be so smart to simply re-use the arguments and filters set in the views instead of hardcoding a new custom database query as the settings in the views might change. I hope someone can help me.

php – Magento 2, programmatically create configurable products

Hello I have the necessity of creating both simpe and configurable products.

This is what (following online guidelines) I have right now:

$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$salt = rand(10, 999);

//REGARDING SIMPLE PRODUCTS
$simple_product = $objectManager->create('MagentoCatalogModelProduct');
$simple_product->setSku('test-simple' . $salt);
$simple_product->setName('test name simple' . $salt);
$simple_product->setAttributeSetId(9); //my own attribute set 

$simple_product->setData('157', '9'); // my custom attribute flavor and it's assigned value

$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(14)); //it's category
$simple_product->setStockData(array(
    'use_config_manage_stock' => 0, //'Use config settings'?
    'manage_stock' => 1, //manage stock
    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
    'is_in_stock' => 1, //Stock Availability
    'qty' => 100 //qty
    )
);
$simple_product->save();

$productRepository = $objectManager->get('MagentoCatalogApiProductRepositoryInterface');
$product = $productRepository->getById($simple_product->getId());
$product->setFlavor('BBBB');  //my custom attribute is called flavor and BBBB it's is value
$product->getResource()->saveAttribute($product, 'flavor');

$simple_product_id = $simple_product->getId(); //get ID
echo "simple product id: ".$simple_product_id."n";

This works just perfect, now I’d like to create a configurable product and assign this product to it:

//REGARDING CONFIGURABLE PRODUCTS
$configurable_product = $objectManager->create('MagentoCatalogModelProduct');
$configurable_product->setSku('test-configurable' . ++$salt);
$configurable_product->setName('test name configurable' . ++$salt);
$configurable_product->setAttributeSetId(9);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(14));
$configurable_product->setStockData(array(
    'use_config_manage_stock' => 0, 
    'manage_stock' => 1, 
    'is_in_stock' => 1, 
    'qty' => 999
    )
);

$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(157),$configurable_product); //attribute ID of attribute flavor

$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);

$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);

$configurableProductsData = array();
$configurableProductsData($simple_product_id) = array( //our simple product ID
    '0' => array(
        'label' => 'gusto', //attribute label
        'attribute_id' => '157', //attribute ID of attribute 'flavor' in my store
        'value_index' => '9', //value of 'BBBB' index of the attribute 
        'is_percent'    => 0,
        'pricing_value' => '10',
    )
);


$configurable_product->setConfigurableProductsData($configurableProductsData);

$configurable_product->save();

echo "configurable product id: ".$configurable_product->getId()."n";

I successifully create both products, but the simple is not associated with the configurable one.

Of course I do something wrong…. can you please guide me in the right direction?

theming – how to programmatically display a view embedded in code with a template

I have a page I’m generating in a module. I’m using this code to get a block from a view:

$block = views_embed_view('testimonials', 'block_1');
$view = Drupal::service('renderer')->render($block);
$my_block = $view->__toString();

and display it in the generated HTML:

<div class='testimonials'>$my_block</div>

When I view the page in a browser and inspect it (with twig.debug=true), I don’t see the output rendered by a block template. If I display the block in a region by using the block layout page, I do see there is twig involved.

What do I have to do to make it use a template when I create the block in code? I think the correct answer would be to create a twig template for the entire page, and pull the block into the page template. But I’m curious if it could also be done this way.

8 – How to get alter value of a views field programmatically

I’ve developed a Views field plugin that extracts the value from another field in the view in order to apply some math. The plugin works great at getting the original value of the field. However, I would like users to be able to get the rewritten value of a given field. I’ve been searching around, but can’t seem to find any documentation on this.

With the following code I can get the value of another field:

public function getValue(ResultRow $values, $field = NULL) {
  parent::getValue($values, $field);

  $field = $this->options('data_field');
  $value = $values->_entity->get($field)->value;

  // I have some logic here that's not necessary for this question.

  return $value;
}

What would I need to do within the above function to get the value of $field if $field had been rewritten via someone checking the “Override the output of this field with custom text” checkbox?

How can I programmatically check if a 12 word mnemonic is valid?

I would like to use python to check if a 12 word mnemonic is valid. How can this be done?

c# – Binding Context Menu MenuItem.IsChecked to custom class bool property programmatically

I’m having trouble binding MenuItem.IsChecked to a boolean outside of XAML.

My intention is this:

MenuItem leftside = new MenuItem
{
    Header = "Left Side", 
    IsCheckable = true,
    IsChecked = "{Binding leftSide}"
};

And for the bool:

public bool leftSide
{
    get
    {
        return shape.LeftSideShown;
    }
    set
    {
        shape.LeftSideShown = value;
        RaisePropertyChangedEvent("leftSide");
    }
}

I’ve tried variations of leftside.SetBinding(), but all I can get is a check/uncheck without triggering a change of the boolean. What does a programmatic binding to boolean look like?

How to Add Products to Multiple Categories Programmatically in Magento?

I am using Magento 1.4.0.1. I have over 21000 simple products, each entered into a single category. There are hundreds of categories on my site. Some products belong in multiple categories. Is there some way for me to programmatically add products into multiple categories?