Skip to content

Issue2112 2.4 #2113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop-2.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Model/Api/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ protected function _makeCart(\Magento\Quote\Model\Quote $cart, $mailchimpStoreId
*/
foreach ($items as $item) {
$line = [];
if ($item->getProductType()=='bundle' || $item->getProductType()=='grouped') {
if ($item->getProductType()=='grouped') {
continue;
}

Expand All @@ -552,6 +552,21 @@ protected function _makeCart(\Magento\Quote\Model\Quote $cart, $mailchimpStoreId
}

$variantId = $variant->getId();
} elseif ($item->getProductType() == \Magento\Bundle\Model\Product\Type::TYPE_CODE) {
$bundleProducts = $cart->getAllItems();
foreach ($bundleProducts as $bundleProduct) {
if ($bundleProduct->getParentItemId() == $item->getItemId()) {
$itemCount++;
$lines[] = [
"id" => (string)$itemCount,
"product_id" => $bundleProduct-> getProductId(),
"product_variant_id" => $bundleProduct-> getProductId(),
"quantity" => (int)$bundleProduct->getQtyOrdered(),
"price" => $bundleProduct->getPrice()
];
}
}
continue;
} else {
$variantId = $item->getProductId();
}
Expand Down
16 changes: 16 additions & 0 deletions Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,22 @@ protected function generatePOSTPayload(
if (!$variant) {
continue;
}
} elseif ($item->getProductType() == \Magento\Bundle\Model\Product\Type::TYPE_CODE) {
$bundleProducts = $order->getAllItems();
foreach ($bundleProducts as $bundleProduct) {
if ($bundleProduct->getParentItemId() == $item->getItemId()) {
$itemCount++;
$data["lines"][] = [
"id" => (string)$itemCount,
"product_id" => $bundleProduct-> getProductId(),
"product_variant_id" => $bundleProduct-> getProductId(),
"quantity" => (int)$bundleProduct->getQtyOrdered(),
"price" => $bundleProduct->getPrice(),
"discount" => $bundleProduct->getDiscountAmount() ? abs($bundleProduct->getDiscountAmount()) : 0
];
}
}
continue;
} else {
$variant = $item->getProductId();
}
Expand Down
225 changes: 188 additions & 37 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Bundle\Model\Product\Type as ProductTypeBundle;


class Product
{
const DOWNLOADABLE = 'downloadable';
const GROUPED = 'grouped';
const PRODUCTIMAGE = 'product_small_image';
const MAX = 100;

Expand Down Expand Up @@ -81,6 +84,10 @@ class Product
* @var \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory
*/
private $_chimpSyncEcommerceCollection;
/**
* @var ProductTypeBundle
*/
private $_productTypeBundle;

/**
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
Expand All @@ -96,6 +103,7 @@ class Product
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection
* @param \Magento\Catalog\Helper\Data $taxHelper
* @param \Magento\Catalog\Model\Product\Option $option
* @param ProductTypeBundle $productTypeBundle
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
Expand All @@ -110,7 +118,8 @@ public function __construct(
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
\Magento\Catalog\Helper\Data $taxHelper,
\Magento\Catalog\Model\Product\Option $option
\Magento\Catalog\Model\Product\Option $option,
ProductTypeBundle $productTypeBundle
) {

$this->_productRepository = $productRepository;
Expand All @@ -126,6 +135,7 @@ public function __construct(
$this->_option = $option;
$this->_categoryCollection = $categoryCollection;
$this->taxHelper = $taxHelper;
$this->_productTypeBundle = $productTypeBundle;
$this->_batchId = \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT. '_' .
$this->_helper->getGmtTimeStamp();
}
Expand All @@ -143,7 +153,8 @@ public function _sendProducts($magentoStoreId)
$this->_markSpecialPrices($magentoStoreId, $mailchimpStoreId);
$batchArray = array_merge($batchArray,$this->processDeletedProducts($magentoStoreId, $mailchimpStoreId));
$collection = $this->_getCollection();
$collection->addFieldToFilter("type_id", ["nin"=>[\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE, "grouped"]]);
// $collection->addFieldToFilter("type_id", ["nin"=>[\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE, "grouped"]]);
$collection->addFieldToFilter("type_id", ["neq"=>self::GROUPED]);
$collection->addStoreFilter($magentoStoreId);
$collection->getSelect()->reset(\Magento\Framework\DB\Select::COLUMNS)->columns(['entity_id']);
$collection->getSelect()->joinLeft(
Expand Down Expand Up @@ -353,11 +364,16 @@ protected function _buildNewProductRequest(
}
break;
case \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL:
$variantProducts[] = $product;
break;
case self::DOWNLOADABLE:
case \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE:
$variantProducts[] = $product;
break;
// case self::DOWNLOADABLE:
// $variantProducts[] = $product;
// break;
// case \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE:
// $variantProducts[] = $product;
// break;
default:
return [];
}
Expand Down Expand Up @@ -391,12 +407,11 @@ protected function _buildOldProductRequest(
$variantProducts = [];
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE ||
$product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL ||
$product->getTypeId() == "downloadable") {
$product->getTypeId() == self::DOWNLOADABLE) {
$data = $this-> _buildProductData($product, $magentoStoreId);
$variantProducts [] = $product;
// $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId());
$parentIds = $this->_configurable->getParentIdsByChild($product->getId());

// $this->_helper->log("parentIds: ".print_r($parentIds, true));
//add or update variant
foreach ($parentIds as $parentId) {
$productSync = $this->_chimpSyncEcommerce->create()->getByStoreIdType(
Expand Down Expand Up @@ -431,6 +446,26 @@ protected function _buildOldProductRequest(
$operations[] = $productdata;
}
}
$bundleParentIds = $this->_productTypeBundle->getParentIdsByChild($product->getId());
foreach ($bundleParentIds as $bundleParentId) {
$bundleProduct = $this->_productRepository->getById($bundleParentId, false, $magentoStoreId);
$data = $this-> _buildProductData($bundleProduct, $magentoStoreId,false);
$body = json_encode($data);
if ($body===false) {
$jsonErrorMsg = json_last_error_msg();
$this->_helper->log("");
$this->_helper->log("$jsonErrorMsg for product [".$bundleProduct->getId()."]");
continue;
}
$productdata = [];
$productdata['method'] = "PATCH";
$productdata['path'] = "/ecommerce/stores/" . $mailchimpStoreId . "/products/" . $bundleParentId;
$productdata['operation_id'] = $batchId . '_' . $bundleParentId;
$productdata['body'] = $body;
$operations[] = $productdata;
$this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::PRO_MOD);
}

} elseif ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
$childProducts = $product->getTypeInstance()->getChildrenIds($product->getId());
$variantProducts[] = $product;
Expand Down Expand Up @@ -552,43 +587,48 @@ protected function _buildProductData(
if (!empty($data['image_url'])) {
$this->_parentImage = $data['image_url'];
}
//variants
$data["variants"] = [];
// put the min price of the simples as the price of the parent
foreach ($variants as $variant) {
if ($variant && $variant->getId() != $product->getId()) {
$variantPrice = $this->_getProductPrice($variant);
if ($this->productPrice) {
if ($variantPrice < $this->productPrice) {
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
list($min, $max) = $this->_bundleVariants($product, $magentoStoreId);
$data["variants"][] = $this->_variantForBundle($product, $magentoStoreId, $min,$product->getSku().'-min',$product->getId().'-min');
$data["variants"][] = $this->_variantForBundle($product, $magentoStoreId, $max,$product->getSku().'-max',$product->getId().'-max');
} else {
//variants
$data["variants"] = [];
// put the min price of the simples as the price of the parent
foreach ($variants as $variant) {
if ($variant && $variant->getId() != $product->getId()) {
$variantPrice = $this->_getProductPrice($variant);
if ($this->productPrice) {
if ($variantPrice < $this->productPrice) {
$this->productPrice = $variantPrice;
}
} else {
$this->productPrice = $variantPrice;
}
} else {
$this->productPrice = $variantPrice;
}
}
}
/**
* @var $variant \Magento\Catalog\Model\Product
*/
foreach ($variants as $variant) {
if ($variant) {
if ($variant->getId() != $product->getId()) {
$this->productPrice = null;
/**
* @var $variant \Magento\Catalog\Model\Product
*/
foreach ($variants as $variant) {
if ($variant) {
if ($variant->getId() != $product->getId()) {
$this->productPrice = null;
}
$data["variants"][] = $this->_buildProductData($variant, $magentoStoreId);
}
$data["variants"][] = $this->_buildProductData($variant, $magentoStoreId);
}
}
if ($this->_childtUrl) {
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE ||
$product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL ||
$product->getTypeId() == "downloadable") {
$data["url"] = $this->_childtUrl;
if ($this->_childtUrl) {
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE ||
$product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL ||
$product->getTypeId() == self::DOWNLOADABLE) {
$data["url"] = $this->_childtUrl;
}
$this->_childtUrl = null;
}
$this->_childtUrl = null;
$this->_parentImage = null;
}
$this->_parentImage = null;
}

return $data;
}

Expand Down Expand Up @@ -654,7 +694,8 @@ public function sendModifiedProduct(\Magento\Sales\Model\Order $order, $mailchim
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE &&
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL &&
$product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE &&
$product->getTypeId() != "downloadable")) {
$product->getTypeId() != self::DOWNLOADABLE &&
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)) {
$this->_helper->log('The product with id [' . $product->getId() .
'] is not supported [' . $product->getTypeId() . ']');
continue;
Expand Down Expand Up @@ -699,7 +740,8 @@ public function sendQuoteModifiedProduct(\Magento\Quote\Model\Quote $quote, $mai
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE &&
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL &&
$product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE &&
$product->getTypeId() != "downloadable")) {
$product->getTypeId() != self::DOWNLOADABLE &&
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)) {
$this->_helper->log(
'The product with id ['.$product->getId().'] is not supported ['.$product->getTypeId().']'
);
Expand Down Expand Up @@ -765,4 +807,113 @@ protected function _updateProduct(
$nullifyBatchId
);
}
protected function _bundleVariants($product, $magentoStoreId)
{
$productsArray = [];
$simpleProducts = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
foreach ($simpleProducts as $item) {
$selectionArray = [];
$selectionArray['selection_product_name'] = $item->getName();
$selectionArray['selection_product_quantity'] = $item->getSelectionQty();
$selectionArray['selection_product_price'] = $item->getPrice();
$selectionArray['selection_product_id'] = $item->getProductId();
$productsArray[$item->getOptionId()]['item'][$item->getSelectionId()] = $selectionArray;
}
foreach ($productsArray as $key => $productItem) {
$min = $max = 0;
foreach ($productItem['item'] as $item) {
if ($min == 0) {
$min = $item['selection_product_price']*$item['selection_product_quantity'];
}
if ($item['selection_product_price']*$item['selection_product_quantity'] < $min) {
$min = $item['selection_product_price']*$item['selection_product_quantity'];
}
if ($item['selection_product_price']*$item['selection_product_quantity'] > $max) {
$max = $item['selection_product_price']*$item['selection_product_quantity'];
}
}
$productsArray[$key]['min'] = $min;
$productsArray[$key]['max'] = $max;
}
$min = $max = 0;
foreach ($productsArray as $key => $productItem) {
$min += $productItem['min'];
$max += $productItem['max'];
}
return [$min, $max];
}
protected function _variantForBundle($product, $magentoStoreId, $price, $sku, $id)
{
$data = [];
$parent = null;

//data applied for both root and varient products
$data["id"] = $id;
$data["title"] = $product->getName();
$data["url"] = $product->getProductUrl();
$data["image_url"] = '';
if ($product->getImage() && $product->getImage()!='no_selection') {
$filePath = 'catalog/product/'.ltrim($product->getImage(), '/');
$data["image_url"] = $this->_helper->getBaserUrl(
$magentoStoreId,
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
).$filePath;
} elseif ($this->_parentImage) {
$data['image_url'] = $this->_parentImage;
} else {
$parent = $this->_getParent($product->getId(), $magentoStoreId);
if ($parent && $parent->getImage() && $parent->getImage()!='no_selection') {
$filePath = 'catalog/product/'.ltrim($parent->getImage(), '/');
$data["image_url"] = $this->_helper->getBaserUrl(
$magentoStoreId,
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
).$filePath;
}
}
$data["published_at_foreign"] = "";
$data["sku"] = $sku;
if ($this->productPrice) {
$data['price'] = $this->productPrice;
} else {
$data['price'] = $price;
}

//stock
$stock = $this->_stockRegistry->getStockItem($product->getId(), $magentoStoreId);
$data["inventory_quantity"] = (int)$stock->getQty();
$data["backorders"] = (string)$stock->getBackorders();
if ($product->getVisibility() == \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE) {
$tailUrl = '';
$data["visibility"] = 'false';
if (!$parent) {
$parent = $this->_getParent($product->getId(), $magentoStoreId);
}
if ($parent) {
$options = $parent->getTypeInstance()->getConfigurableAttributesAsArray($parent);
foreach ($options as $option) {
if (strlen($tailUrl)) {
$tailUrl .= '&';
} else {
$tailUrl .= '?';
}
$tailUrl .= $option['attribute_code'] . "=" . $product->getData($option['attribute_code']);
}
$this->_childtUrl = $data['url'] = $parent->getProductUrl() . $tailUrl;
if (empty($data['image_url'])) {
$filePath = 'catalog/product'.$parent->getImage();
$data["image_url"] = $this->_helper->getBaserUrl(
$magentoStoreId,
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
).$filePath;
}
}
} else {
$data["visibility"] = 'true';
}
return $data;
}
}
Loading