2

Quelle est la meilleure façon de remplacer la fonction de woocommerce qui est défini dans le répertoire de woocommerce plugins dans une classe. Par exemple je veux changer la fonction start_el déclarée dans la classe WC_Product_Cat_List_Walker. Je peux changer le code de fonction dans le répertoire du plugin par les changements reste jusqu'à ce que je mette à jour le plugin woocomerce. Comment puis-je le remplacer dans mon fichier functions.php?La meilleure façon de passer outre la liste des catégories de WooCommerce Walker

Répondre

4

essayer ...

vous pouvez créer une classe et d'étendre à WC_Product_Cat_List_Walker.

class My_WC_Product_Cat_List_Walker extends WC_Product_Cat_List_Walker { 
    public $tree_type = 'product_cat'; 
    public $db_fields = array ('parent' => 'parent', 'id' => 'term_id', 'slug' => 'slug'); 

    public function start_el(&$output, $cat, $depth = 0, $args = array(), $current_object_id = 0) { 
     $output .= '<li class="cat-item cat-item-' . $cat->term_id; 

     if ($args['current_category'] == $cat->term_id) { 
      $output .= ' current-cat'; 
     } 

     if ($args['has_children'] && $args['hierarchical']) { 
      $output .= ' cat-parent'; 
     } 

     if ($args['current_category_ancestors'] && $args['current_category'] && in_array($cat->term_id, $args['current_category_ancestors'])) { 
      $output .= ' current-cat-parent'; 
     } 

     $output .= '"><a href="' . get_term_link((int) $cat->term_id, $this->tree_type) . '">' . __($cat->name, 'woocommerce') . '</a>'; 

     if ($args['show_count']) { 
      $output .= ' <span class="count">(' . $cat->count . ')</span>'; 
     } 
    } 

} 

utilisent alors comme comme:

add_filter('woocommerce_product_categories_widget_args', 'woorei_product_categories_widget_args', 10, 1); 

function woorei_product_categories_widget_args($args) { 
    $args['walker'] = new My_WC_Product_Cat_List_Walker; 
    return $args; 
} 
3

Tout d'abord, essayez simplement de copier /includes/walkers/class-product-cat-list-walker.php dans votre thème et inclure le nouveau fichier. Il peut être être enfichable et vous pouvez simplement le modifier directement sans aucun autre effort. Je ne suis pas sûr à 100%.

Si cela échoue, alors vous pouvez le faire en filtrant woocommerce_product_categories_widget_args et en fournissant votre propre walker au lieu d'essayer d'éditer WooCommerce. Laissez votre fichier de duplicateur en place, mais renommez tous les noms de classe de WC_Product_Cat_List_Walker à SO_Product_Cat_List_Walker.

Ensuite, dans le champ functions.php de votre thème, dites au widget d'utiliser cette nouvelle classe comme Walker approprié.

function so_32901408_cat_widget($args){ 

     // include new walker class - where ever you saved/named it 
     include_once('wc-class-product-cat-list-walker.php'); 
     // set the name as the new walker for the widget args 
     $args['walker'] = new SO_Product_Cat_List_Walker; 
     return $args; 
} 
add_filter('woocommerce_product_categories_widget_args', 'so_32901408_cat_widget');