提问者:小点点

如何为简单产品sku和if变体产品sku和父sku创建阵列


你好,我需要创建一个数组,如果产品是简单的,产品sku。如果产品是变体,则变体sku和父产品sku。它似乎没有清除“is_type(“变量”)检查,只是把itemID和itemGroupID的产品id sku。请查看我的代码:

          if (!class_exists('WooCommerce')) return;
            $cartRecordsJM = [];
    foreach ( WC()->cart->get_cart() as $cart_item ) {
    // get the data of the cart item
   $product = $cart_item['data'];
   // Only for variable products when cart is not empty
    if( $product->is_type('variable') && ! WC()->cart->is_empty() )
 { 
    $variation_id = WC_Product($cart_item['variation_id']);
    $product_id         = $variation_id->get_sku();
    $parent = WC_Product($product['product_id']);
    $parent_id       = $parent->get_sku();
  } elseif (! WC()->cart->is_empty() ) {
    $simple_id = new WC_Product($cart_item['product_id']);
    $product_id = $simple_id->get_sku();
    $parent_id = '';
  } else {
    return;
  }
    
    $cartRecordsJM[] = [
     'itemID'=> $product_id, 
     'itemGroupID'=> $parent_id 
   ];
      }
?>

然后我用javascript传递数组

var jArray = <?php echo json_encode($cartRecordsJM); ?>;
        var cartRecords = [];
        var length = jArray.length;
        // Repeat this block for each item the customer has in their cart
        for (let i = 0; i < length;  i++) {
        cartRecords.push({'itemId': jArray[i]['itemID'] ,'itemGroupId': jArray[i]['itemID']});
}
        var page_meta = {
            'pageType': "cart",
            'cartRecords': cartRecords
        };

我尝试创建购物车中所有项目的数组,如下所示:

cartRecords: Array(5)
0: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}
1: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}
2: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}
3: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}
4: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}
5: {itemId: "simple/variation sku", itemGroupId: "parent product sku if variation"}

共1个答案

匿名用户

这样一个愚蠢的错误,在我所有的编辑之前,我实际上已经纠正了它。我犯了一个错误

cartRecords.push({'itemId': jArray[i]['itemID'] ,'itemGroupId': jArray[i]['itemID']});

"itemID"在两个位置...

以下是完成此任务的正确方法,经过测试,效果良好!

if (page_type == 'cart') {
        <?php
          if (!class_exists('WooCommerce')) {return; }// add this line 
            $cartRecordsJM = [];
    foreach ( WC()->cart->get_cart() as $cart_item ) {
    // get the data of the cart item
   $product = $cart_item['data'];
   // ONLY SIMPLE PRODUCT
    if( $product->is_type('simple') ) { 
    $simple_id = new WC_Product($cart_item['product_id']);
    $product_id = $simple_id->get_sku();
    $parent_id = '';
  } elseif ( $cart_item['variation_id'] > 0 ) {
    $variation_jm = new WC_Product_Variation($cart_item['variation_id']);
    $product_id = $variation_jm->get_sku();
    $parent = wc_get_product($variation_jm->get_parent_id());
    $parent_id       = $parent->get_sku();
  } 
    
    $cartRecordsJM[] = [
     'itemID'=> $product_id, 
     'itemGroupID'=> $parent_id 
   ];
      }
?>
        var jArray = <?php echo json_encode($cartRecordsJM); ?>;
        var cartRecords = [];
        var length = jArray.length;
        // Repeat this block for each item the customer has in their cart
        for (let i = 0; i < length;  i++) {
        cartRecords.push({'itemId': jArray[i]['itemID'] ,'itemGroupId': jArray[i]['itemGroupID']});
}
        var page_meta = {
            'pageType': "cart",
            'cartRecords': cartRecords
        };
        console.log(page_meta);
    }