获取产品变体和修改价格 - woocommerce
问题描述:
我在寻找一种方法来定位产品变体,并根据每个变体的属性修改其价格。下面的代码有点朝着正确的方向前进,但是我不是使用产品属性来定位变化,而是依靠将变体的当前价格与变量进行匹配来实现目标。它的工作原理,但它有多么模糊的错误空间。获取产品变体和修改价格 - woocommerce
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发布以下
答
工作解决方案当你在一个函数运行woocommerce类,有一个需要调用一些全球性的。
global $woocommerce; $product;
$product_variation = new WC_Product_Variation($_POST['variation_id']);
$regular_price = $product_variation->regular_price;
换句话说,这不是因为你有$product
作为参数,你可以使用类函数(它就像一个阵列中的“扁平化”的对象都可以)。您需要初始化类的新对象以使用$ newproductobject-> regular_price()。
如果您的代码是woocommerce类尚未加载的插件的一部分,那么您可能需要包含包含类WC_Product _Variation的文件。 (不要认为是这样)
希望它有帮助!
答
找到了一个工作解决方案。这可能有点臃肿,因为我不是代码大师,但我做到了!以下是一个基本版本,但这种灵活性将允许更多有用的功能,例如自动批量折扣具有特定属性的产品的用户角色的变化价格。
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;
}
}
}
答
使用以下脚本更新产品变体。点击这里查看详细说明。 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;
}
感谢您的回复!不幸的是,你的建议是给我一个 _“致命错误:未捕获的异常'异常'消息'没有父变体#0的产品集'........”_消息。如果我将其格式化为** $ product_variation = $ product ['variation_id']; **和** $ regular_price = $ product ['regular_price']; **,但是我可以管理从字符串中返回的所有内容是数字2,7和9,我不确定这些数字是从哪里拉出来的。 – isk
代码只是一个例子,你可以尝试get_post_meta为'_variation_id',然后'$ product_variation = new WC_Product($ product_id);和var_dump的结果 – Benoti
经过多一点调整,我想出了一个工作解决方案。感谢您的领先! – isk