Reputation: 59
Hello I need to create an array that if the product is simple, product sku. If the product is a variation, variation sku and parent product sku. It doesn't seem to be clearing the "is_type('variable') check and just put the product id sku for both itemID and itemGroupID. Please see my code:
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
];
}
?>
I am then passing the array in 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
};
What i am trying to do is create an array of all items in the cart that looks like the following:
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"}
Upvotes: 1
Views: 287
Reputation: 59
Such a silly mistake, I actually had it correct before all of my edits. I made the mistake of putting
cartRecords.push({'itemId': jArray[i]['itemID'] ,'itemGroupId': jArray[i]['itemID']});
"itemID" in both locations...
Here is the correct way to accomplish this, tested and works!
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);
}
Upvotes: 1