Which hook can I use to update programmatically a product in drupal commerce? - drupal-8

I need to programmatically update a product with drupal commerce.
Which hook can I use?
And how can I get the value of the variation? If for example I have a field in the product variation which is field_newvar?
(In drupal 7 I used
hook_node_presave($node) {
and then got the commerce product entity with entity_metadata_wrapper but now in drupal8 we don't need nodes to show products.)

Related

Sitecore users to add fields to template

I'm building a products table to show all the products attributes. However I need to allow users to add/edit/remove those attributes from the products later on in production without any dev work. I thought about branch templates but when I edit the branch template, existing products are not getting updated. Maybe I'm looking in the wrong direction? Any suggestions?
In an older project we have a similar setup with product and attributes and used a custom database to help us out:
products are stored as items in Sitecore
attributes are stored as items in Sitecore
a custom database stores the values of all attributes for all products
publish events are used to keep the custom database in sync when deleting products
on a product template we added an extra tab in the content editor including an aspx page that offers an editor on our custom database for that product. We use the products Sitecore ID and display all possible attributes in an editable way.
Most attributes have simple string values but that is extendable. Our attributes have a type (string, bool, (multi-)list, ..) and we use that to create the editor. The lists refer back to Sitecore items (a parent is selected on the attribute item).
Adding an extra tab in the editor can be done by creating a new item in the core database (/sitecore/content/Applications/Content Editor/Editors) that point towards your aspx file. In the standard values of your product template, you select the created "editor" in the Appearance section. (note that this is in Sitecore 6 - might have been changed although the path to the editors is still valid)
This solution does mean that the values of your attributes are stored outside Sitecore, but it worked for our requirements.

Sitecore 8: The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel'

I'm randomly getting the following error when I try to login to the Sitecore back end.
The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'Sitecore.ExperienceAnalytics.Client.Mvc.Presentation.ExperienceAnalyticsLineChartViewModel'.
I experience this issue after I have build the Sitecore link databases using Sitecore Launchpad --> Control Panel --> Rebuild link databases.
When I clear the Sitecore cache using http://{your website}/sitecore/admin/cache.aspx this is error is not throwing for sometime.
Is there anyway to eliminate this error permanently ?
When you Remove Broken Links on the Core database, the Model property of the ExperienceAnalyticsLineChart rendering is erroneously deleted.
Navigate to the Core database in the Content Editor of your Sitecore instance at http://{your-sitecore-url}/sitecore/shell/default.aspx?sc_content=core.
Navigate to the ExperienceAnalyticsLineChart rendering in the item tree at /sitecore/client/Applications/ExperienceAnalytics/Common/Layouts/Renderings/ExperienceAnalyticsLineChart.
Set the Model field to Sitecore.ExperienceAnalytics.Client.Mvc.Presentation.ExperienceAnalyticsLineChartViewModel, Sitecore.ExperienceAnalytics.Client.
Save the item and click OK if prompted that the item contains broken links.
Log back into Sitecore and the Launchpad will load.
In your view you're using Sitecore.ExperienceAnalytics.Client.Mvc.Presentation.ExperienceAnalyticsLineChartViewModel instead of Sitecore.Mvc.Presentation.RenderingModel witch is the default.
If you want to use your own model you have to register it in Sitecore
Navigate to /sitecore/layout/Models and add a new model in the model type add "Sitecore.ExperienceAnalytics.Client.Mvc.Presentation.ExperienceAnalyticsLineChartViewModel, YourAssemblyName"
go to your View rendering item in sitecore and select your model in the Model field.
https://community.sitecore.net/developers/f/8/t/109 looks like they have same issue like you

Bulk Creation Of Items In Sitecore

I am using Sitecore 8, Update 3.
I am attempting to bulk create a couple thousand items that use a custom template. All of these items are created under one specific parent item.
The custom template has two fields which are Path [Single-Line Text] and Target [General Link].
The source of the data is in an old SQL database.
Is there a way to do this?
The Sitecore marketplace module Data Importer is a option for you.
Create a custom importer with the sitecore api is also possible, to speed up, you can disable the index rebuild.
Since your data is already in SQL Serve, you should take a look at using the SSIS Components for Sitecore, it was designed for importing exactly this kind of data in bulk into Sitecore using ETL.
You can find more info in the blog post Creating your first project with SSIS Components for Sitecore and there is also some good additional into in the Migrating to Sitecore: Going from WordPress to Sitecore with SQL Server Integration Services (SSIS) post.
The Integration Services Components for Sitecore is available on the Sitecore Marketplace, and it is compatible with Sitecore XP8.
I'll also add, if you are creating anything more than 100 items below a single parent item then this is not recommended Sitecore practice unless the items sit within an Item Bucket. Make sure you account for this, or split your items up into smaller sub-folders (e.g. year/month folders for news article type items)
In these scenarios you normally end up writing code to call the api and create the items programmatically rather than doing a generic import.
If you need to create a large amount of items I would recommend writing a console app to do this. You could use the Sitecore Item Web api to create items outside of the Sitecore context or investigate the new Entity Service api in Sitecore, both will be capable of creating large amounts of Sitecore items.
http://mikerobbins.co.uk/2015/01/06/entityservice-sitecore-service-client/
https://sdn.sitecore.net/upload/sdn5/modules/sitecore%20item%20web%20api/sitecore_item_web_api_developer_guide_sc65-66-usletter.pdf
If these don't float your boat you could always write async code to create the Sitecore items and have that executed by an aspx page in a Sitecore instance - works well for one off tasks.
Example of Asynchronous page processing in ASP.net webforms (.NET 2.0)

Autocomplete field in Web Forms for Marketers and custom Field validator

I am using WFFM in Sitecore 7.1 , one of the requirement to have a field named Countries and want to use it as autocomplete field.
I have already loaded all the Countries in Sitecore as Sitecore Object.
I was following the Custom Credit card field , but it didn't help much.
I created custom autocomplete Field in WFFM and now i want to create custom validation for this field .
Any help..
Thanks
To keep it simple you might want to consider a custom control like credit card and use client side JS to call your countries via sitecore web API and then do all the auto complete stuff in the js leaving all logic there.
If server side code is a preference then u can write a web service to return you the JSON objects of matching countries making your JS more simpler.
Hope this can help with some ideas.

Sitecore Web Forms drop list linkage

I have a question about the Drop List field type of Sitecore Web Forms for marketers.
I want to build a web page which has two drop lists that are connected. When the first one changes, the other one's data changes based on the selected value of the first one.
How do I achieve this?
This is not possible out of the box. You will need to build a custom WFFM field that will store the 2 droplist. If you need more information on how to build custom WFFM fields you can checkout this blog post.