WooCommerce: как вы добавляете текст перед ценой для определенного продукта (только)? [Дубликат]

Это решение работает с unittest и nose:

#!/usr/bin/env python
import unittest

def make_function(description, a, b):
    def ghost(self):
        self.assertEqual(a, b, description)
    print description
    ghost.__name__ = 'test_{0}'.format(description)
    return ghost


class TestsContainer(unittest.TestCase):
    pass

testsmap = {
    'foo': [1, 1],
    'bar': [1, 2],
    'baz': [5, 5]}

def generator():
    for name, params in testsmap.iteritems():
        test_func = make_function(name, params[0], params[1])
        setattr(TestsContainer, 'test_{0}'.format(name), test_func)

generator()

if __name__ == '__main__':
    unittest.main()
0
задан neilgee 17 September 2014 в 08:16
поделиться

3 ответа

Получил это от поддержки woo - хорошо работает

add_filter( 'woocommerce_get_price_html', 'custom_price_message' );

function custom_price_message($price) { 

global $post;

$product_id = $post->ID;
$my_product_array = array( 799,796,792);
if (in_array($product_id, $my_product_array)) {
    $addedtext = '(Upfront Paid in Full Price)'; 
    return $price . '<br /><span class="price-disclaimer">' . $addedtext . '</span>';
}

else { 
    return $price; 
 } 
}
0
ответ дан neilgee 27 August 2018 в 03:53
поделиться

Не только предыдущие ответы на код не сохраняют статус флажка, но также обновляют все цены, а не только отдельные продукты. Попробуйте этот код ниже:

//add meta box to each product which will take status to add text or not
function add_events_metaboxes() {
    add_meta_box('wpt_events_product', 'Add \'From only\' text to product', 'wpt_events_products', 'product', 'advanced', 'default');
}

add_action( 'add_meta_boxes', 'add_events_metaboxes' );


function wpt_events_products($post) {
    $text_status = get_post_meta( $post->ID, 'enable_text', true) ;
    //checkbox to select add text or not
    echo '<input id="wp_add_text_to_woo_product" name="text_enable" type="checkbox" '. checked('on', $text_status, false) .'></input>';

}

//function will update post meta on save post button
function my_project_updated_post_meta( $post_id ) {
    update_post_meta($post_id, 'enable_text', $_POST['text_enable']);
}

add_action( 'save_post', 'my_project_updated_post_meta' );

//function will check for perticular product for which check box is set and assign text for them.
function custom_price_message( $price, $product ) { 
    $text_status = get_post_meta( $product->get_id(), 'enable_text', true) ;

    if($text_status == "on") { 
        $addedtext = 'From only ';
        return '<span class="price-prefix-text">' . $addedtext . '</span>' . $price;
    }

    return $price;

}
add_filter( 'woocommerce_get_price_html', 'custom_price_message', 10, 2 );
0
ответ дан neway_phil 27 August 2018 в 03:53
поделиться
 //add meta box to each product which will take status to add text or not
function add_events_metaboxes() {
    add_meta_box('wpt_events_product', 'Add text to product', 'wpt_events_products', 'product', 'side', 'default');
}

add_action( 'add_meta_boxes', 'add_events_metaboxes' );


function wpt_events_products() {
    //checkbox to select add text or not
        echo '<input id="wp_add_text_to_woo_product" name="text_enable" type="checkbox"></input>';
}

//function will update post meta on save post button
function my_project_updated_post_meta( $post_id ) {
    update_post_meta($post_id, 'enable_text', $_POST['text_enable']);
}

add_action( 'save_post', 'my_project_updated_post_meta' );

//function will check for perticular product for which check box is set and assign text for them.
function custom_price_message( $price ) {
    $args = array('post_type' => 'product', 'nopaging' => true);
        $product_query = new WP_Query($args);
        $posts = $product_query->get_posts();
            foreach ($posts as $post) {

        $text_status = get_post_meta($post->ID,'enable_text',true);
                if($text_status == "on")  { 
                    $addedtext = '(Upfront Paid in Full Price)';
                    return $price . '<br /><span class="price-disclaimer">' . $addedtext . '</span>';
                    }
                else {
                           return $price;
                    }
            }
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
0
ответ дан WisdmLabs 27 August 2018 в 03:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: