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');
Related
I am trying to assign a Channel to my products created programmatically, so they will be displayed on shop. Unfortunately, there is no documentation for this use case.
Here is how I create my products:
$productFactory = $this->container->get('sylius.factory.product');
$productManager = $this->container->get('sylius.manager.product');
$productRepository = $this->get('sylius.repository.product');
$productVariantFactory = $this->get('sylius.factory.product_variant');
$productVariantRepository = $this->get('sylius.repository.product_variant');
$channelPricingFactory = $this->get('sylius.factory.channel_pricing');
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
//CREATE PRODUCT
$product = $factory->createNew();
$product->setName('TEST 2 - '.$title);
$product->setCode($this->generateRandomString());
$product->setSlug($this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName('TEST 2 - '.$title);
$variant->setCode($this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice(999);
$channelPricing->setOriginalPrice(999);
$channelPricing->setChannelCode('US_WEB');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
Unfortunately, the products are not linked to a Channel:
The answer to your question:
You have one product variant, but Product variant implements ProductVariantInterface which extends TranslatableInterface. So we need one translation to make it appear and work. To add the translation:
$productVariant->setCurrentLocale('fr_FR');
$productVariantTranslation = $productVariant->getTranslation();
or
$productVariantTranslation = $productVariant->getTranslation($locale);
after you can add a name or not:
$productVariantTranslation->setName('What a product variant!');
after:
$product->addVariant($productVariant);
$this->entityManager->persist($product);
You will have your product variant on line.
Another thing is that:
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
$channelPricingRepository->add($entity);
it's going to flush directly data to DB in each call. In the example you are going to flush 3 times instead only one. In bigger proccess with many "add" this can be a lack of performance. You can simply;
$this->entityManager->persist($entity); //Many times
$this->entityManager->flush();
Here is how I managed to create my products (to attach them then to my entities):
function createProduct($livre,$options){
$title = $livre['title'];
$prix = $livre['prix'];
// SYLIUS REPOSITORIES LOAD
$productFactory = $options['sylius_factory_product'];
$productRepository = $options['sylius_repository_product'];
$productVariantFactory = $options['sylius_factory_product_variant'];
$productVariantRepository = $options['sylius_repository_product_variant'];
$channelPricingFactory = $options['sylius_factory_channel_pricing'];
$channelPricingRepository = $options['sylius_repository_channel_pricing'];
//CREATE PRODUCT
$product = $productFactory->createNew();
$product->setName($title);
$product->setCode($title.'_'.$this->generateRandomString());
$product->setSlug($title.'_'.$this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName($title. 'Variant');
$variant->setCode($title.'_'.$this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice($prix*100);
$channelPricing->setOriginalPrice($prix*100);
$channelPricing->setChannelCode('CH');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
$productId = $product->getId();
return $productId;
}
I added the channel to the product like this:
$channelCode = 'US_WEB'
$channelRepository = $this->get('sylius.repository.channel');
$channel = $channelRepository->findOneBy(array('code' => $channelCode));
$product->addChannel($channel);
Current I have WP code like this. I need to make it translateable by poedit. How do I wrap the code to make it work? Im not sure which method is use for this case. Some thing like:
<?php my_e( 'Total sales' ); ?> or __('Total sales', 'my')
This is the code. I need to translate ["Sales amount"], ["Number of sales"]
foreach ($results as $result) {
$date = $result->formatted_post_date;
$statistics[$date]["Sales amount"] += $wp_list_table->column_total_sales($result->ID);
$statistics[$date]["Number of sales"]++;
$statistics[$date]["date"] = $date;
$max_number_of_sales = max(array($max_number_of_sales,$statistics[$date]["Number of sales"] )); }
Thank you for help
You have to use __('string','textdomain') to assign a translated string to some variable. And _e('string','textdomain') to echo a translated string. See I18n_for_WordPress_Developers.
Two observations:
you'll not be able to translate array keys, see php.net/manual/en/language.types.array.php
what you're doing seems wrong. I'd do it like:
$sales_amount = 0;
$sales_number = 0;
foreach ($results as $result) {
$sales_amount += $wp_list_table->column_total_sales($result->ID);
$sales_number++;
$date = $result->formatted_post_date;
$statistics[$date]["sales_amount"] = $sales_amount;
$statistics[$date]["sales_number"] = $sales_number;
}
echo __( 'Sales Amount', 'my' ) . $sales_amount;
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;
I am trying to populate nodes by writing a script...i am using following code
$node->field_tags['und'][0]['textfield'] = 'first_tag';
$node->field_tags['und'][0]['textarea'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
whatsits doing is nothing and the term is not being added to vocabulary named tag...if i write the following code it will select the already existed term call xml..i guess because of tid=1;
$node->field_tags['und'][0]['textfield'] = 'first_tag';
$node->field_tags['und'][0]['textarea'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
i even tried the following code
$node->field_tags['und'][0]['name'] = 'first_tag';
$node->field_tags['und'][0]['description'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
can someone please help me how add terms into a already existed vocabulary
Get dynamically vid using below function
$vid = taxonomy_vocabulary_machine_name_load('vocabname');
$terms = new stdClass();
$terms->vid = $vid;
$terms->name='whatever name of your term';
taxonomy_term_save($terms);
Thanks,
Ankush
got it after reading hundreds of line code
$terms = new stdClass();
$terms->vid=1;
$terms->name='success';
$get_tid=new stdClass();
taxonomy_term_save($terms);
term will be save in vocabulary vid=1 with automatic tid....
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.