Корректировка цен на категории и подкатегории ie - woocommerce - PullRequest
0 голосов
/ 19 февраля 2020

Я нашел этот кусок кода, и он прекрасно работает. Я хотел бы видеть, что это также работает на подкатегориях. Я надеюсь, что кто-то может помочь мне с этим.

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'conditional_custom_price', 99, 2 );
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'conditional_custom_price', 99, 2 );
function conditional_custom_price( $price, $product ) {
    global $post;
    if( has_term( 'test', 'product_cat', $post->ID )){
        // Delete product cached price  (if needed uncomment it)
        // wc_delete_product_transients($product->get_id());

        $price = $price * 2.1;
    }
    return $price;
}

1 Ответ

1 голос
/ 20 февраля 2020

Ок, нашел ответ.

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'conditional_custom_price', 99, 2 );
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'conditional_custom_price', 99, 2 );

function conditional_custom_price( $price, $product ) {
    $term_slug = 'test';
    $taxonomy  = 'product_cat';
    $term_id   = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // Get the term ID
    $child_ids = get_term_children( $term_id, $taxonomy ); // Get the children terms IDs
    $terms_ids = array_merge( $child_ids, [$term_id] ); // an array of all term IDs (main term Id and it's children)

    if ( has_term( $terms_ids, $taxonomy, $product->get_id() ) ) {
        // Delete product cached price  (if needed uncomment it)
        // wc_delete_product_transients($product->get_id());

        $price = $price * 2.1;
    }
    return $price;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...