0

所以这段代码过去可以工作,但现在由于某种原因它不再工作了。

我有一个用于定价的自定义 ACF 字段,以便更轻松地更新定价。此功能适用于简单产品,但不适用于可变产品。

该函数只更新已经有值的可变产品常规价格,但不更新空字段,这是违反直觉的。该代码以前可以使用,但我不记得它何时更改。没有插件/主题冲突。

代码如下

    
    global $post;
    $product = get_product($post->ID);
    $price_class = get_field('price_class');
    $class = get_field('selected_price_class');
    //$product = wc_get_product( $post->ID );
    $set = 0;

    if ( $price_class == 'enabled'):    
        switch ($class) {
             case 'class_a':
                $set = 4.95;
            break;
             case 'class_b':
                $set = 8.95;
            break;
            case 'class_c':
                $set = 12.95;
            break;
            case 'class_d':
                $set = 14.95;
            break;
            case 'class_e':
                $set = 18.95;
            break;
            case 'class_f':
                $set = 24.95;
            break;
            case 'class_g':
                $set = 32.95;
            break;
            case 'class_h':
                $set = 42.95;
            break;
       };    
        
        if ( $product->is_type('variable') ){
            foreach( $product->get_available_variations() as $variation ){
                $variation_id = $variation['variation_id'];
                
                //Price update
                update_post_meta( $variation_id, '_regular_price', $set );
                update_post_meta( $variation_id, '_price', $set );
                
                //Clear variation cache
                wc_delete_product_transients( $variation_id ); 
            };
                    
        } elseif ( $product->is_type('simple') ){
                
                //Price update
                update_post_meta( $post->ID, '_regular_price', $set );
                update_post_meta( $post->ID, '_price', $set );
            };
        
        // Clear/refresh the variable product cache
        wc_delete_product_transients( $post->ID );
    
    endif;
};

add_action( 'woocommerce_process_product_meta','update_custom_price', 100, 1 );```
4

0 回答 0