Magento: PHP to PDF Invoice - Create new line / Wordwrap Content - php

I'm editing my invoices file in
/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php
And I'm running into a problem with columns running into and overlapping other columns if they have long content. eg Product description running over the next column. I'm trying to figure out how to limit the width / word wrap / create a new line for content.
Code is:
// draw Product name
$lines[0] = array(array(
'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true),
'feed' => 35,
));
// draw SKU
// $lines[0][] = array(
// 'text' => Mage::helper('core/string')->str_split($this->getSku($item), 25),
// 'feed' => 255
// );
// draw Brand (Added by James)
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getSku($item), array('manufacturer'));
if ($product) {
$lines[0][] = array(
'text' => Mage::helper('core/string')->str_split($product->getAttributeText('manufacturer'), 15),
'feed' => 220
);
}
// draw Colour (Added by James)
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getSku($item), array('pos_short_colour'));
if ($product) {
$lines[0][] = array(
'text' => Mage::helper('core/string')->str_split($product->getAttributeText('pos_short_colour'), 15),
'feed' => 320
);
}
I know that 'feed' is what sets how far in from the left the array starts (pretty much = to a margin-left if all items were 'absolute' in css terms).
But I'm not sure how I can limit their width and word-wrap so that if I have a long manufacturer it won't run into the colour. There isn't enough space on the invoice to just simply give each attribute more room.

The answer was right there:
$lines[0] = array(array(
'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true),
'feed' => 35,
));
The 60 here is how many characters will be displayed per line ->getName(), 60, true,

I had the same issue. When I added a new column, Product description column disappeared. I solved this by reducing a number (length) in str_split function of $productDescr.

Related

How to arrange columns in my Admin bar in WordPress dashboard?

I am using the below code in my functions.php to show a 60x60 image thumbnail of each posts featured image on WordPress dashboard but it shows up in like the 3rd column and I would like it to show up first to the left of the title column - can't seem to make that work - any suggestions anyone?
// show featured images in dashboard
add_image_size( 'showimg-admin-post-featured-image', 60, 60, false );
// Add the posts and pages columns filter. both use the same function.
add_filter('manage_posts_columns', 'showimg_add_post_admin_thumbnail_column', 2);
add_filter('manage_pages_columns', 'showimg_add_post_admin_thumbnail_column', 2);
// Add the featured image column
function showimg_add_post_admin_thumbnail_column($showimg_columns){
$showimg_columns['showimg_thumb'] = __('Featured Image');
return $showimg_columns;
}
// Manage Post and Page Admin Panel Columns
add_action('manage_posts_custom_column', 'showimg_show_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'showimg_show_post_thumbnail_column', 5, 2);
// Get featured-thumbnail size post thumbnail and display it
function showimg_show_post_thumbnail_column($showimg_columns, $showimg_id){
switch($showimg_columns){
case 'showimg_thumb':
if( function_exists('the_post_thumbnail') ) {
echo the_post_thumbnail( 'showimg-admin-post-featured-image' );
}
else
echo 'hmm… your theme doesn\'t support featured image…';
break;
}
}
To insert the item to the right of the checkbox (index 0) in the array.
Use array_merge and array_slice to add to the column array.
// Add the featured image column
function showimg_add_post_admin_thumbnail_column($showimg_columns){
$new_column['showimg_thumb'] = __('Featured Image');
return array_merge(
array_slice( $showimg_columns, 0, 1, true ),
$new_column,
array_slice( $showimg_columns, 1, null, true )
);
}
For reference... the default Column Order Array is this:
array(
'cb' => '<input type="checkbox" />',
'title' => "Title",
'author' => "Author",
'categories' => "Categories",
'tags' => "Tags",
'comments' => '<span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span>',
'date' => "Date"
);

$textrun->addText content being processed before $footer->addText content

I want to have a footer that contains 3 lines of text, with a vertical space (like a blank line) between lines 2 and 3.
Because line 3 contains bold and normal text, I have to implement it as a textrun.
But there should be a line break between lines 1 and 2, so I use addText for both of these.
Unfortunately, the order in which the footer content is displayed is as follows:
textrun
footerText1
footerText2
The textrun gets processed first and appears above the other lines!
How do I get the order right?
My footer code is:
// create footer
$footer = $section->addFooter();
// textrun declaration removed from here
// create footer content
$footerText1 = "Blah blah blah.";
$footerText2 = "Ipsum loret Ipsum loret Ipsum loret.";
// define font styles
$smallFontStyleName = 'smallText';
$phpWord->addFontStyle($smallFontStyleName, array(
'name' => 'Helvetica',
'size' => 8,
));
$boldSmallFontStyleName = 'BoldSmallText';
$phpWord->addFontStyle($boldSmallFontStyleName, array(
'bold' => true,
'name' => 'Helvetica',
'size' => 8,
));
// define paragraph spacing styles
$phpWord->addParagraphStyle('line1FooterStyle', array( 'spaceAfter'=>20));
$phpWord->addParagraphStyle('line2FooterStyle', array( 'spaceAfter'=>380));
// add content
$footer->addText($footerText1,
array('name' => 'Helvetica', 'size' => 8),
array('space' => array('after' => 20))
);
$footer->addText($footerText2,
array('name' => 'Helvetica', 'size' => 8),
array('space' => array('after' => 380))
);
// textrun relocated to here
$textrun = $footer->addTextRun();
$textrun->addText('T', $boldSmallFontStyleName);
$textrun->addText(' ++353 1 555 0001 ', $smallFontStyleName);
$textrun->addText('E', $boldSmallFontStyleName);
$textrun->addText(' abc.def#ghk.ie ', $smallFontStyleName);
$textrun->addText('W', $boldSmallFontStyleName);
$textrun->addText(' abcd.ie/wxz', $smallFontStyleName);
OK, I saw the problem and fixed it. I had declared the textrun before the $footer->addText lines. Which means the textrun code was inserted first, incorrectly. D'oh!

How to styling the cell in PHPexcel?

I would like to:
1) set min-width of the cell
On the internet there is some discussion about this , but I find the code not work:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(100);
the column size is not expand according to the data size / preset width
2) underline the cell
I am making an account table, so I would like to underline some row
$styleArray = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
However the underline is not exist, I wonder is it due to the border also count as 1 line? for example , if A1 is underlined, then I need to user A3 for the next row.
Thanks a lot for helping.
Update:
1) set min-width:
From the source code the column is a number instead of string , so I try like this but still no luck
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setAutoSize(false);
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setWidth('4.42');
2) underline is working now
$border_bottom = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$excel->getActiveSheet()->getStyle("7")->applyFromArray($border_bottom);
it works, but just wondering can I underline when set cell instead of hardcode the position: A1:B2 etc...
In number 1, you can check this: PHPExcel auto size column width
With regards on your number 2, have you tried to use this? Instead of underlining the cell, you underline the value of the cell.
$styleArray = array(
'font' => array(
'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
unset($styleArray);
If you want a range of cells to be underlined, instead of:
objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
use:
$objPHPExcel->getActiveSheet()->getStyle('A1:A3')->applyFromArray($styleArray);

How To Remove Decimal From Magento-1 Prices?

all I found in my search is a programming solution for this.
I know that we can modify /lib/Zend/Locale/Data/en.xml for english stores.
there was in en.xml this part:
<currencyFormats>
<currencyFormatLength>
<currencyFormat>
<pattern>#,##0.00 ¤</pattern>
</currencyFormat>
</currencyFormatLength>
</currencyFormats>
And the price was displaying in this format: 1,321.54
now to remove the decimal part from price I think the only thing I have to do is change en.xml to be like the following:
<currencyFormats>
<currencyFormatLength>
<currencyFormat>
<pattern>#,##0 ¤</pattern>
</currencyFormat>
</currencyFormatLength>
</currencyFormats>
The problem is after this change the prices are show as desired (1,132 Format) but without currency symbol ($).
what I'm missing here??
Thanks in advance.
update
I'm still trying, when pattern node changed to the following
<pattern>¤ #,##0</pattern>
the prices are coming with currency symbol ($ 1,132) but not in desired position O_O, the requirement is to have currency symbol on the right side no left :( SO..
All answers here involve changing the core files. This is NOT what anyone should do. Either you develop a module and make those changes or you leave the core files like that and change the prices with str_replace.
So go into theme/template/catalog/product/price.phtml and (depending on configuration) around line 209 change this:
$_coreHelper->formatPrice($_price, true)
into
$without_decimals = $_coreHelper->formatPrice($_price, true); echo str_replace(".00", "", $without_decimals);
This removes .00 from the price. The good thing is that the prices with other decimals are kept. If you don't want this you can remove everything after the dot and round the number up with round() function.
Depending on configuration other prices might need the change (if you show prices without taxes etc.)
In order to change price precision in magento you would need to overwrite some core files.
In the example below we are changing precision to 0.
1) Overwrite lib/Zend/Currency.php and change precision around line:
69 protected $_options = array(
70 'position' => self::STANDARD,
71 'script' => null,
72 'format' => null,
73 'display' => self::NO_SYMBOL,
74 'precision' => 0, /*CHANGE*/
75 'name' => null,
76 'currency' => null,
77 'symbol' => null,
78 'locale' => null,
79 'value' => 0,
80 'service' => null,
81 'tag' => 'Zend_Locale'
82 );
2) overwrite app/code/core/Mage/Core/Model/Store.php and change roundPrice function:
public function roundPrice($price)
{
return round($price, 4);
}
3) overwrite app/code/core/Mage/Directory/Model/Currency.php and change format function:
public function format($price,
$options=array(),
$includeContainer = true,
$addBrackets = false)
{
return $this->formatPrecision( $price,
4,
$options,
$includeContainer,
$addBrackets);
}
To remove the decimal part from price, you need to modify the file
"code/core/Mage/Directory/Model/Currency.php"
First, instead of the line:
return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
use:
return $this->formatPrecision($price, 0, $options, $includeContainer, $addBrackets);
To change the position of the currency symbol, modify the file "lib/Zend/Locale/Data/en.xml" with the line:
<pattern>#,##0.00 ¤;(#,##0.00 ¤)</pattern>
When done, don't forget to clear cache.
P.S. To avoid any issues during the upgrade, we strongly recommend you to implement all the above mentioned changes via extensions:
(check the tools here: http://www.magentocommerce.com/magento-connect/catalogsearch/result/?id=&s=7&pl=0&eb=0&hp=0&q=currency|position&t=1&p=1)
To remove the decimal part from price, you need to modify the file:
1) First is to change precision around line
lib/Zend/Currency.php
protected $_options = array(
'position' => self::STANDARD,
'script' => null,
'format' => null,
'display' => self::NO_SYMBOL,
'precision' => 2,
'name' => null,
'currency' => null,
'symbol' => null,
'locale' => null,
'value' => 0,
'service' => null,
'tag' => 'Zend_Locale'
);
Change= 'precision' => 2, to 'precision' => 0,
2) Second file (change roundPrice function:)
app/code/core/Mage/Core/Model/Store.php
public function roundPrice($price)
{
return round($price, 2);
}
To
public function roundPrice($price)
{
return round($price, 0);
}
3) Third and last is to change format function:
app/code/core/Mage/Directory/Model/Currency.php
public function format($price,
$options=array(),
$includeContainer = true,
$addBrackets = false)
{
return $this->formatPrecision( $price,
2,
$options,
$includeContainer,
$addBrackets);
}
TO
public function format($price,
$options=array(),
$includeContainer = true,
$addBrackets = false)
{
return $this->formatPrecision( $price,
0,
$options,
$includeContainer,
$addBrackets);
}
You can do one more change addition to the above all.
Please go to the PriceCurrency.php page
then change the last line to
public function round($price)
{
return round($price, 0);
}
after that in the PriceCurrencyInterface.php page make
const DEFAULT_PRECISION = 0;
at the top.
That's all. Hope it will work.

WooCommerce Changing Simple Product to Variable and Vice Versa, but how to get new variations to show on the frontend?

I am creating a plugin that ties into the Runit System for showing onhand products in WooCommerce. This requires changing products via a Cron job that will update them, from Simple Products to Variable Products and vice versa depending on the quantity of stock and the variations of these products, which change. I have working code, that updates these products in the backend, showing the correct variations, however, it does not update on the front end. The new variations, even when editing Variable Products, do not show on the front end when viewing the actual Product itself. I still see the old variations in there. How to update the variations to use the new variations automatically, instead of having to hit the Update button in the backend Administration when editing the Product?
Is there some sort of function that needs to be called for updating the Variations in WooCommerce that I'm not aware of? I am using the array like this and serializing it before updating the meta_value for the meta_key _product_attributes in wp_postmeta table, like so:
function ProcessBasicProperties(Array $properties)
{
$return = array();
$position = 0;
if (!empty($properties))
{
if (!empty($properties['siz']))
{
++$position;
$size = !is_array($properties['siz']) ? array($properties['siz']) : array_unique($properties['siz']);
$return['size'] = array(
'name' => 'Size',
'value' => count($size) > 1 ? implode(' | ', $size) : $size[0],
'is_visible' => count($size) > 1 ? 0 : 1,
'is_variation' => count($size) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['ext']))
{
++$position;
$extension = !is_array($properties['ext']) ? array($properties['ext']) : array_unique($properties['ext']);
$return['extension'] = array(
'name' => 'Extension',
'value' => count($extension) > 1 ? implode(' | ', $extension) : $extension[0],
'is_visible' => count($extension) > 1 ? 0 : 1,
'is_variation' => count($extension) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
// styles do not get added to attributes for variable products, instead, with variable products, the style goes into the overall sku of the product (General Properties)
// So, in short, variable products should not have this key set.
if (!empty($properties['style']))
{
++$position;
$return['style'] = array(
'name' => 'Style',
'value' => htmlspecialchars($properties['style'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['color']))
{
++$position;
$colors = !is_array($properties['color']) ? array($properties['color']) : array_unique($properties['color']);
$return['color'] = array(
'name' => 'Color',
'value' => count($colors) > 1 ? htmlspecialchars(implode(' | ', $colors), ENT_QUOTES) : htmlspecialchars($colors[0], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['gender']))
{
++$position;
$return['gender'] = array(
'name' => 'Gender',
'value' => htmlspecialchars($properties['gender'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['season']))
{
++$position;
$return['season'] = array(
'name' => 'Season',
'value' => htmlspecialchars($properties['season'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
}
return $return;
}
This is a function that returns the proper array that get serialized and inputted into the wp_postmeta table for the meta_value where meta_key = _product_attributes. Each variation also has a meta_key of attribute_size and meta_key of attribute_extension where the meta_value equals the value of that specific variation as required within the wp_postmeta table for that posts variation ID.
I'm at a loss, trying to update the variations of a Variable Product, or when Converting a Simple Product to a Variable Product, that needs to be updated. It shows up fine in the backend admin panel of the product, but when going to the actual product page, it still shows the OLD product variations. The only way to show the new one's is to go into the backend admin panel of the product and hit the Update button, which than shows all of the new variations. But how to do this programmatically? I thought this was possible? Must be something I'm overlooking here? Perhaps there are terms somewhere that need updating?
Functions that I have tried, that failed to update the product with the new variations, s for small = variation id 181342 and l for large = variation id 181343 from ID column of of wp_posts table or post_id column of the wp_postmeta table for that Variation:
wp_set_object_terms(181342, 's', 'size');
wp_set_object_terms(181343, 'l', 'size');
and
do_action( 'woocommerce_create_product_variation', 181342 );
do_action( 'woocommerce_create_product_variation', 181343 );
None of these update the product as a variation that shows on the front end. It still shows ONLY the old variations on the front end. How to get the size s and l showing on the front end?
Figured it out... looks like you have to update the wp_options table using set_transient... Here's what I did and it nipped it in the bud:
$transients = array(
'name' => array(
'wc_product_total_stock_' . $product_id,
'wc_product_children_ids_' . $product_id
),
'values' => array(
$total_stock,
$allVariationIDs
),
'filters' => array(
'woocommerce_stock_amount'
)
);
// Set it up so that the variations show in the front end also...
foreach($transients['name'] as $k => $transient_name)
{
set_transient($transient_name, $transients['values'][$k],YEAR_IN_SECONDS);
if (isset($transients['filters'][$k]))
apply_filters($transients['filters'][$k], $transients['values'][$k]);
}
$product_id = the actual products ID value.
$allVariationIDs = an array of all ID values of the variations within the wp_posts table. $total_stock is the new stock of the product, calculating in all variation stock levels.
Hope this helps someone else to update their variations on the front end, after setting up all wp_posts and wp_postmeta table rows for your products and variations.
Along with updating product attributes, you will also have to update the product variations from the database.
For example, while deleting the product variations, you will have to remove it from the wp_posts table
You can refer this link for more details
Woocommerce - Cannot delete a product variation

Categories