Programmatically update Menu Item in Component in joomla - php

I'm creating a component in Joomla, where among other things, can create and update menus, and I have successfully used this answer to create a new menu item.
https://joomla.stackexchange.com/questions/5104/programmatically-add-menu-item-in-component?newreg=1e7c576205354c0795a55607bc3e2508
$menuTable = JTableNested::getInstance('Menu');
// which menu you want to add to -
$menutype = 'thisismymenusname';
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'menutype' => $menutype,
'title' => $table->alias,
'alias' => $table->alias,
'path' => $table->alias,
'type' => 'heading',
'component_id' => 0,
'language' => '*',
'published' => 1,
);
// this item is at the root so the parent id needs to be 1
$parent_id = 1;
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->save($menuData))
{
$this->setError($menuTable->getError());
return false;
}
But I'm unable to find the correct way to update a menu item, like the title and alias. But it is unsuccessful. I have tried using this with no success.
$menu_title = $_POST['title'];
$menu_alias = JFilterOutput::stringURLSafe($menu_title);
$menuTable = JTableNested::getInstance('Menu');
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'title' => $menu_title,
'alias' => $menu_alias,
);
// this item is at the root so the parent id needs to be 1
$parent_id = ($_POST['parent'] != "" ? $_POST['parent'] : '1');
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->set($_POST['menuid'],$menuData)) {
$this->setError($menuTable->getError());
exit;
}
Can anyone help?

Related

php CI $this->cart->insert($data) get null value

I get stack in script. I used Code Igniter ver. 3.1.10. I use cart library in my controller
here my controller
public function add_to_cart()
{
$idit=$this->input->post('id_item');
$product=$this->Salesmodel->get_item($idit);
$i=$product->row_array();
$data = array(
'id' => $i['id_item'],
'name' => $i['name_item'],
'main_price' => $i['main_price'],
'sell_price' => $i['sell_price'],
);
$this->cart->insert($data);
$rows = count($this->cart->contents()); // I want to find out rows count and result is null
echo $i['id_item']; //get value, not null
echo $rows; // get '0'
}
model.php
function get_item($idit)
{
$rslt=$this->db->query("SELECT * FROM tb_item where id_item='$idit'");
return $rslt;
}
but in that script i always get null row count of the cart.
I have to add this script in config.php :
$config['sess_use_database'] = TRUE;
I also created a new table with a name
ci_session
but that returns the same result, my cart always has null row count and null data. Please help me with an error in the script that I made.
Thanks in advance
In order to save into the cart properly, these 4 array index are required :
id - Item identifier.
qty - Item quantity.
price - Item price.
name - Item name.
And the 5th index are options, which you could store all the additional attribute you need (should be an array though).
So you could modify the $data array like this :
$data = array(
'id' => $i['id_item'],
'qty' => 1, // here I just manually set it to 1
'name' => $i['name_item'],
'price' => $i['main_price'], // here I changed 'main_price' index to 'price'
'options' => array('sell_price' => $i['sell_price']) // moved the 'sell_price' array here
);

Prestashop filter products by category using WebService

I'm trying to use Prestashop WebService via PHP to filter products by categories but it seems that it's impossible.
How to make it? Must be something like this
array(‘resource’ =>’products’, ‘display’ => ‘[name]’, ‘filter[category]’ => ‘[x]');
Which Prestashop version do you use ?
I managed to get products for a specific category for v1.5.6.1 as follows:
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
$opt = array(
'resource' => 'products',
'display' => 'full',
'filter[id_category_default]' => '[8]',
'limit' => '5'
);
$xml = $webService->get($opt);
$resources = $xml->products->children();
At this stage you get a products collection. You can reach properties using standard object notation ..
$xml->categories->category->associations->products->product
foreach ( $resources as $key => $value ) :
echo $value->id; // product's identifier
echo $value->price; // product's .. guess what !
endforeach;
You should be able to see elements exposed by reaching YOUR_SITE/api/products?schema=synopsis
That's fine but I've not been able to retrieve products urls yet in order to print anchors.. Anyone ? Any suggustion ?
Complete documentation (1.5) here
Hope it will help.
EDIT
Construct products URLS on the fly
Make a first API call to retrieve categor(ies/y) you want and their
datas (url slug, ids of products they own, ...)
Make a second API call to retrieve the actual datas corresponding to ids retrieved during the first step.
Slugs are available under the link_rewrite property of items collections (like categories and products). There will be as many slugs as the total of languages that have been configured from the back-end, so you may want to loop over the link_rewrite property to get them all and build all urls.
## Initialize Prestashop API
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
## Getting category I want
$opt = array(
'resource' => 'categories',
'display' => 'full',
'filter[id]' => '[70]', # we are interested only in one category
'limit' => '1'
);
$xml = $webService->get($opt);
$ws_cat = $xml->categories->category;
$products = $ws_cat->associations->products->product;
## Gathering products ids to feed the second API call filter parameter
$productsIds = array();
foreach ( $products as $p ) {
$productsIds[] = (int)$p->id;
}
## Getting products ..
$opt = array (
'resource' => 'products',
'display' => 'full',
'filter[id]' => '['.implode('|',$productsIds).']',
'limit' => '4'
);
$xml = $webService->get($opt);
$products = $xml->products->product;
if ( count($products) ) {
$products = array();
foreach ( $products as $value ) {
$products[] = array(
'id' => $value->id
,'catalogURL' => "{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";
);
# There you go ..
}
}

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

how to create treeview in yii framework

I want to create a tree view which fetching
data from Database and after that
order it by parent field
So table fields are included :
product_category_id
product_category_parent_id
product_category_name
the record that is been mentioned
by name " product_category_parent_id"
is 0 without any root and when it wish have any
ID code / number , parent Id should come in this palce
so structure of the table must be sent to the
View :
<ul><li><ul><li></li></ul></li></ul>
There is an example how to create CTreeView
private function generateTree($models)
{
$data = array();
foreach ($models as $category) {
$data[$category->id] = array(
'id' => $category->id,
'text' => ''.$category->category_name.'',
);
foreach ($category->goods as $item) {
$data[$category->id]['children'][$item->id] = array(
'id' => $item->id,
'text' => ''.$item->article.'',
'expanded' => false,
);
}
}
return $data;
}
In view
$this->widget('CTreeView', array('data' => $data,'persist'=>'cookie'));

Opencart Show the flag country in reviews

I want to show the flag country when customers leaves reviews
I did this:
added a row in db table review called country_id
in catalog/model/catalog/review.php in public function addReview( I added :
, country_id = '" . (int)$this->config->get('config_country_id') . "' so it stores the user's country_id if he is logged in
Now how can I call it and bring the image flag from Image/flag and place in review.tpl?
There is a topic in http://forum.opencart.com/viewtopic.php?t=58425 where nobody could give a solution
Any help?
Now that You have stored the country_id, You can simply load the concrete country based on that id when loading the reviews (for each review, respectively).
When the reviews are loaded from DB, in catalog/controller/product/product.php in method review() the array of results is looped through:
foreach ($results as $result) {
$this->data['reviews'][] = array(
'author' => $result['author'],
'text' => $result['text'],
'rating' => (int)$result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$review_total),
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
);
}
Now You only need to load the country to get it's code for flag, so You could end up with something like this:
$this->load->model('localisation/country');
foreach ($results as $result) {
$country = $this->model_localisation_country->getCountry($result['country_id']);
$this->data['reviews'][] = array(
'author' => $result['author'],
'text' => $result['text'],
'rating' => (int)$result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$review_total),
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'flag' => strtolower($country['iso_code_2']) . '.png'
);
}
Now the flag index should contain value like de.png, gb.png, fr.png etc. Just make sure You type the right value to the flag images in the template and that You have the flag images with such a names...

Categories