本文整理汇总了PHP中Magento\Catalog\Model\Product::setWebsiteIds方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::setWebsiteIds方法的具体用法?PHP Product::setWebsiteIds怎么用?PHP Product::setWebsiteIds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Product
的用法示例。
在下文中一共展示了Product::setWebsiteIds方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initialize
/**
* Initialize product before saving
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function initialize(\Magento\Catalog\Model\Product $product)
{
$productData = $this->request->getPost('product');
unset($productData['custom_attributes']);
unset($productData['extension_attributes']);
if ($productData) {
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
$productData['stock_data'] = $this->stockFilter->filter($stockData);
}
foreach (['category_ids', 'website_ids'] as $field) {
if (!isset($productData[$field])) {
$productData[$field] = [];
}
}
$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
$product->unlockAttribute('media');
$wasLockedMedia = true;
}
$product->addData($productData);
if ($wasLockedMedia) {
$product->lockAttribute('media');
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}
/**
* Check "Use Default Value" checkboxes values
*/
$useDefaults = $this->request->getPost('use_default');
if ($useDefaults) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
$links = $this->request->getPost('links');
$links = is_array($links) ? $links : [];
$linkTypes = ['related', 'upsell', 'crosssell'];
foreach ($linkTypes as $type) {
if (isset($links[$type])) {
$links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
}
}
$product = $this->productLinks->initializeLinks($product, $links);
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
// mark custom options that should to fall back to default value
$options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
$product->setProductOptions($options);
}
$product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
return $product;
}
示例2: initialize
/**
* Initialize product before saving
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
*/
public function initialize(\Magento\Catalog\Model\Product $product)
{
$productData = $this->request->getPost('product');
if ($productData) {
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : array();
$productData['stock_data'] = $this->stockFilter->filter($stockData);
}
foreach (array('category_ids', 'website_ids') as $field) {
if (!isset($productData[$field])) {
$productData[$field] = array();
}
}
$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
$product->unlockAttribute('media');
$wasLockedMedia = true;
}
$product->addData($productData);
if ($wasLockedMedia) {
$product->lockAttribute('media');
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds(array($this->storeManager->getStore(true)->getWebsite()->getId()));
}
/**
* Create Permanent Redirect for old URL key
*/
if ($product->getId() && isset($productData['url_key_create_redirect'])) {
$product->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']);
}
/**
* Check "Use Default Value" checkboxes values
*/
$useDefaults = $this->request->getPost('use_default');
if ($useDefaults) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
$links = $this->request->getPost('links');
$links = is_array($links) ? $links : array();
$linkTypes = array('related', 'upsell', 'crosssell');
foreach ($linkTypes as $type) {
if (isset($links[$type])) {
$links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
}
}
$product = $this->productLinks->initializeLinks($product, $links);
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
$product->setProductOptions($productData['options']);
}
$product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
return $product;
}
示例3: initializeFromData
/**
* Initialize product from data
*
* @param \Magento\Catalog\Model\Product $product
* @param array $productData
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function initializeFromData(\Magento\Catalog\Model\Product $product, array $productData)
{
unset($productData['custom_attributes']);
unset($productData['extension_attributes']);
if ($productData) {
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
$productData['stock_data'] = $this->stockFilter->filter($stockData);
}
$productData = $this->normalize($productData);
if (!empty($productData['is_downloadable'])) {
$productData['product_has_weight'] = 0;
}
foreach (['category_ids', 'website_ids'] as $field) {
if (!isset($productData[$field])) {
$productData[$field] = [];
}
}
foreach ($productData['website_ids'] as $websiteId => $checkboxValue) {
if (!$checkboxValue) {
unset($productData['website_ids'][$websiteId]);
}
}
$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
$product->unlockAttribute('media');
$wasLockedMedia = true;
}
$dateFieldFilters = [];
$attributes = $product->getAttributes();
foreach ($attributes as $attrKey => $attribute) {
if ($attribute->getBackend()->getType() == 'datetime') {
if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
}
}
}
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
$productData = $inputFilter->getUnescaped();
if (isset($productData['options'])) {
$productOptions = $productData['options'];
unset($productData['options']);
} else {
$productOptions = [];
}
$product->addData($productData);
if ($wasLockedMedia) {
$product->lockAttribute('media');
}
if ($this->storeManager->hasSingleStore() && empty($product->getWebsiteIds())) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}
/**
* Check "Use Default Value" checkboxes values
*/
$useDefaults = (array) $this->request->getPost('use_default', []);
foreach ($useDefaults as $attributeCode => $useDefaultState) {
if ($useDefaultState) {
$product->setData($attributeCode, null);
}
}
$product = $this->setProductLinks($product);
/**
* Initialize product options
*/
if ($productOptions && !$product->getOptionsReadonly()) {
// mark custom options that should to fall back to default value
$options = $this->mergeProductOptions($productOptions, $this->request->getPost('options_use_default'));
$customOptions = [];
foreach ($options as $customOptionData) {
if (empty($customOptionData['is_delete'])) {
if (isset($customOptionData['values'])) {
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
return empty($valueData['is_delete']);
});
}
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
$customOption->setProductSku($product->getSku());
$customOption->setOptionId(null);
$customOptions[] = $customOption;
}
}
$product->setOptions($customOptions);
}
$product->setCanSaveCustomOptions(!empty($productData['affect_product_custom_options']) && !$product->getOptionsReadonly());
return $product;
}
示例4: _saveWebsiteIds
/**
* Save product website relations
*
* @param \Magento\Catalog\Model\Product $product
* @return $this
*/
protected function _saveWebsiteIds($product)
{
if (empty(array_diff($product->getWebsiteIds(), [0]))) {
$product->setWebsiteIds([1]);
}
$websiteIds = $product->getWebsiteIds();
$oldWebsiteIds = [];
$product->setIsChangedWebsites(false);
$connection = $this->getConnection();
$oldWebsiteIds = $this->getWebsiteIds($product);
$insert = array_diff($websiteIds, $oldWebsiteIds);
$delete = array_diff($oldWebsiteIds, $websiteIds);
if (!empty($insert)) {
$data = [];
foreach ($insert as $websiteId) {
$data[] = ['product_id' => (int) $product->getEntityId(), 'website_id' => (int) $websiteId];
}
$connection->insertMultiple($this->getProductWebsiteTable(), $data);
}
if (!empty($delete)) {
foreach ($delete as $websiteId) {
$condition = ['product_id = ?' => (int) $product->getEntityId(), 'website_id = ?' => (int) $websiteId];
$connection->delete($this->getProductWebsiteTable(), $condition);
}
}
if (!empty($insert) || !empty($delete)) {
$product->setIsChangedWebsites(true);
}
return $this;
}
示例5: initialize
/**
* Initialize product before saving
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function initialize(\Magento\Catalog\Model\Product $product)
{
$productData = $this->request->getPost('product');
unset($productData['custom_attributes']);
unset($productData['extension_attributes']);
if ($productData) {
$stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
$productData['stock_data'] = $this->stockFilter->filter($stockData);
}
foreach (['category_ids', 'website_ids'] as $field) {
if (!isset($productData[$field])) {
$productData[$field] = [];
}
}
$wasLockedMedia = false;
if ($product->isLockedAttribute('media')) {
$product->unlockAttribute('media');
$wasLockedMedia = true;
}
$dateFieldFilters = [];
$attributes = $product->getAttributes();
foreach ($attributes as $attrKey => $attribute) {
if ($attribute->getBackend()->getType() == 'datetime') {
if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
$dateFieldFilters[$attrKey] = $this->dateFilter;
}
}
}
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
$productData = $inputFilter->getUnescaped();
$product->addData($productData);
if ($wasLockedMedia) {
$product->lockAttribute('media');
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}
/**
* Check "Use Default Value" checkboxes values
*/
$useDefaults = $this->request->getPost('use_default');
if ($useDefaults) {
foreach ($useDefaults as $attributeCode) {
$product->setData($attributeCode, false);
}
}
$links = $this->request->getPost('links');
$links = is_array($links) ? $links : [];
$linkTypes = ['related', 'upsell', 'crosssell'];
foreach ($linkTypes as $type) {
if (isset($links[$type])) {
$links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
}
}
$product = $this->productLinks->initializeLinks($product, $links);
$productLinks = $product->getProductLinks();
$linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()];
foreach ($linkTypes as $linkType => $readonly) {
if (isset($links[$linkType]) && !$readonly) {
foreach ($links[$linkType] as $linkId => $linkData) {
$linkProduct = $this->productRepository->getById($linkId);
$link = $this->productLinkFactory->create();
$link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0);
$productLinks[] = $link;
}
}
}
$product->setProductLinks($productLinks);
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
// mark custom options that should to fall back to default value
$options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
$customOptions = [];
foreach ($options as $customOptionData) {
if (!(bool) $customOptionData['is_delete']) {
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
$customOption->setProductSku($product->getSku());
$customOption->setOptionId(null);
$customOptions[] = $customOption;
}
}
$product->setOptions($customOptions);
}
$product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
return $product;
}
示例6: assignProductToWebsites
/**
* @param \Magento\Catalog\Model\Product $product
* @return void
*/
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
{
if (!$this->storeManager->hasSingleStore()) {
if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
$websiteIds = array_keys($this->storeManager->getWebsites());
} else {
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
}
$product->setWebsiteIds(array_unique(array_merge($product->getWebsiteIds(), $websiteIds)));
}
}