I have a category named Z and a subcategory named A.
In my function, I am using this to get category Z (Parent)
$category[0]->slug
The problem is that subcategory A is set as [0] because it is alphabetical order. Z is the parent category, so Z should be [0]. I don't get why it is in this way.
I recommend to use this wordpress function to do that:
//Load data for your Sub-Category
$subcategory = get_category("A");
//from Sub-Category get parent ID ( Z )
$parent_category_ID = $subcategory->parent;
//load object for Parent-Category ( Z )
$objet_parent_category = get_category($parent_category);
//Get the Parent Category slug
$parent_name = $parent_name->slug; // to get name: ->name
I hope help you!
Related
the issue
When I define navigation on a child of a Category member I get the following MDX error when I click on a member:
[Stats].[Top X].[Total].[subtotal].[<child of subtotal category]' is not a known MDX entity
this is the set-up
I have simplified the MDX and transferred it to run on the default Sales cube:
set [level] as [Customers].[Geography].[City]
set [selection] as
filter([level],mid([level].currentmember.name,1,1) = 'A')
+ filter([level],mid([level].currentmember.name,1,1) = 'B')
+ filter([level],mid([level].currentmember.name,1,1) = 'C')
/* MDX + function to create a dynamic hierarchy */
CATEGORY HIERARCHY [Stats].[ABC], DEFAULT_MEMBER_NAME = "Total",LEVEL_NAME_PATTERN="L - TOP X - ${levelDepth}"
CATEGORY MEMBER [Stats].[ABC].[Total].[subtotal] as [selection] , ADD_CHILDREN=true
CATEGORY MEMBER [Stats].[ABC].[Total].[remainder] as except([level],[selection]),ADD_CHILDREN=false
SELECT
{[Measures].[amount]} on 0,
{[Stats].[ABC].[total].[subtotal].children
,[Stats].[ABC].[total].[subtotal]
,[Stats].[ABC].[total].[remainder]
,[Stats].[ABC].[total]} on 1
FROM [sales]
In the table widget, I have defined the navigation as:
click behavior: Drilldown
Drilldown Strategy: mdxExpression
Drilldown axis: Rows
MDX Expression:
when $member.name = 'subtotal' then ic3drilldownStop()
when $member.name = 'total' then ic3drilldownStop()
when $member.level is [Time].[Calendar].[Month] then ic3drilldownStop()
else nonempty([Time].[Calendar].[Month],[measures].[amount])
end
Result is
Now clicking on, for example, Bogota gives:
Error:
[Stats].[Top X].[Total].[subtotal].[Bogotá]' is not a known MDX entity
the question
How to solve this error?
The issue is because you're trying to use a category member defined in the SELECT statement itself. As a workaround you could modify your drilldown MDX expression as following:
non empty
case
when $member.name = 'subtotal' then ic3drilldownStop()
when $member.name = 'total' then ic3drilldownStop()
when $member.level is [Time].[Calendar].[Month] then ic3drilldownStop()
else nonempty([Time].[Calendar].[Month],[measures].[amount])
end
axis 0 ([Measures].[Amount], $member)
As you can see the expression is not only redefining the axis 1 but the axis 0 as well to mimic the "Filter by" option (i.e., filtering by the Bogota member in your example).
How I can get variation parent product ID.
EXAMPLE:
I have product with ID 35
and this product has two variations colors - red (ID 351), black (ID 352)
My code:
$product = wc_get_product(get_the_ID()); //get_the_ID() is ID 351 and I need this parent ID 35
The proper way
As LoicTheAztec suggested in comments, you should use this:
$parent_product = wc_get_product($product->get_parent_id());
The reason why you should retrieve parent product via get_parent_id() is that it will trigger hook woocommerce_product_variation_get_parent_id and it will be easily modifiable by other plugins/themes:
add_filter('woocommerce_product_variation_get_parent_id', function($value, $wc_data) {
// ...
return $value;
}, 10, 2);
This will also work, but it won't trigger WC-specific hooks:
$parent_product_id = wp_get_post_parent_id($product->get_id());
$parent_product = wc_get_product($parent_product_id);
Old answer
Note: That's not working outside the loop and will always return 0 if you attempt to substitute get_the_ID() with $product->id - in that case use $product->get_id() as in the example above.
Use wp_get_post_parent_id, as variations have their parent as the product itself.
Example:
$variation_id = get_the_ID();
$product_id = wp_get_post_parent_id($variation_id);
Never use WC_Product::get_parent():
$parent_product = $product->get_parent(); // will always return '0
i am getting 'object' has no attribute 'objects'. Here is my code from views.py
def product_list(request, category_slug=None):
category = None
categories = category.objects.all()
products = Product.object.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,'shop/prod/list.html' , {'category':category,
'categories': categories,
'products': products
})
def product_detail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
cart_product_form = CartAddProductForm()
return render(request,
'shop/prod/detail.html',
{'product': product,
'cart_product_form':cart_product_form })
category = None
categories = category.objects.all()
You are setting category to None then immediately setting categories to a property of category, which can only be an error because None in python is the absence of a value.
You can check out more information on the official python docs here:
https://docs.python.org/3/library/constants.html#None
I am trying to make ACF data available on child pages two levels under the parent. I have a solution for making it available to a child page:
if ( $post->post_parent ) {
$headingFont = get_field('community_heading_font', $post->post_parent);
$bodyFont = get_field('community_body_font', $post->post_parent);
$primaryColor = get_field('community_primary', $post->post_parent);
$secondaryColor = get_field('community_secondary', $post->post_parent);
$fifteenSecondaryColor = get_field('community_fifteen_secondary', $post->post_parent);
$tertiaryColor = get_field('community_tertiary', $post->post_parent);
}
However, this information isn't available once we're a level deeper. That is, the ACF field 'community_heading_font' isn't available to the grandchild of the page originally providing data for that field.
I've tried post->post_parent->post_parent, and I've also tried to use post->post_parent on a variable:
$parentPage = $post->post_parent;
$grandparentPage = $parentPage->post_parent
To get the $grandparentPage ID for use in your ACF functions, use the wp_get_post_parent_id() function.
$grandparentPage = wp_get_post_parent_id($post->post_parent);
$headingFont = get_field('community_heading_font', $grandparentPage);
https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id
Hi
Suppose I have taxonomy "test", and under this taxonomy, I have terms "test2" and "test3". I want to loop all terms under taxonomy "test'. I found a api function "taxonomy_get_children" that can get all children (reference: http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/function/taxonomy_get_children/6), my code didn't works, could someone give me a idea?(I am using Drupal 6)
Thank you
<?php
$a = taxonomy_get_children(1, $vid, 'tid');
print_r($a);
?>
Are you sure your tid = 1? Or is your $vid = 1
tid = term id (the actual term in the vocabulary)
vid = vocab id (the vocabulary where the term exists)
Typically, if you know your vocabulary id is 1, youd call it like this:
$a = taxonomy_get_children($tid, 1);
print_r($a);
Or if you don't know the vid, just pass in the $tid
$a = taxonomy_get_children($tid);
print_r($a);