How to execute symfony method in prestashop - symfony

Im developing a module for prestashop,
I create a cart_rule in the cart.
I need to delete this cart rule in the hook "actionValidateOrder" and move this quantity to payment method. (for example 20€ discount to 20€ with my module).
Im trying to replicate the method that use prestashop by default.
They use a symfony "method" that i dont know how to use it.
You can find the class DeleteCartRuleFromOrderHandler.
To construct this class you need other classes, okey.
public function __construct(
OrderAmountUpdater $orderAmountUpdater,
OrderProductQuantityUpdater $orderProductQuantityUpdater,
ContextStateManager $contextStateManager
)
I search all classes to construct all of them to pass this variables but some classes don't have method construct!
How can i execute this?
Thanks.

Related

How to override function from core service

The plan is to remove some fields from the default Shopware 6 registration form.
I've copied some twig-templates to my plugin and removed the fields like billingaddress etc ...
An error occurred in the validateRegistrationData function because the field value for billingaddress is null (sounds logic because I've deleted the field in the twig-template)
in vendor/shopware/platform/src/Core/Checkout/Customer/SalesChannel/RegisterRoute.php (line 259)
In my module I would like to override below function in the RegisterRoute.php file (service)
private function validateRegistrationData(...) { ... }
What steps are needed to properly override above function from within my custom shopware 6 plugin.
IMHO validateRegistrationData is only the first place where you have an issue, even you change the validation billing address variable is required below in \Shopware\Core\Checkout\Customer\SalesChannel\RegisterRoute::register() method.
Some my suggestion to you is implementing your own RegisterRoute in your plugin and decorate or even override existing core service.
So steps:
implement own RegisterRoute in your plugin
Register it in service container with the same id as core route:
<service id="Shopware\Core\Checkout\Customer\SalesChannel\RegisterRoute"
class="YourPlugin\Core\Checkout\Customer\SalesChannel\RegisterRoute" public="true">
<!-- your own arguments -->
</service>
Also I think you need to implement your own CustomerValidationFactory and AddressValidationFactory if it is needed by your business logic.
Anyway, you can also face the issue that some fields are required by Customer DAL definition, but you don't set them. So most probably you also need to change a bit CustomerDefinition, i mean override of course.

Symfony 3 and Sonata Admin

I wish to know simply if it is possible to preload the create entity form with given values.
My application has a Daily Schedule view with 30 minute intervals. I want the user to be able to click on a time (eg:12:30) on the Daily Schedule and have it preload a Booking entity form along with the current date.
I have searched to no avail a solution, I would have thought this would be a common action.
I figure I need to access the instantiated object (the Booking) prior to it binding to the request however this is all handled by Sonata Admin.
Is it possible to simply override the Create action to preload the form?
Thank you in advance.
Yes, it is. You could set defaults on your Booking entity or overwrite getNewInstance() in your Admin class, like so:
public function getNewInstance()
{
$object = parent::getNewInstance();
$object->setDefaults();
return $object;
}

sonata_type_model_list customize the admin class called

I am looking for the solution to customize the linked admin class when I use a sonata_type_model_list form type in my admin classes.
An example :
I have 2 admin for one entity named EntityA:
class EntityA
class EntityA1Admin
class EntityA2Admin
This entity is linked in many_to_one relationships with others entities : EntityB and EntityC.
In EntityBAdmin I want to call A1Admin on $formMapper->add('entityA','sonata_type_model_list');
In EntityCAdmin I want to call A2Admin on $formMapper->add('entityA','sonata_type_model_list');
Is there any solution to set manually the admin class that should be call by sonata_type_model_list ?
At least, if it's not possible, is there anyway to customize the default filters in the list view ? (is it possible to customize $dataGridValues through sonata_type_model_list field ?)
Thanks by advance (I already spend hours to find the solution in the code, but i can't find any clear solution....)
If I were you, I would go against using multiple admins for a single entity. I would first try to use some sort of context or parameters, to distinguish on what to show and what to not show in each case for the same EntityA admin (instead of using two separate admins for EntityA).
I believe you want to change filters that are shown in sonata_type_model_list. You might want to try this to know, whether you EntityA admin is being called from within sonata_type_model_list window:
protected function configureDatagridFilters(DatagridMapper $filterMapper)
{
$request = $this->getRequest();
if ($request->query->get('pcode') == '_entity_b_code_') {
...
}
}
Then accordingly add or not add needed filter fields depending on the context.

Silverstripe's Versioned feature for dataobjects in new release (3.2)

I want to audit-trail all changes made to dataobjects. Say I have Event dataobject and I want to know who changed it, when changed, what is changed etc. (Similar to Pages).
Silverstripe site recommends the use of Verioned but I can't find any examples of implementation. It says the best example is Pages which is is already comes with Versioned implemented. The basic rule is to define an augmentDatabase() method on your decorator.
So, I want to use DataExtention for dataobject (extension) and then use the extended one for my Event dataobject. But is there any simple example?
Assuming you want to manage and monitor multiple versions of the event DataObject, you simply need to declare that you want to use the versioned extension for thatDataObject
Class Event extends DataObject{
static $extensions = array(
"Versioned('Stage', 'Live')"
);
...
}
Then run a dev/build
You should now have a Event, Event_Live, and Event_versions tables.
You can then have a look at the methods available in Versioned.php, and use them with Event, ie publish(). This should get you started.
"Versioning in SilverStripe is handled through the Versioned class. It's a DataExtension, which allow it to be applied to any DataObject subclass."
"Similarly, any subclass you create on top of a versioned base will trigger the creation of additional tables, which are automatically joined as required."
Here is link to read further with examples
Versioning of Database Content

Buddypress plugin Group Hierarchy

Buddypress has a group functionality in which I combined with the plugin BP Group Hierarchy so that I can create an hierarchy of groups based on user role.
However, the plugin used an method as taught by Buddpress in group-extension-api> link.
The group steps are registered using the function bp_register_group_extension and add_action are called. I tried to remove the action by with no success. Because I not really understand how the array works i.e. array( &$extension, \'_register\' ), so I go search out and found this post.
There's a line stating that
The new format for the above object referenced method callbacks are always: class name, followed immediately by the method name, followed by the amount of previously added (classname+methodname). For classes, this allows you to add as many object referenced classes and add methods which don’t override each other.
However I can't seems to be able to remove the action.
I tried to remove the action by putting following lines of code in function.php
function remove_bp_hierarchy(){
if (has_action('bp_actions')) {
echo $extension = new BP_Groups_Hierarchy_Extension;
remove_action('bp_actions', array( &$extension, '_register' ), 999);
} else {
}
add_action('bp_init','remove_bp_hierarchy', 999);
Is it something wrong with my remove_action or I use wrong method? Thanks and regards.
## Update
Found a page in which let we see a list of hooks and also hooked function in the page. I see that there's a function with the name _register which is the function I'm looking for. However, class address always change. I was thinking using the function found to do a preg_match on it and remove it when it found. this is super heavy. So is there other way of removing it? Thanks and Regards.
CodingBabyDotCom -
Long story short: you will have to traverse the $wp_filter array to remove the action.
You need a reference to the SAME instance that was used to create the action in order to remove it with the remove_action function. So the function you posted doesn't work because it is using a new instance.
Unfortunately bp_register_group_extension() creates only a temporary instance, so it can't be referenced by later functions.
The code in your comment will remove ALL actions at level 8, which means all group extensions. To remove only the one you want, iterate over each filter and check its type with:
is_a( $wp_filter['bp_actions'][8][$key], 'BP_Groups_Hierarchy_Extension' )

Resources