【问题标题】:Woocommerce - Get variation SKU from Bundled ProductsWoocommerce - 从捆绑产品中获取变体 SKU
【发布时间】:2017-11-01 20:51:39
【问题描述】:

我使用带有捆绑产品插件的 WooCommerce 来显示一个页面,人们可以在其中选择配置产品的选项。每个选项都是具有变化的产品。为了获得所有 4 种产品的变化,我使用下面的代码。在我升级到 WC 3 (oops) 之前,它一直运行良好。现在我收到一个错误,即 sku 为空。我是否需要更改从产品中获取 SKU 的方式?

   do_action( 'woocommerce_before_bundled_items', $product );

foreach ( $bundled_items as $bundled_item ) {

    /**
     * 'woocommerce_bundled_item_details' hook
     *
     * @hooked wc_pb_template_bundled_item_details_wrapper_open  -   0
     * @hooked wc_pb_template_bundled_item_thumbnail             -   5
     * @hooked wc_pb_template_bundled_item_details_open          -  10
     * @hooked wc_pb_template_bundled_item_title                 -  15
     * @hooked wc_pb_template_bundled_item_description           -  20
     * @hooked wc_pb_template_bundled_item_product_details       -  25
     * @hooked wc_pb_template_bundled_item_details_close         -  30
     * @hooked wc_pb_template_bundled_item_details_wrapper_close - 100
     */
    do_action( 'woocommerce_bundled_item_details', $bundled_item, $product );
 $variations = $bundled_item->get_product_variations();
 foreach($variations as $variation) {
    if($variation['sku'] == 'colors') {
      $bi['colors'] = $bundled_item;
      continue 2;
    }
    if($variation['sku'] == 'material') {
      $bi['material'] = $bundled_item;
      continue 2;
    }
    if($variation['sku'] == 'corners') {
      $bi['corners'] = $bundled_item;
      continue 2;
    }
    if($variation['sku'] == 'text') {
      $bi['text'] = $bundled_item;
      continue 2;
    }
 }
    do_action( 'woocommerce_after_bundled_items', $product );

}

【问题讨论】:

    标签: php wordpress loops foreach woocommerce


    【解决方案1】:

    此代码适用于我在 3.0 中查找每个捆绑商品的变体或捆绑中没有变体的商品的 SKU:

    //Check for Variation ID first & use Variation, if it exists
    $product_variation_id = $bundled_item->get_variation_id();        
    // Check if product has variation.
    if ($product_variation_id){
       $product = new WC_Product_Variation($product_variation_id);            
    }else{
       $product = $bundled_item->get_product();                
    }
    $sku = $product->get_sku();
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多