2016-11-20 15 views
0

商品バリエーションをターゲットにして、バリエーションごとに属性に基づいて価格を変更する方法を探しています。以下のコードは正しい方向に向いていますが、商品属性を使用してパターンをターゲティングする代わりに、パターンのターゲットを設定するためにパターンの現在の価格を変数と照合することに頼っています。それは動作しますが、それはあいまいさとの間違いのための余裕の余地を残します。商品バリエーションを取得して価格を変更する - 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クラスを実行すると、いくつかは、グローバルコールする必要があります

答えて

1

下記の作業溶液を投稿。あなたは(それだけで、配列のような「フラット化」オブジェクトをすることができます)あなたはクラスの機能を使用することができ、パラメータとして$productを持っているので、他のWordで

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

は、そうではありません。 $ newproductobject-> regular_price()を使うためには、クラスの新しいオブジェクトを初期化する必要があります。

コードがwoocommerceクラスがまだロードされていないプラグインの一部である場合は、WC_Product _Variationクラスを含むファイルを含める必要があります。 (そうは思わない)

希望します!

+0

お返事ありがとうございました!残念ながら、あなたの提案は私に _ "致命的なエラーを通知しています:メッセージ ''バリエーション#0の親製品が設定されていません '........" _メッセージでキャッチされない例外'例外 ' ** product_variation = $ product ['variation_id']; **と** $ regular_price = $ product ['regular_price']; **のようにフォーマットすると消えますが、どちらの文字列からでも返すことができます数字は2、7、9で、どこから数字が引かれているのか分かりません。 – isk

+1

コードは単なる例で、get_post_metaを '_variation_id'にしようとすると' $ product_variation = new WC_Product($ product_id); var_dump the result – Benoti

+0

もう少し微調整した後、私は解決策を見つけました。ヘッドスタートをありがとう! – isk

0

実用的な解決策が見つかりました。私はコードマスターではないので、おそらく少し肥大ですが、私はそれが仕事です!以下は基本的なバージョンですが、このような柔軟性により、特定の属性を共有する製品のユーザー役割による変動価格を自動的に一括して割り引くなど、より有用な機能が可能になります。

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; 
      } 
     } 
} 
0

次のスクリプトを使用して製品バリエーションを更新してください。詳細はこちらをクリックしてください。 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; 

} 
関連する問題