I'm working on a prestashop custom module.
This module will have to show some product features, let the admin modify its values and then save them in a custom tab.
So, showing features in a custom tab haven't been so difficult:
public function hookDisplayAdminProductsExtra($params) {
$id_lang = $this->context->language->id;
$features = FeatureCore::getFeatures($id_lang);
$values = [];
foreach ($features as $feature) {
array_push($values, $feature['value']);
}
$this->context->smarty->assign(array(
'features' => $features,
'values' => $values
));
if(!empty($sampleObj) && isset($sampleObj->id)){
$this->context->smarty->assign(array(
'custom_text_area' => $sampleObj->textarea
));
}
return $this->display(__FILE__, 'views/admin/sample.tpl');
}
The only thing which I can't get is features' default values in order to put them into a select.
After that what I want to do is to be able to save every value on product save.
So I wrote this hook:
public function hookActionProductUpdate($params) {
$id_product = $params['id_product'];
$product = new Product($id_product);
var_dump($product);
$all_tpl_vars = $smarty->getTemplateVars();
print_r($all_tpl_vars);
die("hello");
}
but when I press the save button nothing happens, and nothing will be shown on screen.
This is my first prestashop module, all the hooks has been registered in the module constructor.
Thanks to all.
Related
I'm having trouble setting the configurable attributes for a configurable product.
My configurable and simple products are being displayed on the admin side.
But when I click on a configurable product I see this
I found this question that's similar to mine
Magento 'Select Configurable Attributes' with PHP
but I still can't get my config product to be set correctly.
I'm calling my class MagentoProduct and passing "color_of_product" as my attribute code to my "setConfigurableAttributesData" method.
I later save the product by calling "save()".
class MagentoProduct {
private $product;
public function __construct ( ) {
Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->product = Mage::getModel('catalog/product');
}
...
...
...
public function setConfigurableAttributesData($attribute_code){
$super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attribute_code);
$configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
$newAttributes[] = array(
'id' => $configurableAtt->getId(),
'label' => $configurableAtt->getLabel(),
'position' => $super_attribute->getPosition(),
//not 100% if values is correct, what do I set this to?
'values' => $configurableAtt->getPrices() ? $this->product->getPrices() : array(),
'attribute_id' => $super_attribute->getId(),
'attribute_code' => $super_attribute->getAttributeCode(),
'frontend_label' => $super_attribute->getFrontend()->getLabel(),
);
echo $configurableAtt->getId()."\n";
echo $configurableAtt->getLabel()."\n";
echo $super_attribute->getPosition()."\n";
//not 100% if values is correct, what do I set this to?
$temp = $configurableAtt->getPrices() ? $this->product->getPrices() : array();
echo $temp."\n";
echo $super_attribute->getId()."\n";
echo $super_attribute->getAttributeCode()."\n";
echo $super_attribute->getFrontend()->getLabel()."\n";
$this->product->setCanSaveConfigurableAttributes(true);
$this->product->setConfigurableAttributesData($newAttributes);
}
...
...
public function save(){
try{
$this->product->save();
}catch(Exception $e){
echo $e->getMessage();
Mage::log($e->getMessage());
}
}
}
The echos in my setConfigurableAttributesData function are printing:
Color
0
Array
176
color_of_product
Color
What am I doing wrong?
I've been trying to debug this for hours and can't figure it out.
If you want to set configuratble product using color_of_product than you have to set this attribute as
1) Global
2) DropDown
3) Apply to Configurable Product And Simple Product
4) Set Yes for Use To Create Configurable Product.
If for more you can check Color attribute of default magento.
I want to override postSaveHook in VirtueMart so that when I edit any user then I can update some fields automatically.
protected function postSaveHook(JModelLegacy $model, $validData = array())
{
//Add some code here..
return;
}
This hook can be found in
"administrator/components/com_users/controllers/user.php"
I have a basic relation editor GridField, and I need to dynamically define/set the value of any objects added via that GridField, with data related to the GridField's context.
class Draw extends DataObject {
private static $has_many = array(
'Items' => 'Item'
);
}
When an Item is added via Draw's Items GridField, I need to define a value for use in Item::getCMSFields()
Some good suggestions in here: https://www.silverstripe.org/community/forums/data-model-questions/show/21517?start=7
You can work directly with the GridField's GridFieldDetailForm component, and set fields accordingly.
The code that worked for me is:
$config = GridFieldConfig_RecordEditor::create();
if($this->exists()) {
// Ensure that fields are generated with knowledge of the parent
$editComponent = $config->getComponentByType('GridFieldDetailForm');
$item = new Item();
$item->DrawID = $this->ID;
$editComponent->setFields($item->getCMSFields());
}
$items = new GridField('Items', 'Items', $this->Items(), $config);
$fields->addFieldToTab('Root.Main', $items);
You can then call Draw::get()->byID($this->DrawID) from Item::getCMSFields()
I would like to create sales orders programmatically using quotes with my own custom shipping method, shipping price & title.
This is my custom shipping method model:
<?php
class Mycompany_Mymodule_Model_Carrier
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'icw_shipping';
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!Mage::registry($this->_code)) {
return false;
}
$info = Mage::registry($this->_code);
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setMethod($this->_code);
$method->setCarrierTitle($info['shippingCarrier']);
$method->setMethodTitle($info['shippingTitle']);
$method->setPrice($info['shippingPrice']);
$method->setCost($info['shippingPrice']);
$result = Mage::getModel('shipping/rate_result');
$result->append($method);
Mage::unregister($this->_code);
return $result;
}
public function getAllowedMethods()
{
return array(
$this->_code => 'ICW Shipping',
);
}
}
I try to use it like this:
// Method to save sales order quote
private function saveSalesOrderQuote()
{
Mage::register($this->_settings['ShippingMethod'], array(
'shippingCarrier' => 'Custom',
'shippingTitle' => $this->_orderData->ShippingMethod,
'shippingPrice' => $this->_orderData->ShippingAmount
));
$this->_quote->getShippingAddress()->setShippingMethod(
$this->_settings['ShippingMethod']
);
$this->_quote->getShippingAddress()->setCollectShippingRates(
true
);
$this->_quote->getShippingAddress()->collectShippingRates();
$this->_quote->collectTotals();
$this->_quote->reserveOrderId();
$this->_quote->save();
}
But it does not appear to be working. When the order is created, everything is correct expect for the shipping method, title & price. This is what I see in the backend:
Here's my full code so far: http://pastebin.com/jUTM0VbD
Any idea what I am doing wrong here? How do I use my own custom shipping method and set custom price and title?
Its very simple to add shipping method for order generated programmatically,
Follow above steps
$shippingAddress =$_quote->getShippingAddress()->addData($ShippingAddress);
$shippingAddress->setShippingMethod('methodname_methodname')->setCollectShippingRates(true)->collectShippingRates()->setPaymentMethod('methodcode'); //
How to add Custom button and its functionality in Admin Silverstripe?
Please tell me solution.
Custom Button add only in one menu.
Like #wmk mentioned in the comments, you can just take the framework code for GridFieldPrintButton as a base and go from there. SilverStripe also have a basic tutorial for creating a custom ActionProvider.
Rather than rehash the tutorial here, I will provide you a very basic custom action provider that you can copy and extend to do what you need. While you don't note the exact result you are wanting from the button, I will provide just a very generic class.
This code is a stripped down version of the GridFieldPrintButton that #wmk mentioned. It supports both the button itself invoking the custom code as well as the URL.
I've noted in the code a reference that I have kept to "grid-print-button", this is to make your button sit nicely next to the print rather than likely sitting on another line (as it did in my testing on an older 3.1 site I built).
class GridFieldCustomButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
protected $targetFragment;
protected $someCustomConstructData;
//TargetFragment is just for positioning control of the HTML fragment
//SomeCustomConstructData is just an example of providing some default options into your butotn
public function __construct($targetFragment = "after", $someCustomConstructData = null) {
$this->targetFragment = $targetFragment;
$this->someCustomConstructData = $someCustomConstructData;
}
//Generate the HTML fragment for the GridField
public function getHTMLFragments($gridField) {
$button = new GridField_FormAction(
$gridField,
'custom',
'Custom Action',
'custom',
null
);
return array(
//Note: "grid-print-button" is used here to match the styling of the buttons in ModelAdmin
$this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
);
}
public function getActions($gridField) {
return array('myCustomAction');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'myCustomAction') {
return $this->handleMyCustomAction();
}
}
//For accessing the custom action from the URL
public function getURLHandlers($gridField) {
return array(
'myCustomAction' => 'handleMyCustomAction',
);
}
//Handle the custom action, for both the action button and the URL
public function handleMyCustomAction($gridField, $request = null) {
//Do your stuff here!
}
}
Continuing on from the discussion in the comments, you will need to modify your custom ModelAdmin to add new components to its GridField.
class MyCustomAdmin extends ModelAdmin
{
private static $managed_models = array(
'MyCustomObject'
);
private static $url_segment = 'custom-admin';
private static $menu_title = 'All Custom Objects';
public function getEditForm($ID = null, $Fields = null)
{
$form = parent::getEditForm($ID, $Fields);
$fields = $form->Fields();
$gridField = $fields->fieldByName('MyCustomObject');
$gridFieldConfig = $gridField->getConfig();
$gridFieldConfig->addComponent(new GridFieldCustomButton());
return $form;
}
}
Specifically, the line $gridFieldConfig->addComponent(new GridFieldCustomButton()) does the work, taking your custom button as I have shown above and added it to the ModelAdmin. You can also specify where it should go in the GridField too by providing "buttons-before-left" as the first argument in the GridFieldCustomButton constructor.
eg. $gridFieldConfig->addComponent(new GridFieldCustomButton("buttons-before-left"))
More information regarding GridField fragments can be found in the SilverStripe developer documentation.