Questions tagged [programmatically]

Denotes questions related to making changes to the Magento platform programmatically, rather than via the admin interface.

Filter by
Sorted by
Tagged with
22
votes
1answer
13k views

How to add a foreign key to a table programmatically?

I am trying to add a set of new tables and I'm trying to add a foreign key to link these tables. I have looked online to try and find some information about addForeignKey() but cannot find anything. ...
21
votes
4answers
31k views

Programmatically create a simple product in magento 2

I'd like to create simple product programmatically in magento 2. Is there any way to create ?
15
votes
4answers
4k views

Adding multiple items with different attributes to cart programmatically

I'm making a bulk add to cart system. Please note : i want it to works for simple products with with custom options -> Where custom option are like color(red , green , blue) or Size(Xl, M , S) ...
13
votes
3answers
9k views

How to add a CMS block programmatically in Magento 2?

I need to add a CMS block via an install/upgrade script. I've already figured out how to add "normal" CMS pages as seen in the script below. But since I can't find any way to add CMS blocks in Magento ...
11
votes
2answers
2k views

Magento create order programmatically with paypal payment method

Currently we have two magento independent websites. One is behaving as storefront and other as back end. User can place the order at front end. So same order I am trying to create programmatically ...
8
votes
8answers
18k views

Magento 2: Programmatically Updating Inventory

In the following code, I am able to store all the information with the exclusion of the stock data. Has something changed in Magento 2? public function __construct( ScopeConfigInterface $...
8
votes
4answers
4k views

Create an admin user programmatically in Magento 2.0

It is simple to add new admin user in Magento 1.9 <?php require_once('app/Mage.php'); umask(0); Mage::app(); $user = Mage::getModel('admin/user') ->setData(array( 'username' =>...
8
votes
1answer
12k views

Magento2 - How to add a product into cart programatically when checkout_cart_product_add_after is fired

Consider there are two product ie: Product A and Product B. Product B is a virual product which i need to add to cart when Product A is added to it. To do so, I am trying to add Product B to cart by ...
7
votes
2answers
2k views

How to get disabled products in Magento 2

Is there a way to get disabled products programmatically in Magento 2 ?
6
votes
2answers
6k views

Magento 2 - Change customer password programmatically

I have build a plugin on the authenticate() method in Magento\Customer\Model\AccountManagement with beforeAuthenticate(). It is working and I can change the firstname of something else of the ...
6
votes
1answer
9k views

Get magento shipping amount from each ordered item

I am trying to get shipping and handling amount. I tried the below snippet code but its not working. echo $item->getShippingAmount(); but if i try with echo $order->getShippingAmount(); It ...
5
votes
3answers
4k views

Create a bundle product programmatically - Magento 2

I am trying to create a bundle product with Magento 2. But somehow its not linking simple products with the bundle product (i.e. its not saving bundle options & data, also linking same with ...
5
votes
4answers
7k views

Magento 2 Programatically add product to cart with additional options

I want to add product to cart with some additional data. My code adds the product to cart,but the additional info is not saved properly. I want to display the additional content in cart like custom ...
5
votes
2answers
865 views

How to add new item (product) into existing order programmatically in Magento 2

I have created one virtual product in my Magento2 website. Now, I want to add it programmatically to the existing customer orders on some occasions with its custom price and name. Can someone please ...
5
votes
2answers
2k views

Problem with addWebsiteFilter and addStoreFilter

I am working on my product collection query. I used "addWebsiteFilter" and "addStoreFilter" in my collection query. The problem is addWebsiteFilter supports array of website ids but addStoreFilter not ...
5
votes
0answers
709 views

Override tax rate on programmatically created order

I'm building an extension which imports orders created on a third-party system into Magento. The third-party system should be the single source of truth for the order's prices, tax rate, tax paid, etc....
4
votes
3answers
3k views

set related/upsell products programatically only works for 1 product

I am trying to update products programatically and I'm having issues with the related/upsell section. It updates only with the last product sku. So if I am trying to link 3 products with a parent ...
4
votes
3answers
8k views

Magento 2 how to add product attribute of type textarea with editor programmatically

How can we add custom attribute using setup script in magento 2 attribute type should be textarea with editor. Like Product description.
4
votes
2answers
4k views

How to retrieve custom attribute set ID by name

I have created couple of custom Attribute Set and now I want to assign a group in those attribute sets programmatically, so is there any way to retrieve attribute set IDs based on name?
4
votes
3answers
4k views

Magento 2: How to add customer address programmatically?

I have succeed create customer programmatically. Now I want to add customer address to the customer, how can i do this programmatically?
4
votes
5answers
10k views

Magento 2 - Create product programmatically with different store view values

I have created this custom import module that will create products and set different values for each store view. The problem is, when I save a single attribute for the product on a store view, all the ...
4
votes
3answers
2k views

How to programmatically add custom options to all products without duplication?

I have over 40,000 products in store and am trying to add custom options to all. The script below works fine, but stops after about 1000 products, then starts again.. making duplicates rather than ...
4
votes
2answers
4k views

How to create store view or store programmatically and do it right in Mage 2?

I want to create store or store view programmatically and I use the followng code (for store view): $storeView = $objectManager->create(\Magento\Store\Model\Store::class); // Store - is a ...
4
votes
4answers
5k views

Magento 2 : How to clear mini cart programatically

I am using this solution and get referenece from Here. But It dose not update mincart after page refresh. Although it deletes items from quote_item table. I am also trying deleting quote id from ...
4
votes
4answers
1k views

Import many products programatically

After getting data with curl I create new product and save it: $product = Mage::getModel('catalog/product'); try{ $product ->setSku('testsku1252') ... ->setPrice(99.99)...
4
votes
1answer
129 views

How to set cms block to category in Magento 2 programmatically?

I would like to add CMS block to a category programmatically, how to achieve it?
4
votes
1answer
5k views

Programmatically adding images to products

I am trying to create a system which automatically creates a configurable product and creates and associates simple products to it. It is all working well but a lot of the simple products will be ...
4
votes
5answers
3k views

How to add bundle product to cart programmatically with custom price and quantity (of all products) in Magento 2?

I am struggling with Bundle Product add to cart programmatically in Magento 2. Here I need to add all products with custom price and quantity and I want to do this operation in Product list.phtml ...
4
votes
3answers
2k views

Create simple product programatically in magento2

I have created simple product using following code.almost its working fine, but I'm not able assign product to websites. Actually I have created two websites. I want to assign product to particular ...
4
votes
0answers
75 views

Apply unique images by attribute to each sku magento 2 programmatically

How can I perform this task through a script? I already relate the configurations to the product with this segment $attributeModel = $this->objectManager->create('Magento\ConfigurableProduct\...
4
votes
1answer
197 views

How add configurable product for selected options magento 2 programatically?

I want to add configurable product pragmatically. If product is ABC it has 3 size options XS M L so I want to add all options same time only changeable thing is quantity. I tried but it added as first ...
4
votes
1answer
2k views

Create order programmatically for the items on cart on checkout page

I am successfully creating the order in the Magento dashboard with the below code. //for order $id=3; // get Customer Id $customer = Mage::getModel('customer/customer')->load($id); $transaction = ...
3
votes
1answer
13k views

magento 2 create categories programmatically [duplicate]

I would like to create categories programmatically in magento 2. Please suggest me proper code for it. I have try this code Link but not working properly . I have to create number of categories on ...
3
votes
1answer
4k views

How to add programmatically product to wishlist in Magento 2?

I want to add the product to the wishlist in magento2. What is the way to programmatically add the product into wishlist in Magento2?
3
votes
3answers
5k views

Programmatically duplicated product not getting displayed in frontend

I use the following script (inside a controller - action for now) for duplicating a product programatically. public function createAction(){ $data = $this->getRequest()->getParams(); $...
3
votes
1answer
647 views

Difference between catalog_product_prepare_save and catalog_product_save_before

What is the difference between catalog_product_prepare_save and catalog_product_save_before? I have an observer that updates some attributes when a product is saved. It works great when I save a ...
3
votes
1answer
2k views

Catch errors from validation when saving product or category ($product->validate() or $category->validate())

I would like to validate the data before saving a product programatically because ->save() saves a product even if it doesn't have an sku. If I do ->validate() I get the expected error: [...
3
votes
2answers
2k views

How to disable/enable a product for admin store programatically in Magento2?

I want to disable product for admin store means product should be disable for all store/website. I am trying to do something like this but product disable for default store only: $productRepository =...
3
votes
2answers
3k views

How to search product programatically in magento 2?

I want to search product in custom page so how search products programmatically in custom controller ? Below Magento 1.x search programmatically $queryText = Mage::helper('catalogSearch')->...
3
votes
3answers
1k views

Increasing all product prices by percentage | Custom Script

I am trying to find a way to update all product prices by 5% then I want to revert them to the original price and wondered if anyone had any ideas of the best way to achieve this. At the moment I am ...
3
votes
1answer
5k views

Update Custom Attribute Value Programatically

I have created a script that tests importing products programmatically into Magento. Unfortunately, I'm having trouble updating a product's custom attribute value - in this case "manufacturer." When I ...
3
votes
1answer
1k views

How to use getStoreCategories function [duplicate]

I am working on category collection, i find this link. Here $helper = Mage::helper('catalog/category'); // sorted by name, fetched as collection $categoriesCollection = $helper->...
3
votes
2answers
896 views

Show customer data after setCustomerAsLoggedIn()

Im experiencing an issue with Magento 2.1.7 I want to login users programmatically by only their email. But after setCustomerAsLoggedIn it still shows sign in or create an account in the header. ...
3
votes
3answers
5k views

Magento2 how to add configurable product to cart programmatically [duplicate]

Magento2 how to add the configurable product to cart programmatically. I searched but found question related to how add simple product mostly.
3
votes
1answer
777 views

Programmatically create a Smart Category Rules

I was following this guide from inchoo for programmatically creating 50+ categories, but then I discovered this Enterprise feature, “Smart Category Rules”, and would like to use this approach instead. ...
3
votes
2answers
2k views

Add to cart the same product with different configurations

I'm working on a Magento product configurator. In one page, you are able to configurate multiple file, then add to cart. Let's say, we want to add to cart the same product twice but with 2 different ...
3
votes
1answer
462 views

How to create sortable array (draggable) in configuration

I would like to create an array in a custom module configuration where I can manually order the lines thanks to drag & drop. I can see there is a similar behavior in the related products when ...
3
votes
2answers
2k views

Programatically create credit memo

I am working on script and want to know about how magento credit memo created. My requirement is same as magento after update quantities of product in order, hit update order button. Then enter custom ...
3
votes
1answer
880 views

Create invoice again which was deleted programmatically

I had deleted an invoice programmatically by using the following code: $invoice = Mage::getModel('sales/order_invoice')->load(97); $invoice->delete(); Now I need Invoice Button to invoice ...
3
votes
3answers
74 views

Remove existing and assign new categories to existing product while updating it programmatically in magento 2

How can I remove existing and assign new categories to product programmatically in magento 2 while updating product data. <?php $newCategoryIds = array(5,6); $existingProductId = 25; $...