tisdag 22 september 2020

Programlanmış Eylemleri WooCommerce Eylem Zamanlayıcısından Kaldırma

WooCommerce Eylem Zamanlayıcı'da bulunan Zamanlanmış Eylemler, biriken binlerce eylemle (başarısız, iptal edilmiş, beklemede veya tamamlanmış) durumda takılıp kalabilir. Bu, özellikle wp_actionscheduler_actions ve wp_actionscheduler_logs tablolarında şişirilmiş veritabanı tablolarına neden olabilir.

Başarısız, iptal edilmiş veya tamamlanma durumları zaten geçtiğinden, bunları wp_actionscheduler_actions tablosundan güvenle kaldırabilirsiniz. Bunu phpMyadmin SQL sekmesinde yapabilirsiniz.


SQL komutunda gerekli olan kodlar:
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'canceled'
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'failed'
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'complete'
wp_actionscheduler_logs tablosu şişmişse, boşaltın.



fredag 3 juni 2016

WooCommerce'de /ürün ve /ürün-kategorisini kaldırmak

WooCommerce'de cikan kalıcı bağlantılarda /ürün ve /ürün-kategori linklerini kaldırmak icin asagikdaki kodu temanizin function.php dosyasina yapistirin.

add_action('init','change_category_base');



function change_category_base( $args ){
        $define = true;
        global $wp_rewrite, $wp_taxonomies;
        //change rewrite slug value for the taxonomy
        if( isset( $wp_taxonomies['product_cat'] ) )
            $wp_taxonomies['product_cat']->rewrite['slug'] = '';

        if( isset( $wp_rewrite->extra_permastructs['product_cat'] ) ){
            if( array_key_exists( 'struct', $wp_rewrite->extra_permastructs['product_cat'] ) ) $wp_rewrite->extra_permastructs['product_cat']['struct'] = '%product_cat%';
            else $wp_rewrite->extra_permastructs['product_cat']['0'] = '%product_cat%';
        }
        if ($define) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules( false );
        }
}

add_filter('rewrite_rules_array', 'create_category_rules');
function create_category_rules($rules){

        //These rules are generated from changing the product category base to nothing and conflict with wordpress pages
        unset( $rules['(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$'] );
        unset( $rules['(.+?)/(feed|rdf|rss|rss2|atom)/?$'] );
        unset( $rules['(.+?)/page/?([0-9]{1,})/?$'] );
        unset( $rules['(.+?)/?$'] );

        //Generate rules for each product category
        $args = array( 'hide_empty' => 0 );
        $categories = get_terms( 'product_cat', $args );

        $category_rules = array();
        foreach( $categories as $category ){

            $link = str_ireplace(site_url("/"), "", rtrim(get_term_link($category), "/"));

            $category_rules["($link)/feed/(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?product_cat=$matches[1]&feed=$matches[2]';
            $category_rules["($link)/(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?product_cat=$matches[1]&feed=$matches[2]';
            $category_rules["($link)/page/?([0-9]{1,})/?$"] = 'index.php?product_cat=$matches[1]&paged=$matches[2]';
            $category_rules["($link)/?$"] = 'index.php?product_cat=$matches[1]';

        }

        $rules = $category_rules + $rules;


    return $rules;
}



remove_filter( 'post_type_link', 'wc_product_post_type_link' );

function wc_custom_product_post_type_link( $permalink, $post ) {
    // Abort if post is not a product
    if ( $post->post_type !== 'product' )
        return $permalink;

    // Abort early if the placeholder rewrite tag isn't in the generated URL
    if ( false === strpos( $permalink, '%' ) )
        return $permalink;

    // Get the custom taxonomy terms in use by this post
    $terms = get_the_terms( $post->ID, 'product_cat' );

    if ( empty( $terms ) ) {
        // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
        $product_cat = _x( 'uncategorized', 'slug', 'woocommerce' );
        $product_sub_cat = '';
    } else {
        // Replace the placeholder rewrite tag with the first term's slug
        $first_term = 0;
        foreach ( $terms  as $term) {
            if ($term->parent == '' ) {
                $first_term = $term;
                break;
            }
        }
        if($first_term){
            $product_cat = $first_term->slug;
            $termchildren = get_term_children( $first_term->term_id, 'product_cat' );
            $child_term = get_term( array_shift($termchildren), 'product_cat' );
            $product_sub_cat = $child_term->slug;
        }
    }

    $find = array(
        '%year%',
        '%monthnum%',
        '%day%',
        '%hour%',
        '%minute%',
        '%second%',
        '%post_id%',
        '%category%',
        '%product_cat%',
        '%product_sub_cat%'
    );

    $replace = array(
        date_i18n( 'Y', strtotime( $post->post_date ) ),
        date_i18n( 'm', strtotime( $post->post_date ) ),
        date_i18n( 'd', strtotime( $post->post_date ) ),
        date_i18n( 'H', strtotime( $post->post_date ) ),
        date_i18n( 'i', strtotime( $post->post_date ) ),
        date_i18n( 's', strtotime( $post->post_date ) ),
        $post->ID,
        $product_cat,
        $product_cat,
        $product_sub_cat
    );

    $replace = array_map( 'sanitize_title', $replace );

    $permalink = str_replace( $find, $replace, $permalink );

    return $permalink;
}
add_filter( 'post_type_link', 'wc_custom_product_post_type_link', 10, 2 );


function get_slugs($id) {
  remove_filter( 'post_link', 'custom_permalinks_post_link', 'edit_files', 2 ); // original hook
  remove_filter( 'post_type_link', 'custom_permalinks_post_link', 'edit_files', 2 );
  $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
  add_filter( 'post_link', 'custom_permalinks_post_link', 'edit_files', 2 ); // original hook
  add_filter( 'post_type_link', 'custom_permalinks_post_link', 'edit_files', 2 );
  return $originalPermalink;
}


function on_save_post($id) {
$character_lenght = "6";
$permalink = get_permalink($id);
if (strpos($permalink,'/product/') == false) {
return;
}
 
  delete_post_meta( $id, 'custom_permalink' );
 $postn = get_post($id);
$slugnew = get_permalink($id);
$ogslug = substr($slugnew, strpos($slugnew, "co.uk/") + $character_lenght);
  $original_link = get_slugs($id);
  $permalink_structure = get_option('permalink_structure');
$terms = get_the_terms( $id, 'product_cat' );
$terms2 = get_the_terms( $id, 'product_cat' );
if (! (empty($terms) and empty($terms2))) {
foreach ($terms as $term) {
    $pcategory = $term->slug;
   break;
}


foreach ($terms2 as $term1) {
    $scategory = $term1->slug;


}
}
if ($scategory !== $pcategory) {
 $add_sub = str_replace('product/', '', $ogslug);
 $new_permalink = str_replace($pcategory, 'tyinput', $add_sub,$count1);
 $new_permalink = str_replace('tyinput', $pcategory.'/'.$scategory, $new_permalink,$count);
} else {

 $new_permalink = str_replace('product/', '', $ogslug);
}


  if ("1" == "1") {
      add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($new_permalink),"/"))) );
  }
}
add_action('save_post','on_save_post'); 

Ikinci alternatif:
Remove /product/ or /shop/ in URL

/*
* Remove /product/ or /shop/ ... support %product_cat%
* Author: levantoan.com
*/
function devvn_remove_slug( $post_link, $post ) {
    if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
        return $post_link;
    }
    if('product' == $post->post_type){
        $post_link = str_replace( '/product/', '/', $post_link ); //replace "product" to your slug
    }else{
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 );
 
function devvn_woo_product_rewrite_rules($flash = false) {
    global $wp_post_types, $wpdb;
    $siteLink = esc_url(home_url('/'));
    foreach ($wp_post_types as $type=>$custom_post) {
        if($type == 'product'){
            if ($custom_post->_builtin == false) {
                $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
                            FROM {$wpdb->posts}
                            WHERE {$wpdb->posts}.post_status = 'publish'
                            AND {$wpdb->posts}.post_type = '{$type}'";
                $posts = $wpdb->get_results($querystr, OBJECT);
                foreach ($posts as $post) {
                    $current_slug = get_permalink($post->ID);
                    $base_product = str_replace($siteLink,'',$current_slug);
                    add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');
                }
            }
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_woo_product_rewrite_rules');
/*Fix 404*/
function devvn_woo_new_product_post_save($post_id){
    global $wp_post_types;
    $post_type = get_post_type($post_id);
    foreach ($wp_post_types as $type=>$custom_post) {
        if ($custom_post->_builtin == false && $type == $post_type) {
            devvn_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'devvn_woo_new_product_post_save');


Remove product-category in URL

/*
* Remove product-category in URL
* Author: levantoan.com
*/
add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 );
function devvn_product_cat_permalink( $url, $term, $taxonomy ){
    switch ($taxonomy):
        case 'product_cat':
            $taxonomy_slug = 'product-category'; //Change product-category to your product category slug
            if(strpos($url, $taxonomy_slug) === FALSE) break;
            $url = str_replace('/' . $taxonomy_slug, '', $url);
            break;
    endswitch;
    return $url;
}
// Add our custom product cat rewrite rules
function devvn_product_category_rewrite_rules($flash = false) {
    $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'post_type' => 'product',
        'hide_empty' => false,
    ));
    if($terms && !is_wp_error($terms)){
        $siteurl = esc_url(home_url('/'));
        foreach ($terms as $term){
            $term_slug = $term->slug;
            $baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
            add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top');
            add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top');
            add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top');
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_product_category_rewrite_rules');
/*Fix 404 when creat new term*/
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 );
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) {
    devvn_product_category_rewrite_rules(true);
}