Can't add subcategory Woocommerce - woocommerce

When I try to add a subcategory they disappear after a page-refresh. I can add products to them but they do not show up in front and backend.
Any ideas?
I've already "cleared transients" and "recounted the terms" in Woocommerce settings. Both with no luck.

You must add this code to file functions.php of your Wordpress theme:
//Добавляем Подкатегории В Постоянные Ссылки В Woocommerce
remove_filter( 'post_type_link', 'woocommerce_product_cat_filter_post_link', 10, 2 ); // для версии woocommerce ниже 2.0
remove_filter( 'post_type_link', 'wc_product_post_type_link', 10, 2 ); // для версии woocommerce >= 2
add_filter( 'post_type_link', 'woocommerce_subcategory_permalink', 10, 2 );
function woocommerce_subcategory_permalink( $permalink, $post ) {
// Прекращаем работу, если запись не является товаром
if ( $post->post_type !== 'product' )
return $permalink;
// Прекращаем работу, если тег перезаписи местоположения не находится в генерируемой ссылке
if ( false === strpos( $permalink, '%product_cat%' ) )
return $permalink;
// Получаем пользовательскую таксономию, используемую этой записью
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( empty( $terms ) ) {
$permalink = str_replace( '%product_cat%', _x('product', 'slug', 'woocommerce'), $permalink );
} else {
$first_term = array_shift( $terms );
// Получаем иерархическую product_category
$parents = woo_get_term_parents( $first_term->term_id, 'product_cat' );
$permalink = str_replace( '%product_cat%/', $parents, $permalink );
}
return $permalink;
}
if ( ! function_exists( 'woo_get_term_parents' ) ) {
function woo_get_term_parents( $id, $taxonomy ) {
$chain = '';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;
$name = $parent->slug;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) ) {
$chain .= woo_get_term_parents( $parent->parent, $taxonomy);
}
$chain .= $name."/";
return $chain;
} // End woo_get_term_parents()
}

Related

How do i Replace function from wordpress plugins?

I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}

Custom wysiwyg textarea - html text format

I created a custom field. Saving to db is OK. But when I save a page, the text is not saved as formatted but with html tags.
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', esc_attr( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );
When I save the content, it displays it as follows:
<strong>Lorem ipsum</strong> dolor sit amet...
It should display it without html tags. Lorem ipsum must be bold.
You can use following code
function sidebar_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function sidebar_add_meta_box() {
add_meta_box(
'sidebar-sidebar',
__( 'sidebar', 'sidebar' ),
'sidebar_html',
'page',
'advanced',
'default'
);
}
add_action( 'add_meta_boxes', 'sidebar_add_meta_box' );
function sidebar_html( $post) {
wp_nonce_field( '_sidebar_nonce', 'sidebar_nonce' ); ?>
<p>bočný stlpec ak by bolo treba</p>
<?php
$content = sidebar_get_meta( 'sidebar_sidebar' );
$editor_id = 'sidebar_sidebar';
$settings = array(
'media_buttons' => true,
'quicktags' => true,
'tinymce' => true,
'textarea_rows' => 5
);
wp_editor( $content, $editor_id, $settings );
}
function sidebar_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['sidebar_nonce'] ) || ! wp_verify_nonce( $_POST['sidebar_nonce'], '_sidebar_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['sidebar_sidebar'] ) )
update_post_meta( $post_id, 'sidebar_sidebar', stripslashes( $_POST['sidebar_sidebar'] ) );
}
add_action( 'save_post', 'sidebar_save' );

woocommerce - Display Products in View orders page

I'm working on displaying the order products in the View Orders page in the customer account page like this
Can we do this?
Add this to your theme's 'functions.php'.
function add_products_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add products after order total column
if ( 'order-total' === $key ) {
$new_columns['order-products'] = __( 'Products', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );
function add_products_data_my_account_orders_column( $order ) {
//loop through products of the order
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$is_visible = $product && $product->is_visible();
$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p>%s', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );

Edit Title Tag Wordpress tag.php

I'am trying to build an Input field for setting a custom HTML title-tag on tag.php in Wordpress. I archived it on page.php with custom fields. But it have no idea how to get it working on tag.php.
In My Function i have now:
add_theme_support( 'title-tag' );
add_filter( 'document_title_parts', 'orweb_custom_title');
function orweb_custom_title( $title ) {
if ( ! is_singular() ) return $title;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true ));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title['title'] = $custom_title;
}
return $title;
}
function orweb_custom_title_old($title){
if ( ! is_singular() ) return;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title = $custom_title;
}
return $title;
}
add_filter('wp_title', 'orweb_custom_title_old', 10, 2);
Found here: https://orbitingweb.com/blog/custom-seo-friendly-title-tags-wordpress/.

WordPress add filter to wp_get_attachment_link

I need custom class for filter to wp_get_attachment_link. So what I so:
function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );
It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page
I mean I don't need a custom class at this case. Any help please?
And core function for wp_get_attachment_link is:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );
if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
return __( 'Missing Attachment' );
if ( $permalink )
$url = get_attachment_link( $_post->ID );
$post_title = esc_attr( $_post->post_title );
if ( $text )
$link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
$link_text = wp_get_attachment_image( $id, $size, $icon );
else
$link_text = '';
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
So I mean if ( $permalink ) I don't need to add custom class for this function.
Try
function modify_attachment_link( $markup, $id, $size, $permalink ) {
global $post;
if ( ! $permalink ) {
$markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );
That may work

Resources