Get Category Description in my cutom phtml file in magento 2 - categories

I am using Magento 2. I want to get category description and image in my custom phtml file.
How can i do that?

If you want to get in current category information in phtml you can use following code.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentCategory = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category information
echo $currentCategory->getId();
echo $currentCategory->getName();
echo $currentCategory->getDescription();
echo $currentCategory->getImageUrl();
?>
Or do you want to load any specific category then you can use below code.
$categoryId = 3;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')
->load($categoryId);
echo $category->getName();
echo $category->getImageUrl();

Related

Set attribute value in magento 2

I have created custom attribute named my_shipping_charge in magento 2 programmatically. I set default value '0' for this attribute. It works fine when I create new product. But what if I want to set this attribute for already created product what should I do.? Please help me to solve this problem.
For already created products we should have to update manually, if the product collection is larger can run a file in root.
In this root file we can load all the product collection and set the custom attribute value for all the products and save it.
custom file in root folder will be like below :
<?php
use \Magento\Framework\App\Bootstrap;
require __DIR__ . "/app/bootstrap.php";
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$instance = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product_collections = $instance ->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collections = $product_collections->create();
$shippingCharge = "custom value";
foreach ($collections as $product) {
$product->setMyShippingCharge($shippingCharge);
$product->save();
}
?>
$shippingCharge will be the custom value have to update.
Run the root file in terminal and reindex. And check from admin panel

In Magento 2 how can I retrieve a container from layout?

How can I retrieve a container from the layout programmatically?
I would like to be able to do something like the following...
$container = $layout->getContainer('name');
$container->setAttribute('htmlClass', 'class');
So I'll be that guy, try your best not to use the object manager directly. To use or not to use the ObjectManager directly?
<?= $block->getLayout()->renderNonCachedElement('top-right-wrapper'); ?>
-or-
<?= $this->getLayout()->renderNonCachedElement('top-right-wrapper'); ?>
Both above will work but using $this is discouraged. Magento 2 Templates: Use $block or $this?
With little debugging in core I have found that there is a method which
returns any container,block or UIcomponent html using name as parameter.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$layoutObj = $objectManager->get('Magento\Framework\View\Layout');
$html = $layoutObj->renderNonCachedElement('top-right-wrapper');
echo $html;
* Replace top-right-wrapper with your block,container or UIcomponent name.

Facebook API images not downloading

First post here,
i am developing a app to fetch and download facebook images from user's account.
I have a basic code till now as follows..
Need help as to why only the 1 file keeps downloading alone...in the zip i create
it is the only image without the ?oh= concatenation, all others with the oh parameter dont get
downloaded...
<?php
set_time_limit(0);
$url='https://graph.facebook.com/me/photos/uploaded?access_token=CAACEdEose0cBAEGw8R4QOGfN3bfX8qtuZCCy8FX6g5Sil8CKaYlp9x8lZAiNFEGJXhh9MYOWdZAJR1x2M627rc5I3n3AauVe8nJtYBsehjUZAljMLkQMMxP03VVXknZBa81Nk1S23z7SnyTkIlgt2oZC1euoFNCTuGesAap1uQUGZAX7LnILlBwyj3V7IVo9QQCNv66Lv8UcA86wYn9jlJE';
$result = file_get_contents($url);
$a=json_decode($result, true);
$files = array();
echo '<div class="col-lg-12">';
$num= count($a['data']);
echo "<table class='table table-hover table-responsive'>";
for($o=0;$o<$num;$o++)
{
$files_to_zip[$o]=$a['data'][$o]['source'];
if($a['data'][$o]['source']!="")
{
echo "<tr><td><img src='".$a['data'][$o]['source']."' id=".$o." width='200' height='200' /></td></tr>";
array_push($files,$a['data'][$o]['source']);
//echo $files_to_zip[$o]."<br/><br/><br/><br/>";
}
}
echo '</table></div><br/>';
?>

How to filter New WP Query by Custom Field Value?

I am creating new pages for each of my categories in wordpress. The post editor has a custom field that allows the selection of a sector type, this gets applied to the post on update. The custom field key is: sector, for custom field meta value options lets use SectorA, SectorB and SectorC. I am using a custom post type called projects.
I followed the advice at this link http://weblogtoolscollection.com/archives/2008/04/13/how-to-only-retrieve-posts-with-custom-fields/
How can I change the query line in the code below so that it filters the loop by a Sector name, lets use SectorA. I'll then reuse the code on each template page changing the value to SectorB and SectorC on the other pages.
I think this needs changing somehow:
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );
Currently it echos the sector value and description value successfully but is showing all the posts. So my attempt to limit it to sectorA using sector=sectorA doesn't seem to work?
This code is in functions.php:
function get_custom_field_posts_join($join) {
global $wpdb, $customFields;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
global $wpdb;
$group .= " $wpdb->posts.ID ";
return $group;
}
And this code is on the Template Page:
<?php /* Begin Custom Field Posts */ ?>
<h2>Custom Posts</h2>
<ul>
<?php
global $customFields;
$customFields = "'sector', 'description'";
$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');
while ($customPosts->have_posts()) : $customPosts->the_post();
$sector = get_post_custom_values("sector");
$description= get_post_custom_values("description");?>
<li><?php echo $sector[0]; ?></li>
<li><?php echo $description[0]; ?></li><br />
<?php endwhile; ?>
</ul>
<?php /* End Custom Field Posts */ ?>
Thanks for your help
May be this what you want
function get_custom_field_posts_join($join) {
global $wpdb, $customSector;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key = 'sector' and postmeta.value = '$customSector') ";
}
and modification of page
$customSector='sectorA';//<--- insert this
add_filter('posts_join', 'get_custom_field_posts_join');
Try using this code.
<?php
$sector = get_post_meta($post->ID, "sector", false);
if ($sector[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<div class="sector">
<h3>Title</h3>
<?php foreach($sector as $sector) {
echo '<blockquote><p>'.$sector.'</p></blockquote>';
} ?>
</div>
<?php } ?>

displaying row details in zend framework

I am simply trying to show detail view page from table page
I have set up displaying list of pages via another function like this and works fine
> <a href="< ? php echo
> $ this -> url(array('controller'=>'page','action'=>'detail',
> 'id'=>$page->id));? >"> < ? php echo
> $this->escape($page->title);? ></a>
In my controller i added
public function detailAction()
{
$id = $this->_getParam('id', 0);
$page = new Application_Model_DbTable_Pages();
$this->view->myVar = $page->getPage($id);
}
and in view
> <?php print_r($this->myvar) ?>
This list
I just see blank page, how can i show detail page ?
Try:
<?php print_r($this->myVar) ?>
insted of myvar --> myVar. That is how You called Your variable in the action method.