0

Вопрос заключается в записи значения в произвольное поле по заданному признаку. В качестве признака выступает наличие атрибута с определенным значением. Пробовал так (для вариативного товара), но толку нет:

function woocommerce_product( $price, $_this) {
    if ( 'product_variation' === $_this->post_type ) {
        $data = (object) $_this->get_data();
        $parent_id       = $data->parent_id;
        $attribute_name  = get_the_terms($product->get_id(), ‘pa_razmer’);
        $n = 6;
        if ( $attribute_value == '30m') {
            update_post_meta($product_id, 'custom_field', $n);

        }
    }
    return;
}
add_filter( 'woocommerce_product_variation_get_price', 'woocommerce_product', 10, 2);

Где 'custom_field' - название произвольного поля.

UPDATE

function filter_woocommerce_product_variation_get_p( $price, $_this) {
    if ( 'product_variation' === $_this->post_type ) {
       // $data            = (object) $_this->get_data();
        $parent_id       = $data->parent_id;
        $product_id      = 596;
        $attribute_name  = get_the_terms( $product>id, 'pa_razmer');
        $n = 6;
    foreach ( $attribute_name as $value ) {
                   //echo $value->slug;
        if ( $product_id === $parent_id ) {
            if ( $value->slug == '30m' ) {
                update_post_meta($product_id, 'custom_field', $n);


            }
        }
    }
}
    return;

}
9
  • У вас $attribute_value не определена 31 окт 2017 в 12:58
  • да, действительно, а как записать в нее список значений? или список значений уже хранится $attribute_name?
    – user271244
    31 окт 2017 в 13:28
  • а почему не взять и не посмотреть, что там в $attribute_name? 31 окт 2017 в 13:38
  • посмотрел, расстроился - bool(false), для get_the_terms() не передаются аргументы
    – user271244
    31 окт 2017 в 13:39
  • Я же вам показывал в своем ответе, как работать с атрибутами: ru.stackoverflow.com/a/735241/220220 31 окт 2017 в 13:45

1 ответ 1

0

Надо было добавить всего одну строчку в код предыдущего моего ответа:

function filter_738363_woocommerce_product_variation_get_price( $price, $_this) {
    if ( 'product_variation' === $_this->post_type ) {
        $data            = (object) $_this->get_data();
        $variation_price = $data->price;
        $product_id       = $data->parent_id;
        $variation_rrp_price = get_rrp_price( $variation_price, $product_id );
        update_post_meta( $product_id, $data->name, $variation_rrp_price );
        $price = $variation_rrp_price;
    }

    return $price;
}

add_filter( 'woocommerce_product_variation_get_price', 'filter_738363_woocommerce_product_variation_get_price', 10, 2);

Так теперь выглядят произвольные поля товара под названием "Вариативный товар":

введите сюда описание изображения

Подчеркнуты поля, имеющие отношение к вопросу.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.