Manipulate WooCommerce category description output - php

I have around 4000 categories in WooCommerce and I don't have the time to go through every category and manually update the category descriptions. I am wondering if it would be possible to output a generic category description based on the H1 tag?
For example:
if the title is 'Ford Focus Car Spares'
The category description would autofill to something like:
'Buy Ford Focus Car Spares - We stock a huge range of Ford Car Spares'
I would need a way of programmatically removing the keywords from the h1 title and outputting them into the description.
Thankyou to anybody that can help.

You can hook the display of the title of your category page
add_action( 'woocommerce_archive_description', 'custom_category_description', 10, 2);
function custom_category_description($woocommerce_taxonomy_archive_description, $int) {
if ( is_product_category()) {
$title_cat = get_the_title($int);
echo 'Buy '.$title_cat.' - We stock a huge range of '.$title_cat;
}
}
I haven't tested this part of code but this is what I will explore

Related

add_action to specific page type or post ID on WordPress

I am using the following WooCommerce shortcode to output product data from a certain category on a page:
<?php echo do_shortcode('[products category="'. $category .'" columns="1"]'); ?>
By default, this code does not include the product short description, but I can add the following to the functions.php file to enable the short description:
add_action('wc_single_excerpt' , 'woocommerce_template_single_excerpt', 10 , 2);
However, this will then output the product short description on every single WooCommerce product loop which I don't want to do.
My question is, how can I ONLY output the product short description i.e. the add_action on a page that has an ID of 123 for example?
call this function woocommerce_template_single_excerpt conditionally:
add_action('wc_single_excerpt' , function() {
// if it's in a loop
if( get_the_ID() == 123 ) {
woocommerce_template_single_excerpt();
}
}, 10 , 2);
let me know if it answers your question

woocommerce-products-header in Subcategory - Remove Header Image

I have searched around but can't seem to find exactly what I am looking for. I have subcategories set up on my store, and images assigned to these subcategories, as I want the thumbnails to be displayed when you click into a main category .
However, when I then click into the subcategory page, to display products within that subcategory, the image I set for the thumbnail appears under the description, quite large.
I don't want the subcategory image on the product list page whatsoever. Is there anything within the woocommerce code I can change or add to make this happen? This is what it looks like now... I want it to look like this
Thanks in advance!
Using functions.php:
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 );
add_action( 'woocommerce_before_subcategory_title', 'custom_subcategory_thumbnail', 10 );
function custom_subcategory_thumbnail( $category ) {
if ( $category->parent == '0' ) {
woocommerce_subcategory_thumbnail( $category );
} else {
// do not show a category image
}
}
Using CSS:
.tax-product_cat ul.products li.product a img {
display:none
}
Note: this code was found here.

How can I call programmatically a product description in Magento 1.7.0.2?

I am new in Magento 1.7.0.2
I would like to make some custom html divs inside the product's phtml page and to call
1) the product description ,
2) another div with product tags and
3) another div with reviews of this specific product.
4) another div which will contain a specific cms page (ask about this product)
Do you know how should I write php inside each div in order to call these
specific attributes of a product in Magento 1.7.0.2 ?
Thank you very much
here i am giving your code to do with your above specification you can set it in to your html
1. Product description
$_product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
2. product tags
$model=Mage::getModel('tag/tag');
$tags= $model->getResourceCollection()
->addPopularity()
->addStatusFilter($model->getApprovedStatus())
->addProductFilter(PRODUCT_ID)
->setFlag('relation', true)
->addStoreFilter(Mage::app()->getStore()->getId())
->setActiveFilter()
->load();
if(isset($tags) && !empty($tags)):
foreach($tags as $tag):
echo '<span class="tag">'.$tag->getName().'</span>';
endforeach;
3. Product Reviews
$productId = $product->getId();
$reviews = Mage::getModel('review/review')
->getResourceCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addEntityFilter('product', $productId)
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
4.contain a specific cms page
I would like to suggest you if you want to display product specific content you can create block for product and in phtml file you can call like below
echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-block-id')->toHtml();
Also you can refer detail page
hope this will sure help you.

woocommerce product with multiple categories, active category in sidebar

I need some food for thought on this because I have had trouble finding a decent solution here.
There is a woocommerce shop, categories on the left side (sidebar) and on the right side products. A product can have multiple categories; for example, the product "Burger" is in the categories "Food" and "Fastfood".
Now on the left side we have the categories "Food" and "Fastfood" with both "Burger" in them.
I go to category "Food". On the left side in the sidebar I highlight the category food with:
(start loop going through categories)
if(get_query_var('product_cat') == $cat->slug) - echo <li class="active">
else - echo <li class="active">
(end loop)
so the category "Food" is highlighted. Then I select the product "Burger".
Now, because the product "Burger" is also in the category "Fastfood", the category "Fastfood" is displayed as active.
The active category should be based on the category that the product was selected from originally ("Food"). If you go to the category "Fastfood" and select "Burger", then "Fastfood" should be active. Open "Burger" from "Food", "Food" should be the active category.
My attempt is to change the category in the permalink (<?php the_permalink(); ?>) to the current category when displaying all the products, but it just doesn't feel like the right way.
Maybe someone has a better solution.
Wish you a good day!
EDIT:
I've managed to create the permalink with the right category:
$custom_permalink = get_settings('siteurl')."/".get_query_var('product_cat')."/".basename(get_permalink());
Sadly this does not work.. (redirects me to the "wrong" category) but I don't like this hack anyways :)..
I got it working. This is what I did:
In your custom template file /yourtheme/woocommerce/content-product.php you will change the a href.
Code that generated the new permalink (using the current selected category):
// HOOK FOR CORRECT ACTIVE SIDEBAR ELEMENT WHEN PRODUCT HAS MULTIPLE CATEGORIES
if(get_query_var('product_cat') == ""){
$product_categries = get_the_terms( $post->ID, 'product_cat' );
foreach ($product_categries as $category) {
$cur_cat = $category->slug;
}
$custom_cat = $cur_cat;
}else{
$custom_cat = get_query_var('product_cat');
}
$custom_permalink = get_permalink(5).$custom_cat."/".basename(get_permalink());
I also check if a category is set (because there is none if you are viewening "All Products", in this case I loop through the Products and get their category to use it in the permalink).
The get_permalink with the id 5 is basically my shop-page, so it stays dynamic when you change the permalink from your shop. Don't like hardcoded stuff here because it's already an ugly hack.
Place this code before the a href that wraps your Product, in my case this was on line 42 (Woocommerce V2.0.10).
Then change this a href from
<a href="<?php the_permalink(); ?>">
to
<a href="<?php echo $custom_permalink; ?>">
I hope this will help you when you face the same issue!

How to sort products into categories Magento

I have toons of products in a magento store thats need to be
sorted into new categories based on diffrent attributes any
one knows how to do this with a script.
example:
if the product attribute "COLOR" has a value of Blue the product should be in cat 10
and so on
Once you iterate over your products you can use the setCategoryIds method (this code is meant to be used for a dropdown attribute as it uses the getAttributeTextMethod):
$product->load($productId);
if( strcmp($product->getAttributeText('COLOR') , 'Blue') == 0 ){
$product->setCategoryIds(array('cat1Id' ,'cat2Id'));
}
$product->save();

Categories