简体   繁体   English

获取产品变化并修改价格-woocommerce

[英]Get product variation and modify price - woocommerce

I'm looking for a way to target product variations and modify their prices based on the attributes each variation has. 我正在寻找一种针对产品变体并根据每个变体具有的属性修改其价格的方法。 The below code is somewhat heading in the right direction, but instead of using product attributes to target variations, I'm relying on matching the variation's current price to a variable in order to target it. 以下代码朝着正确的方向发展,但我没有使用产品属性来定位变体,而是依靠将变体的当前价格与变量进行匹配,以便将其作为目标。 It works, but it leaves a ton of room for error with how vague it is. 它可以工作,但是它含糊不清却留下了很多错误的余地。

add_filter('woocommerce_get_price','change_price', 10, 2);
function change_price($price, $productd){

if (  $productd->product_type == 'variation' || $productd->product_type == 'variable' ) {

$basicmp3 = 25;

$vprice = get_post_meta( $productd->variation_id, '_regular_price',true);

    if ($vprice == $basicmp3) {

            $vprice2 = $vprice * .5;    
            return $vprice2;
    }
    else{           
         return $vprice;
    } 
}
}

UPDATE Posted a working solution below 更新下面发布了一个可行的解决方案

When you run a woocommerce class in a function, there's a need to call some global. 当您在函数中运行woocommerce类时,需要调用某些全局变量。

 global $woocommerce; $product;
 $product_variation = new WC_Product_Variation($_POST['variation_id']);
 $regular_price = $product_variation->regular_price;

In other Word, it's not because you have $product as parameter that you can use the class functions (it's just a 'flatten' object like an array can be). 换句话说,并不是因为您有$product作为参数,您才可以使用类函数(它只是一个“扁平”对象,就像数组一样)。 You need to initialize a new object of a class to use $newproductobject->regular_price(). 您需要初始化一个类的新对象,以使用$ newproductobject-> regular_price()。

It's possible, if your code is part of a plugin that the woocommerce class are not yet load, then you'll need to include the file containing the class WC_Product _Variation. 可能的是,如果您的代码是woocommerce类尚未加载的插件的一部分,则您需要包括包含类WC_Product _Variation的文件。 (Don't think it's the case) (别以为是)

Hope it helps ! 希望能帮助到你 !

Found a working solution. 找到了可行的解决方案。 It's probably a little bloated since I'm no code master, but I does it's job! 因为我不是代码大师,所以可能有点肿,但是我做到了! Below is a basic version, but this kind of flexibility will allow for more useful functions, such as automatically bulk discounting variation prices by user role for products who share certain attributes. 以下是基本版本,但是这种灵活性将允许使用更多有用的功能,例如,对于具有某些属性的产品,按用户角色自动批量打折变异价格。

add_filter('woocommerce_get_price','change_price', 10, 2);
function change_price($price, $productd){


global $post, $woocommerce;
$post_id = $variation->ID;


    $args = array(
                 'post_type'     => 'product_variation',
                 'post_status'   => array( 'private', 'publish' ),
                 'numberposts'   => -1,
                 'orderby'       => 'menu_order',
                 'order'         => 'asc',
                 'post_parent'   => $post->ID
             );
             $variations = get_posts( $args ); 

    //declare variables for later use        
    $demomp3 = '';
    $basicmp3 = '';
    $basicwav = '';
    $premiummp3 = '';
    $premiumwav = '';
    $syncmp3 = '';
    $syncwav = '';

    foreach ( $variations as $variation ) {

                $variation_id           = absint( $variation->ID );$variable_id = $this['variation_id'];
                $variation_post_status  = esc_attr( $variation->post_status );
                $variation_data         = get_post_meta( $variation_id );
                $variation_data['variation_post_id'] = $variation_id;

                //find attributes
                $option1 = 'pa_license-options';
                $option2 = 'pa_delivery-format';
                $getmeta1 = get_post_meta($variation_id , 'attribute_'.$option1, true);
                $getmeta2 = get_post_meta($variation_id , 'attribute_'.$option2, true);
                $attribute1 = get_term_by('slug', $getmeta1, $option1);
                $attribute2 = get_term_by('slug', $getmeta2, $option2);
                $license = $attribute1->name;
                $format = $attribute2->name;


                //get the prices of each variation by the attribute combinations they have              
                if ($format === "mp3" && $license === "Demo License" ){

                    //retrieve variation price and assign it to the previously created variable
                    $demomp3 = get_post_meta($variation_id, '_regular_price', true ); 

                }   
                if ($format === "mp3" && $license === "Basic License" ){
                    $basicmp3 = get_post_meta($variation_id, '_regular_price', true ); 

                }
                if ($format === "WAV" && $license === "Basic License" ){
                    $basicwav = get_post_meta($variation_id, '_regular_price', true ); 

                }   
                if ($format === "mp3" && $license === "Premium License" ){
                    $premiummp3 = get_post_meta($variation_id, '_regular_price', true ); 

                }
                if ($format === "WAV" && $license === "Premium License" ){
                    $premiumwav = get_post_meta($variation_id, '_regular_price', true ); 

                }   
                if ($format === "mp3" && $license === "Sync License" ){
                    $syncmp3 = get_post_meta($variation_id, '_regular_price', true ); 

                }   
                if ($format === "WAV" && $license === "Sync License" ){
                    $syncwav = get_post_meta($variation_id, '_regular_price', true ); 

                }   
    }
        //Simple product doesn't need to be modified
        if($productd->product_type == 'simple'){


            return $price;

        }
        //adjust prices of variable products    
        if (  $productd->product_type == 'variation' || $productd->product_type == 'variable' ) {

            $regprice = get_post_meta( $productd->variation_id, '_regular_price',true);
            //target the current variation by matching its price to the one stored in the variable
            if ($regprice == $demomp3) {

                    //apply price adjustment and return the new value
                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $basicmp3) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $basicwav) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $premiummp3) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $premiumwav) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $syncmp3) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else if ($regprice == $syncwav) {

                    $price = $regprice * .5;    
                    return $price;
            }
            else{           
                 return $price;
            }   
       }
}

Use the following script to update product variation. 使用以下脚本更新产品版本。 click here for detailed explanation. 单击此处了解详细说明。 https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/ https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/

function getExistingProducts($updatedPrices,$skuArray) {

$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));

while ($loop->have_posts()) : $loop->the_post();

    $id = get_the_ID();
    $product = wc_get_product( $id );
    $sku = get_post_meta($id, '_sku', true);

    if( in_array( $sku, $skuArray  ) ) {

        $attributes = $product->get_attributes();
        $attributes['medium-quantity-price']['value'] = $updatedPrices[$sku][4];
        $attributes['low-quantity-price']['value'] = $updatedPrices[$sku][3];
      //  $attributes['v-low-quantity-price']['value'] = $updatedPrices[$sku][2];
        update_post_meta( $id,'_product_attributes',$attributes);
        echo ' Update Sku : '.$sku.' '.PHP_EOL;

    }

endwhile;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM