Gravityforms - Add category mid-radio-button field - radio-button

I am populating Gravityforms fields using the functions.php from my template and it works like a charm, but I have one field that is ... a challenge. I have been able to populate the choices from my database just fine, but with the functions.php I cannot control the content of the display area of the field so that I can, for example, add a title or header for each category. Is there a way to programattically adjust the display here's an example of what im hoping to accomplish
RadioButton Choice (Field ID 21)
Dark Colors (category title)
maroon (choice 1)
navy blue (choice 2)
black (Choice 3)
Standard Colors (Category Title)
Red (choice 4)
blue (choice 5)
gray (Choice 6)
Light Colors )Category Title)
pink (choice 7)
sky blue (choice 8)
white (Choice 9)
I am just looking for a way to add the category title between the choices. My DB Query has the categories as part of the response, but the only option I have to populate choices to to feed an array.
I have seen where I can add additional Gravityform fields and have them controlled by the same "single select" radio button option, but the categories involved change based on the DB query I call to dynamically populate the choices and could range from 1 category to 10, which will not have correlating fields in the form itself, as this is all under a single radio-button field.
Any thoughts would be appreciated.

I was able to use the link I posted in my comment to provide a solution. As part of my "choices"
here is the function for the Pre-Render to populate choices. In this case I am populating a radio-button field with product images instead of default radio buttons
add_filter('gform_pre_render_1', 'populate_choices');
function populate_choices($form) {
global $wpdb;
// Now to get a list of the products I want to include on this radio-button
$dataQ = "
SELECT category, product_id, product_name
FROM product_table
WHERE product_type = 'whatever I am looking for'
ORDER BY category, product_name ASC
";
$dataR = $wpdb->get_results($dataQ);
$dataList = array();
// get current protocol to use in Product Images
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// Rebuild URL for use with product image graphics
$productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';
// Generate list of choices for the radio-button, along with category headers
foreach ($form['fields'] as $field) {
if ($field->id == '32') { // 32 My specific radio-button field
$category = "";
foreach ($dataR as $data) {
if ($category != $data->category) {
// If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
$category = $data->category;
$dataList[] = array(
'text' => '#'.$category."#",
'value' => ''
);
}
$productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
$productID = $data->product_id;
$dataList[] = array(
'text' => $data->product_name,
'value' => $productID,
'isSelected' => false,
'price' => '0.00',
'imageChoices_image' => $productImagehUrl.$productImage
);
}
$field->choices = $dataList;
}
}
}
I then added a specific field modifier to update the "#category#" choice elements with html to make the category names show up.
// numbers after filter = "1" for form ID 1, "32" for field ID 32
add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
if ( strpos( $choice['text'], '#' ) === 0 ) {
$categoryName = str_replace("#","",$choice['text']);
$choice_markup = '
<li style="width:100%;text-align:left;">
<span style="color:#000000;font-weight:bold;font-size:18px;">
<br>
'.$categoryName.'
</span>
</li>
';
}
return $choice_markup;
}, 10, 2);

Related

getProduct()->getTag() return null, when it should return tags associated to the Product

In my project, we have products that has tag called serviceItem. Those item with that tag when ordered should be separated by the quantity into individuals order.
It issue is that getTags() returns null, and getTagIds gets "Call to a member function getTagIds() on null" when it gets to the next loop.
Is there a reason for why getTags() returns null?
private function transformOrderLines(OrderEntity $order): array
{
/**
* TODO: If we need to send advanced prices,
* the price value of the the lines array should be changed to caldulate the advanced price,
* with the built in quantity calculator
*/
$lines = [];
foreach ($order->getLineItems() as $orderLine) {
$hasDsmServiceItemTag = $orderLine->getProduct()->getTags();
$lines[] = [
'name' => $orderLine->getLabel(),
'sku' => substr($orderLine->getProduct()->getProductNumber(), 0, 19),
'price' => (string) ($orderLine->getProduct()->getPrice()->first()->getNet()
* $order->getCurrencyFactor()), //gets original price, calculates factor
'quantity' => (string) $orderLine->getQuantity()
];
}
$shipping = $this->transformShipping($order);
if ($shipping) {
$lines = array_merge($lines, $shipping);
}
return $lines;
}`
I also tried $orderLine->getProduct()->getTags()->getName() it also return "Call to a member function getTags() on null"
The problem is wherever the $order is fetched from the DB the orderLineItem.product.tag association is not included in the criteria.
For performance reasons shopware does not lazily load all association when you access them on entities, but you have to exactly define which associations should be included when you fetch the entities from the database.
For the full explanation take a look at the docs.

Retrieve data from Array to text-boxes on select change

I want to auto-populate data in text-boxes for in VUE. I have this set of array.
[
{"ID":"1","Name":"JOHN DOE","Email":"JohnDoe#GMAIL.COM","Phone Number":"58656","Address":"Somewhere"},
{"ID":"2","Name":"JANE ZOE","Email":"JohnDoe#GMAIL.COM","Phone Number":"9969","Address":"Anywhere"},
{"ID":"3","Name":"JENNY JAMES DOE","Email":"JJames#GMAIL.COM","Phone Number":"888888","Address":"Everywhere"}
]
CODE PEN https://codepen.io/hiro-john/pen/jOOwwza?editors=1010`
If anyone select, 'JOHN DOE' from the Dropdown Name , his details should be auto-populate to the respective fields which are 'Email, Phone & Address' from the Array List. User can add more than 1 Person and each Person data should populate base on 'Name' Dropdown.
Used this function to search inside the Array.
function indexWhere(array, conditionFn) {
const item = array.find(conditionFn)
return array.indexOf(item)
}
And bind the value on Select Change Event.
const index = indexWhere(items, item => item.Name === name)
this.shareholders[id].Address = items[index].Address;
this.shareholders[id].Email = items[index].Email;
this.shareholders[id].Phone = items[index].Phone;
Updated CODE PEN https://codepen.io/hiro-john/pen/jOOwwza?editors=1010

Magento - SQL - Select all products by attribute and update it

I need to select all products with specific attribute (barcolor) and then update attribute with another value.
EXAMPLE.
I would like to select all SKU with barcolor = LIGHT GREEN and update them to GREEN.
Thanks!
you can do this from backend. you can go to catalog > manage products > select all products > at the right side you can see update attributes option , select that and click on submit and you will redirect to another page and than give the value in required field and save it.
It can be achieve by programming also.
If you have attribute option ID than:
$sAttributeName = 'brands';
$mOptionValueId = 250; // 250 is Id of Brand 1
$newOptionValueId = 255; // 255 is Id of Brand 2
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter(
$sAttributeName,
array(
'eq' => $mOptionValueId
)
);
$storeId = Mage::app()->getStore()->getStoreId();
foreach($productsCollection as $product) {
$productId = $product->getId();
Mage::getSingleton('catalog/product_action')->updateAttributes(
array($productId),
array('brands' => $newOptionValueId),
$storeId
);
}
If you do not have attribute option ID than you can use the option value directly as:
$sAttributeName = 'brands';
$mOptionValue = 'Brand 1';
$newOptionValue = 'Brand 2';
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter(
$sAttributeName,
array(
'eq' => Mage::getResourceModel('catalog/product')
->getAttribute($sAttributeName)
->getSource()
->getOptionId($mOptionValue)
)
);
$storeId = Mage::app()->getStore()->getStoreId();
foreach($productsCollection as $product) {
$productId = $product->getId();
Mage::getSingleton('catalog/product_action')->updateAttributes(
array($productId),
array('brands' => Mage::getResourceModel('catalog/product')
->getAttribute($sAttributeName)
->getSource()
->getOptionId($newOptionValue)),
$storeId
);
}
If you have the list of the entity_ids for all the products and you are using a custom attribute, you can run an SQL query for each and every product like so:
UPDATE `catalog_product_entity_varchar` SET value = 'NEW BLACK' WHERE entity_id = '12345' AND attribute_id = 'attribute_id_here';
You can also filter the fields by:
SELECT entity_id,value FROM catalog_product_entity_varchar WHERE value LIKE 'something';

How to put contrasting information into a CGridView column based on a condition?

I'm looking into showing/hiding specific column data on a CGridView widget for the Wii Framework.
I have a CButtonColumn which contains 3 buttons. However, on certain conditions, I want to display something different for a particular row.
I have 3 different conditions which determin what gets displayed for particular row.
The following illustrates what I want to do:
| 1 | Title A | [hide][view][update] <-- if (condition == 'a')
| 2 | Title B | [hide][view][update] <-- if (condition == 'a')
| 3 | Title C | display text or link or button <-- if (condition == 'b')
| 4 | Title D | display alternative buttons <-- if (condition == 'c')
What is my best approach to take here?
I can't use 'visible'=> $model->processingStatus != "processed" on the column because this will remove the whole column. I need to target each row insatead.
Should I use the 'visible' parameter on each individual button? I have tried this using the commented out code below but it breaks the page.
FYI: I have successfully tried the 'visible' parameter on the CButtonColumn itself, but its not what I need. Plus not sure which row's status it is reading.
Or should I add a function to the controller? Have it do the if/else statements and return back what is to be displayed. How would this work?
Here is my code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'my-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'myid',
'header'=>'ID',
),
'Title',
array(
'class'=>'CButtonColumn',
'visible'=> $model->status != "done",
'template'=>'{hide}{view}{update}',
'buttons'=>array(
'hide'=>array(
'label'=>'Hide', //Text label of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/bulb-off.png' //Image URL of the button.
//'click'=>'function(){alert("Toggle Hide!");}', //A JS function to be invoked when the button is clicked.
//'options'=>array(), //HTML options for the button tag.
//'url'=>'javascript:void(0)', //A PHP expression for generating the URL of the button.
//'visible'=> $model->status == "done", //A PHP expression for determining whether the button is visible.
),
'view'=>array(
//Text label of the button.
'label'=>'View',
//Image URL of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/view-record.png'
),
'update'=>array(
'label'=>'Update/Edit',
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/edit-pencil.png',
'url'=>'Yii::app()->createUrl("metadataandchapters/create?bookid=" . $data->bookid)',
)
)
)
)
)); ?>
Hope I am making good enough sense here!
You should use visible button option, but it should be a PHP expression string, e.g. :
'visible'=> '$data->status == "done"',
http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail
Extend CButtonColumn with your own class, then you should be able to change this function to whatever you need to render or hide buttons or do any changes you want.
/**
* Renders a link button.
* #param string $id the ID of the button
* #param array $button the button configuration which may contain 'label', 'url', 'data-icon', 'imageUrl' and 'options' elements.
* #param integer $row the row number (zero-based)
* #param mixed $data the data object associated with the row
*/
protected function renderButton($id, $button, $row, $data)
More details about the function http://www.yiiframework.com/doc/api/1.1/CButtonColumn#renderButton-detail

How do I do different maxlengths for different text fields?

I have an options panel in my WordPress theme with several text fields e.g. project title, project description, etc.
case 'text':
$val = $value['std'];
$std = get_option($value['id']);
if ( $std != "") { $val = $std; }
$output .= '<input class="" maxlength="12" name=""'. $value['id'] .'" id="'. $value['id'] .'" type="'. $value['type'] .'" value="'. $val .'" />';
break;
As the above code shows... I currently have my max length for the text fields as 12, but I would like to have a max length to be different for each of the text fields. How can I do this?
Define an array containing the fields and their corresponding maxlength values.
(I've used field names, but you could also use an Id number)
E.g: field_name => max_length_for_that_field
$maxFieldLengths = array(
'project_title' => 12, //project title has 12 max length
'project_description' => 100 //project description has 100 max length
);
Identify which text field (input) you are currently building and use that identifier to reference your maxFieldLengths array.
case 'text':
...
$field_name = // Identify the current field here;
$output .= '<input class="" maxlength="'. $maxFieldLengths[$field_name] .'" etc .....' />
break;
Note: From your code sample it's not 100% clear how you identify the field you are currently building. You could, for example, use something like:
...
$field_name = getFieldFromId($value['id']);