I'm trying to do a WP plugin that will show PrestaShop products randomly from the whole database and from one specified category.
I need to re-construct the image path for a product having the product id only.
I have tried an SQL query but isn't helping me to construct the right path to the right image for the right product.
SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend` FROM [DB_PREFIX]product p
LEFT JOIN `[DB_PREFIX]product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = [ID_LANG])
LEFT JOIN `[DB_PREFIX]image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `[DB_PREFIX]image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = [ID_LANG])
The absolute path of the pictures is: website.com/img/p/
then the number in the url gives the path to the picture.
So with website.com/5-medium_atch/product-name.jpg the path of the picture is
website.com/img/p/5/5-medium_atch.jpg
and for website.com/145-home_atch/product-name.jpg the path is
website.com/img/p/1/4/5/145-home_atch.jpg
I'm using the prestashop version 1.5.4.1 and WP 3.5.2
Any suggestion is welcomed. Thanks.
I finally do that:
Get these from plugin settings:
$product_name = $PrestaShopdb->get_var('SQL here');
$store_url
$image_folder
$id_image = $PrestaShopdb->get_var('
SELECT i.`position` FROM ps_product p
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.$language.')
LEFT JOIN `ps_image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `ps_image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.$language.')
WHERE p.`active` = 1
AND p.`id_product` = '.$product_id.''
);
$image_path = $image_folder . "/". $product_id . "/".$id_image."/" . $product_id.$id_image."-" . $image_size."_default.jpg";
if(!url_exists($image_path)){ //check if file exist? Function is below
$image_path = $image_folder . "/". $product_id ."/" . $product_id."-" . $image_size."_default.jpg";
}else{
$image_path = $image_path;
}
function url_exists($url) {
// Version 4.x supported
$handle = curl_init($url);
if (false === $handle)
{
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$connectable = curl_exec($handle);
curl_close($handle);
return $connectable;
}
It looks more like a temporary solution to my question but do the job for now.
I think the best way to find image folder path in prestashop
$oImage = new Image($iImg_id);
$sPath = _PS_PROD_IMG_DIR_.$oImage->getExistingImgPath().'.'.$oImage->image_format;
Related
Second add_action function is not working but I am not getting any error. if I take the second add_action out of the function it works outside but since I want it to work on specific pages I can't work with that.
Do you have any suggestion how may I fix this problem?
add_action('current_screen', function ($current_screen) {
if ('smart_board' == $current_screen->post_type && 'post' == $current_screen->base) {
add_action('add_attachment', function ($post_ID) {
$file = get_attached_file($post_ID);
$path = pathinfo($file);
$file_slugified_name = $path['filename'];
$file_prefix = 'smart_board_';
$file_dir = $path['dirname'];
$file_extension = $path['extension'];
$newfilename = $file_prefix . $file_slugified_name;
$newfile = $file_dir . "/" . $newfilename . "." . $file_extension;
rename($file, $newfile);
update_attached_file($post_ID, $newfile);
});
}
});
Hi all I am a bit stumped it seems after the latest update to wordpress this function has stopped working. Any ideas would be gratefully received . . .
First snippet gets the parameter "we" and should then auto fill the form field id #input_2_4 after making some calculations.
`<script>
$.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)')
.exec(window.location.href);
return results[1] || 0;
}
var we = parseInt( $.urlParam('we') );
$('#input_2_4').val( '£' + (we * 0.1450).toFixed(2));
</script>`
Everything seems present in the url string including &we=1685 value but field #input_3_4 is now blank.
Link
I'd try it like below to remove any $ issues. Console.log should let you know if the issue is in getting the URL parameters. You may need to look at enqueueing the script as well to make sure it's getting loaded in the proper order.
jQuery.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)')
.exec(window.location.href);
console.log(window.location.href)
console.log(results[1] || 0);
return results[1] || 0;
}
var we = parseInt( jQuery.urlParam('we') );
jQuery('#input_2_4').val( '£' + (we * 0.1450).toFixed(2));
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();
}
}
I'm creating nodes using a custom modules
$node = new stdClass();
$node->type = $link['content_type'];
node_object_prepare($node);
$node->uid = $user->uid;
$node->name = $user->name;
$node->title = $html['title'];
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = $html['html'];
$node->body[$node->language][0]['summary'] = $html['summary'];
$node->body[$node->language][0]['format'] = 'filtered_html';
$node->menu['enabled'] = 0; // 1 to enable providing a link in main menu
$node->menu['link_title'] = urlencode($html['title']);
$node->menu['description'] = urlencode($html['summary']);
$node->menu['parent'] = 'main-menu:0';
$node->menu['weight'] = 5;
$node->path['alias'] = urlencode($html['title']) . time();
$node->comment = 1;
$node->status = 1; // 1 means published
$node->promote = 0;
$node->revision = 0;
$node->changed = $_SERVER['REQUEST_TIME'];
$node->created = $_SERVER['REQUEST_TIME'];
node_submit($node);
#node_save($node);
$node->path['alias'] .= '+' . $node->nid;
node_submit($node);
#node_save($node);
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
But now I need to assign each node a I create a workbench section, so I tried doing this:
$node->workbench_access = array('66');
node_submit($node);
#node_save($node);
$node->path['alias'] .= '+' . $node->nid;
node_submit($node);
#node_save($node);
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
This add the workbench access id temporarily, but when the page is refreshed it doesn't apply it. Is there a way to assign a node to a workbench section using php?
I just installed this module for the first time today funnily enough, it looks good :-)
Having a look at the workbench_access_node_insert() function (in the workbench_access.module file) it looks like the node object key it's looking for is workbench_access_id, not workbench_access.
Also you need to provide an access scheme (either menu or taxonomy depending on what access scheme you've chosen at admin/config/workbench/access/settings). I think your code should look a bit like this:
$node->workbench_access_scheme['access_scheme'] = 'taxonomy'; // or 'menu'
$node->workbench_access_id = array('66');
That's untested but looking at the module file it should work.
The following line didn't work for me.
$node->workbench_access_id = array('66');
It worked when I changed it to
$node->workbench_access = array('66');
How to get list of terms of a node ( by node id) belongs to a particular vocabulary. Is there any drupal function ?
taxonomy_node_get_terms function.
http://api.drupal.org/api/function/taxonomy_node_get_terms/6
Or also:
taxonomy_node_get_terms_by_vocabulary
http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary/6
I know there is api for getting list of vocabularies But i am nto sure, one api exist for gettign list of terms of vocabularies.
However, you can try this function. It will work.
function myutils_get_terms_by_vocabulary($vname, $tname = "") {
$sql = "select td.*
from term_data td
inner join vocabulary v on td.vid = v.vid
where v.name = '%s'";
if($tname) {
$result = db_query($sql . " and td.name = '%s'", $vname, $tname);
return db_fetch_object($result);
} else {
$result = db_query($sql, $vname);
}
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->tid] = strtolower($term->name);
}
return $terms;
}
Basically i created a 'myutils' module for such common functions and added this function there. so that i can use them in all similar scenarios.