Drupal commerce save personnal field - drupal

I'm new to Drupal Commerce ...
I added two fields in the order table :
field_date_de_livraison (text) and field_info_comp_cmde (long text).
In a personalized pane , I try to save the entered values ​​with this code :
function pane_date_livraison_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
if (!empty($form_state['values'][$checkout_pane['pane_id']])) {
$Date_saisie = $form_state['values'][$checkout_pane['pane_id']];
if (!empty($Date_saisie['date_livraison'])) {
$Date_sauvegarde = new DateTime($Date_saisie['date_livraison']);
$Date_sauvegarde = $Date_sauvegarde->format('d/m/Y');
$order->field_date_de_livraison = $Date_sauvegarde;
}
if (!empty($Date_saisie['info_comp'])) {
$order->field_info_comp_cmde = $Date_saisie['info_comp'];
}
}
dpm($order, "RETURN_SUBMIT", $type = 'status');
}
With dpm , I see the values ​​assigned to variables , but then they are not saved to the table?
Thank's for your answers !

Related

product variant array issue

Trying create a variable product variants
I am getting this array from product upload form but I want to convert it like :
{color: ["red", "blue"], size: ["M", "L"]}
how I want to convert it like this ....
[{"color":"red","size":"M"},{"color":"red","size":"L"},{"color":"blue","size":"M"},{"color":"blue","size":"L"}]
I try with this but not working
`foreach($att as $key=>$value)
{
$array[$key] = $value;
foreach($value as $key1=>$value1){
$array[][$key] = $value1;
}
$array[][$key] = $value;
}`

How to insert 50k products in woocomerce from json response using cron job?

I want to insert 50k products in woocommerce using cron job. Data will come from json API. SO please guide me how can I do this job ?
$fileContents = file_get_contents(ABSPATH.'json_array.txt');
if ($fileContents === false)
{
echo 'ERROR!';
}
else
{
$data = json_decode($fileContents, true);
// count($data['DataList']; output 50000
for($i=0;$i<count($data['DataList']);$i++)
{
$Shape = $data->DataList[$i]->Shape;
$Size = $data->DataList[$i]->Size;
$Color = $data->DataList[$i]->Color;
$Clarity = $data->DataList[$i]->Clarity;
$objProduct = new WC_Product();
$objProduct->set_name($ReportNo); //Set product name.
$objProduct->set_status('publish');
$objProduct->set_featured(TRUE);
$objProduct->set_catalog_visibility('visible');
$new_product_id = $objProduct->save();
}
}

PHPexcel - getOldCalculatedValue and rangeToArray

Searched for quite a while now, but I'm stuck at the following problem.
I am using PHPexcel 1.8.0
The spreadsheet is read using the following code:
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, TRUE);
So far ok and it works well.
But some spreadsheets contain external referenced data.
And for that I want to use "getOldCalculatedValue".
How do I combine "getOldCalculatedValue" with "rangeToArray" ?
Or is "rangeToArray" inappropriate for this ?
Thanks for any help or hints !
Simple answer, you can't combine the two
rangeToArray() is a simple method for a simple purpose, it doesn't try to do anything clever, simply to return the data from the worksheet as efficiently and quickly as possible
getOldCalculatedValue() is used for a very specific circumstance, and isn't guaranteed to be correct even then, because it retrieves the last value calculated for the cell in MS EXcel itself, which ,ay not be correct if the external workbook wasn't available to MS Excel in that circumstance, or MS Excel formula evaluation was disable.
When calculating cells values from a formula, the PHPExcel calculation engine should use the getOldCalculatedValue() as a fallback if it finds an external reference, and rangeToArray() will try to use this method, but it isn't perfect, especially when that reference in nested deep inside other formulae referenced in other cells.
If you know that a formula in a cell contains an external reference, you should use getOldCalculatedValue() directly for that cell
I came up with the following solution.
Maybe not perfect, but it currently does the job. Thanks for any improvements!
With PHPExcel included and the excel file uploaded and ready, I continue with:
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
Create a new array to store the cell values of a row
$arr_row = array();
Loop through the rows
for ($rownumber = 2; $rownumber <= $highestRow; $rownumber++){
$row = $sheet->getRowIterator($rownumber)->current();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
Then loop through the cells of the current row
foreach ($cellIterator as $cell) {
Find cells with a formula
$cellcheck = substr($cell->getValue(),0,1);
if($cellcheck == '='){
$cell_content = $cell->getOldCalculatedValue();
}
else{
$cell_content = $cell->getValue();
}
Add the cell values to the array
array_push($arr_row,$cell_content);
Close cell loop
}
At this point I use the $arr_row to do further calculations and string formatting, before finally inserting it into a mysql table.
Close row loop
}
I made some changes in the function rangeToArray() inside Worksheet.php.
Worked fine!
public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) {
// Returnvalue
$returnValue = array();
// Identify the range that we need to extract from the worksheet
list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
$minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] -1);
$minRow = $rangeStart[1];
$maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] -1);
$maxRow = $rangeEnd[1];
$maxCol++;
// Loop through rows
$r = -1;
for ($row = $minRow; $row <= $maxRow; ++$row) {
$rRef = ($returnCellRef) ? $row : ++$r;
$c = -1;
// Loop through columns in the current row
for ($col = $minCol; $col != $maxCol; ++$col) {
$cRef = ($returnCellRef) ? $col : ++$c;
// Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
// so we test and retrieve directly against _cellCollection
if ($this->_cellCollection->isDataSet($col.$row)) {
// Cell exists
$cell = $this->_cellCollection->getCacheData($col.$row);
if ($cell->getValue() !== null) {
if ($cell->getValue() instanceof PHPExcel_RichText) {
$returnValue[$rRef][$cRef] = $cell->getValue()->getPlainText();
} else {
if ($calculateFormulas)
{ ##################################### CHANGED LINES
if(!preg_match('/^[=].*/', $cell->getValue()))
{
$returnValue[$rRef][$cRef] = $cell->getCalculatedValue(); # THE ORIGINAL CODE ONLY HAD THIS LINE
}
else
{
$returnValue[$rRef][$cRef] = $cell->getOldCalculatedValue();
}
} ##################################### CHANGED LINES
else
{
$returnValue[$rRef][$cRef] = $cell->getValue();
}
}
if ($formatData) {
$style = $this->_parent->getCellXfByIndex($cell->getXfIndex());
$returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString(
$returnValue[$rRef][$cRef],
($style && $style->getNumberFormat()) ?
$style->getNumberFormat()->getFormatCode() :
PHPExcel_Style_NumberFormat::FORMAT_GENERAL
);
}
} else {
// Cell holds a NULL
$returnValue[$rRef][$cRef] = $nullValue;
}
} else {
// Cell doesn't exist
$returnValue[$rRef][$cRef] = $nullValue;
}
}
}
// Return
return $returnValue;
}

How to ignore a custom field value if its empty

I have five custom fields for loading images but all of them are not required. I mean the user can upload a random number of images from 1 to 5. I am stuck in a simple lack of concept here. How should I check if any of them is empty and discard it. More specifically I want to discard the non-existing fields and store only the uploaded ones in an array. Here is my code
$custom_fields = get_post_custom($id);
$my_custom_field1 = $custom_fields['image1'];
$my_custom_field2 = $custom_fields['image2'];
$my_custom_field3 = $custom_fields['image3'];
$my_custom_field4 = $custom_fields['image4'];
$my_custom_field5 = $custom_fields['image5'];
if(!(false===($my_custom_field1))) { $img[]=$my_custom_field1;}
if(!(false===($my_custom_field2))) { $img[]=$my_custom_field2;}
if(!(false===($my_custom_field3))) { $img[]=$my_custom_field3;}
if(!(false===($my_custom_field4))) { $img[]=$my_custom_field4;}
if(!(false===($my_custom_field5))) { $img[]=$my_custom_field5;}
$images = Array("image1","image2","image3","image4","image5");
foreach($images as $image){
if(isset($custom_fields[$image])){
$img[] = $custom_fields[$image];
}
}
Didn't tested that, but should work.
if(isset($my_custom_field5)){
// do something with $my_custom_field5
}
I would suggest using
if ( !empty($my_custom_field)){
\\Do Something
}
because isset only checks for variable beings set and not null where as empty checks if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

Drupal: How to override a function from a contrib module

I am using a module that builds a price table for Drupal Commerce items. There is a function that formats the table headers:
/**
* Helper function that takes care of the quantity displayed in the headers of
* the price table.
*/
function commerce_price_table_display_quantity_headers($item) {
// Set the quantity text to unlimited if it's -1.
$max_qty = $item['max_qty'] == -1 ? t('Unlimited') : $item['max_qty'];
// If max and min qtys are the same, only show one.
if ($item['min_qty'] == $max_qty) {
$quantity_text = $item['min_qty'];
}
else {
$quantity_text = $item['min_qty'] . ' - ' . $max_qty;
}
return $quantity_text;
}
As you can see, this is not a theme function where I can override it in template.php but I can to tweak some of the output.
How can I redefine this function so I can chop and change a few things?
My work so far...
So far, I have tried to create it as a seperate module with a few subtle changes to show if it's working or not, but it's not overriding any of the output.
Info file
; $id$
name = Price Table: Tweaked Display
description = A different layout for the price table as shown on the product display nodes
package = Commerce (contrib)
core = 7.x
dependencies[] = commerce_product
dependencies[] = commerce_price
dependencies[] = commerce_price_table
Module File
/**
* Override of the helper function that takes care of the quantity displayed in the headers of
* the price table.
*/
function commerce_table_tweak_display_quantity_headers($item) {
// Set the quantity text to unlimited if it's -1.
$max_qty = $item['max_qty'] == -1 ? t('Unlimited gnhh') : $item['max_qty'];
// If max and min qtys are the same, only show one.
if ($item['min_qty'] == $max_qty) {
$quantity_text = $item['min_qty'];
}
else {
$quantity_text = $item['min_qty'] . ' - this is working - ' . $max_qty;
}
return $quantity_text;
}
Most drupal modules I believe have a hook_function (or at least ones that are hookable) I don't do this much so I would search the drupal api for how to hook functions and if your module will support it.

Resources