Edit weight based shipping titles in OpenCart - php

I'm using weight based shipping but I don't want the weight information to be displayed in the checkout page. Right now the displayed information is "USA (Weight: 0.00kg) $24.30", but all I want to be displayed is "USA $24.30".
In controller/checkout/shipping I'm printing the $this->data['shipping_methods'] variable, getting the following array:
Array
(
[weight] => Array
(
[title] => Weight Based Shipping
[quote] => Array
(
[weight_11] => Array
(
[id] => weight.weight_11
[title] => USA (Weight: 0.00kg)
[cost] => 24.3
[tax_class_id] => 0
[text] => $24.30
)
)
[sort_order] => 1
[error] =>
)
)
What I need is to, for each $this->data['shipping_methods']['weight']['quote'], change the title to a substr to minus 16 characters, in order display only the country info (USA, in this case).
How can I do this?

Open up file catalog/model/shipping/weight.php, find this line:
'title' => $result['name'] . ' (' . $this->language->get('text_weight') . ' ' . $this->weight->format($weight, $this->config->get('config_weight_class_id')) . ')',
replace with
'title' => $result['name'],
done.
Best when applied via vQmod extension.

Related

WooCommerce: Get order export with tax lines

I want to export all my orders with the tax lines separated by tax rate.
For example: An order could have the tax rates 7% and 19%. If the order has products with both tax rates, I only get one combined tax amount.
Let's say the order has two products for 100€ each. There would be the following taxes:
100€ at reduced rate: 7€
100€ at default rate: 19€
taxes combined: 26€
I'm using the following code (excerpt) to export order data:
$order_data = $order->get_data(); // The Order data
$order_date_created = $order_data['date_created']->date('d.m.Y'); // Example for oder creation date
After getting the data, I need to store them in an array:
$custom_data = array(
'custom_order_date_created' => $custom_order_date_created,
)
What I need is a fixed column (like the $order_date_created above) for every tax rate.
Like:
Column 1: tax rate - 7%
Column 2: tax amount - 7€
Column 3: tax rate - 19%
Column 4: tax amount - 19€
If there is no amount for a tax rate the column could be empty.
I know that there is an order property called tax_lines. But it's an array and I don't know how to use it. The property tax_lines has some own tax lines properties.
Does anyone has a clue how to get these values from an order?
EDIT: I found a snippet in another question:
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
$tax_rate_id = $tax->rate_id;
$tax_label = $tax->label;
$tax_amount = $tax->amount;
$tax_f_amount = $tax->formatted_amount;
$compound = $tax->is_compound;
echo '<tr><td>' . $tax_label . ': </td><td>' . $tax_f_amount . '</td></tr>';
}
It looks right, but I'm not sure how to use it with my code example and store it in my array.
EDIT 2: After some more digging I found the WC_Order_Item_Tax object which has the order tax items in it.
So I tried to use that data but it is an huge object and I'm not sure how to select the data and show it in the right column.
It looks like this (there is a lot more). And I need to select the tax by the rate_id for example:
[4543] => WC_Order_Item_Tax Object
(
[extra_data:protected] => Array
(
[rate_code] =>
[rate_id] => 0
[label] =>
[compound] =>
[tax_total] => 0
[shipping_tax_total] => 0
[rate_percent] =>
)
[data:protected] => Array
(
[order_id] => 11244
[name] =>
[rate_code] => DE-MWST.-1
[rate_id] => 1
[label] => MwSt.
[compound] => 1
[tax_total] => 7.54
[shipping_tax_total] => 0
[rate_percent] => 16
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 4543
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[rate_code] =>
[rate_id] => 0
[label] =>
[compound] =>
[tax_total] => 0
[shipping_tax_total] => 0
[rate_percent] =>
)
You can use it this way may be after this code line
$custom_data = array(
'custom_order_date_created' => $custom_order_date_created,
)
Then the foreach loop with tax rate items and using WC_Tax method get_rate_percent() to get the tax rate percentage as follows:
// Loop through order tax items
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
// Store it in the array
$custom_data['taxes'][] = array(
'tax_rate' => WC_Tax::get_rate_percent($tax->rate_id), // <=== the tax rate percentage
'tax_amount' => $tax->formatted_amount
);
}
Related:
How to get order tax details and rates in WooCommerce?
Get separate Order's tax totals amounts with tax class

How can I get the cod costs in WooCommerce?

I've a problem. I'm currently writing a WordPress plugin for WooCommerce which reads out an order for a booking system.
The booking system has an own field for cod costs (costs on delivery / Nachnahmegbühr (deutsch)). The problem is that I can't find a method to read this cost from an order.
If I read out the fees from the order via $order->get_fees(), I can see the cod costs but this is not safe enough cause the fee name is always in the shop language which makes it nearly impossible to read out the cod costs this way (in an international plugin):
[data:protected] => Array
(
[order_id] => 24
[name] => Zahlungsgebühr
[tax_class] => 0
[tax_status] => taxable
[amount] => 2.16
[total] => 2.16
[total_tax] => 0.34
[taxes] => Array
(
[total] => Array
(
[6] => 0.344828
)
)
)
So is there any known way to get this kind of costs?

Array - Select the sub children id's from an array

I have this array built from a function that sorts allowed or valid url slugs and their ids into another array. I can call the current category id by passing the url slug to the category. Now I need to get the children category ids (if any) so i can call the appropriate items from the database for display purpose.
Heres an example of the array:
Array (
[radios] => 1
[radios/motorola] => 2
[radios/motorola/handheld] => 3
[radios/motorola/mobile] => 4
[radios/icom] => 5
[radios/icom/handheld] => 6
[radios/icom/mobile] => 7
[radios/mics-and-speakers] => 8
[radios/mounts] => 9
[radios/radio-other] => 10
[misc] => 11
[misc/led] => 12
[phones] => 13
[phones/samsung] => 14
[phones/lg] => 15
[phones/motorola] => 16
[phones/huawei] => 17
[phones/blackberry] => 18
[phones/flip] => 19
[boosters] => 20
[boosters/standalone] => 21
[boosters/indoor-antenna] => 22
[boosters/outdoor-antenna] => 23
[boosters/connections] => 24
[accessories] => 25
[accessories/cases] => 26
[accessories/other] => 27
[internet] => 28
[internet/fusion] => 29
[internet/point-to-point] => 30
[internet/hotspots] => 31
[internet/gateways] => 32
[internet/switches] => 33
[cameras] => 34
[cameras/complete-kits] => 35
[cameras/additional-cameras] => 36
[cameras/other] => 37
);
As you can see, each result points to the category ID of that group. If i visit the following url:
http://example.com/store/cameras
I can print out that THE PATH CURRENTLY IS: 34 which is correct. Since It has children under it, I also need their ID's and the ID's of any of their children, etc etc. That way on radios, i can show ALL of the sub category items, and on radios/motorola i am only showing the Motorola based items and its children, and such down the line.
If there an easy way, using this array I have now, to sort the children (if any) all the way down and get back just their id's (preferably in a new array) for showing database items?
You might want to create a function like this to filter your array,
function filterArray($array, $term) {
$pattern = "/\b" . str_replace($term, '/', '\/') . "\b/i";
foreach($array as $key => $value) {
if(preg_match($pattern, $key)) {
/* Following condition makes sure that your search
will match starting from the beginning. */
if (stripos(trim($key), $term) === 0){
$filtred[] = $value;
}
}
}
}
Then call the above function with the $array and your search $term.
filterArray($array, 'radios') will give you this,
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
filterArray($array, 'radios/motorola') will give you this,
Array ( [0] => 2 [1] => 3 [2] => 4 )
And so on.. I hope this helps.
You can maybe try double layer array, so that you can store values like this,
Array (
[name] => John
[surname] => Smith
[contact] => Array (
[address] => 18 Maple Street
[number] => 555 477 77 77
)
)
This way if you need to print something from "contact" you can use a loop and print all childs of contact field.

Update node of session array + zf2

First I added a session then I print my session it looks completely ok here it is
Array
(
[14] => Array//(main key is my restaurant Id)
(
[retaurantDetail] => Array
(
[restId] => 14
[restaurantName] => Barca
[published] => 1
[timings] => 10 to 10
[normalCost] => 150
[logo] => 44f7afcffb0aeea5c69ccee9041cab84.jpg
[email] => barca#barca.com
[phone] => 741258
)
[menuArray] => Array
(
[70] => Array // (menu ID is the Key)
(
[menuId] => 70
[productId] => 35
[productName] => Coca Cola
[categoryTitle] => Beverages
[categoryId] => 52
[price] => 100
[attributeName] => 1.5L
[isDefault] => 1
[qty] => 1
)
)
)
)
Now I add some logic if some one add again that menu in add to cart in short he/she adds plus 1 qty to that menu here it is my code,(code is not completed yet but now I am focused on just to update qty)
foreach($session->cartSession as $sessionKey=>$sessionVal)
{
foreach($sessionVal['menuArray'] as $sessionMenuKey=>$sessionMenuVal)
{
if($sessionMenuKey == $post_data['menuId'])
{
echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
//$qty = $session->cartSession[$restaurantDetail['restId']]['menuArray'][$sessionMenuKey];
$sessionMenuVal['qty'] = $sessionMenuVal['qty']+1;
echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
}
}
}
What I am missing I want to updatre session qty.
every time I press add it shows 1 qty then I add plus 1 qty to it then it show 2 after then it again shows 1 qty :(.
You're never writing back into the Session...
If i understand your code correctly, the following should work. Check the correct Array-Nesting of my code in case it doesn't work out of the box.
// Somewhere on top, use this for your loops
$cartSession = $session->offsetGet('cartSession');
// Inside your matched loop
$currentQty = $sessionMenuVal['qty'];
$cartSession[$sessionKey][$sessionMenuKey]['qty'] = ++$currentQty;
// After your loops at the end
$session->offsetSet('cartSession', $cartSession);

How to retrieve custom attributes in Opencart?

I have defined some custom attributes and assigned them to products via the Opencart admin panel. I'm trying to retrieve these attributes upon checkout to adjust some shipping costs.
I'm currently working in the catalog/model/shipping/ups.php file in the getQuote function. I'm getting close with this:
print_r($this->cart->getProducts());
Which gives me the following product data:
Array
(
[59] => Array
(
[key] => 59
[product_id] => 59
[name] => My Product Name
[model] => ABC
[shipping] => 1
[image] => data/myproduct.jpg
[option] => Array
(
)
[download] => Array
(
)
[quantity] => 1
[minimum] => 1
[subtract] => 1
[stock] => 1
[price] => 29.99
[total] => 29.99
[reward] => 0
[points] => 0
[tax_class_id] => 0
[weight] => 3.75
[weight_class_id] => 5
[length] => 0.00
[width] => 0.00
[height] => 0.00
[length_class_id] => 3
)
)
However, no custom attributes are returned. I'm sure there is a nice way to pass the product ID to an attribute-getting function, but I can't find it. The Opencart docs are a little lacking for the development side and no luck on Google.
Usually I'd grep the hell out of the whole directory structure to find what I'm looking for, but shell access is disabled on this particular web host :(.
EDIT
I believe Attributes are a newer feature that is built into Opencart. Its like a custom field.
Anyway, on the admin side I went to Catalog > Attributes and created a custom attribute called "Shipping Box Type". Then, under a specific product I can set the attribute. See screenshot. My goal is to retrieve the value of "Shipping Box Type". Does that answer your question #Cleverbot? I'm sure I can do a custom db query to grab it, but theres got to be a built-in function to grab attributes?
Retrieve attributes for a given product_id
$this->load->model('catalog/product');
$attributes = $this->model_catalog_product->getProductAttributes(59);
print_r($attributes);
Output
Array
(
[0] => Array
(
[attribute_group_id] => 9
[name] => Shipping Fields
[attribute] => Array
(
[0] => Array
(
[attribute_id] => 16
[name] => Shipping Box Type
[text] => SM2
)
)
)
)
Taking it further, here is an example of looping through the products currently in the cart to see what attributes they have:
$this->load->model('catalog/product');
$cart_products = $this->cart->getProducts();
// get attributes for each product in the cart
foreach($cart_products as $product) {
$attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);
print_r($attributes);
}
This is not the most intuitive feature in Opencart. First you have to go catalog>attributes>attribute groups and you have to create a group name that will be a title for your attribute. For this it might be "Shipping Specifications".
Then you need to go to catalog>attributes>attributes> and create a new attribute under "Shipping Specifications" named "Shipping Box Type".
Now you are ready to go to catalog>products and add the attribute to a product.
The catch is that it won't be displayed how you were hoping I think. It will show up under the specifications tab next to description on your products page. You have the easy option of changing "Specifications" to the Heading of your choice in catalog/view/*your_theme*/products/products.tpl or you can edit the same tpl file to change the container/format that the data output goes.

Categories