Commit f6912097 by wd

meta生成

parent 76989a22
......@@ -5,7 +5,6 @@ use Magento\Catalog\Block\Adminhtml\Category\Tab\Product as JoshineProduct;
class Product extends JoshineProduct
{
public function setCollection($collection){
echo 111;
$collection->addAttributeToSelect('thumbnail');
parent::setCollection($collection);
}
......
<?php
namespace Joshine\Category\Observer\Frontend\Catalog;
use Magento\Framework\Url;
use Magento\Framework\UrlInterface;
class CategoryLoadAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
private $url;
private $attributeRepository;
public function __construct(
\Psr\Log\LoggerInterface $logger,
UrlInterface $url,
\Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository
)
{
$this->logger = $logger;
$this->url = $url;
$this->attributeRepository = $attributeRepository;
}
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
)
{
$category = $observer->getEvent()->getCategory();
if ($category instanceof \Magento\Catalog\Model\Category) {
$str = $this->getUrlParameter($category);
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle('Buy the Latest ' . $str['title'] .' on '.$this->url->getBaseUrl());
$category->setMetaDescription('Shop ' . $str['title'] . '. ' . $oldMetaDescription);
if ($str['keyword']){
$category->setMetaKeywords($str['keyword']);
}else{
$category->setMetaKeywords($oldMetaKeyword.'.');
}
}
}
public function getUrlParameter($category){
$categoryName = $category->getName();
$data = $_REQUEST;
$arr = ['title'=>'' , 'keyword'=>''];
if (isset($data['product_list_order'])){
$sortBy = $category->getAvailableSortByOptions();
$arr['title'] .= $sortBy[$data['product_list_order']] . ' ';
$arr['keyword'] .= $sortBy[$data['product_list_order']] .' '. $categoryName.',';
}
if (isset($data['color'])){
$attribute = $this->attributeRepository->get('4', 'color');
$optionText = $attribute->getSource()->getOptionText($data['color']);
$arr['title'] .= $optionText .' ';
$arr['keyword'] .= $optionText .' '. $categoryName .',';
}
if (isset($data['size'])){
$attribute = $this->attributeRepository->get('4', 'size');
$optionText = $attribute->getSource()->getOptionText($data['size']);
$arr['title'] .= $optionText .' ';
$arr['keyword'] .= $optionText .' '. $categoryName .',';
}
if (isset($data['price'])){
$priceArr = explode('-',$data['price']);
$price = '$'.$priceArr[0].'-$'.$priceArr[1];
$arr['title'] .= $price .' ';
$arr['keyword'] .= $price .' '. $categoryName.',';
}
$arr['title'] .= $categoryName;
$arr['keyword'] = rtrim($arr['keyword'],',') . '.';
return $arr;
}
}
\ No newline at end of file
<?php
namespace Joshine\Category\Plugin\Catalog\Helper\Product;
use Magento\Catalog\Helper\Product\View as ProductViewHelper;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\UrlInterface;
use Magento\TestFramework\Event\Magento;
class ViewPlugin
{
/**
* @var \Magento\Framework\Registry $coreRegistry,
*/
private $coreRegistry;
private $resultPage;
private $urlInterface;
public function __construct(Registry $coreRegistry, UrlInterface $urlInterface)
{
$this->coreRegistry = $coreRegistry;
$this->urlInterface = $urlInterface;
}
public function aroundPrepareAndRender(
ProductViewHelper $productViewHelper,
callable $proceed,
Page $resultPage,
$productId,
$controller,
$params = null
) {
// collecting result page instance for future use
$this->resultPage = $resultPage;
// send the control to the original method
$result = $proceed($resultPage, $productId, $controller, $params);
// after meta title and description is generated, this will be executed
// make sure the helper instance is correct
if (!$result instanceof ProductViewHelper) {
return $productViewHelper;
}
// collecting product information from the core registry
$product = $this->coreRegistry->registry('product');
// changing meta title and description as needed
$this->alterMetaTitle($product);
$this->alterMetaDescription($product);
$this->alterMetaKeyword($product);
return $result;
}
/**
* Changing meta title of PDP; Allowing only 60 characters
* @param $product
*/
private function alterMetaTitle($product)
{
$pageConfig = $this->resultPage->getConfig();
$title = $product->getMetaTitle();
if (!$title){
$pageConfig->getTitle()->set('$'.$product->getFinalPrice() . ' - ' .$product->getName() . ' Online at '.$this->urlInterface->getBaseUrl());
}
}
/**
* Changing meta description of PDP; Allowing only 60 characters
* @param $product
*/
private function alterMetaDescription($product)
{
$pageConfig = $this->resultPage->getConfig();
$description = $product->getMetaDescription();
if (!$description) {
$pageConfig->setDescription('Buy ' . $product->getName() . ' Online at the Price of $' . $product->getFinalPrice() .' at '.$this->urlInterface->getBaseUrl() . '.' . ' You Will Get the Best Service and Worldwide Free Shipping for Orders $69+.');
}
}
/**
* Changing meta keyword of PDP; Allowing only 60 characters
* @param $product
*/
private function alterMetaKeyword($product)
{
$pageConfig = $this->resultPage->getConfig();
$keyWord = $product->getMetaKeyword();
$ids = $product->getCategoryIds();
if (!$keyWord) {
$pageConfig->setKeywords($product->getName() . '. ' . $this->getCategoryName($ids) . '.');
}
}
private function getCategoryName($ids){
if (empty($ids) || !is_array($ids) || count($ids) < 2){
return '';
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$name = [];
foreach ($ids as $val){
$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($val);
$name[] = $cat->getName();
}
$diffArr = ['Root Catalog','Category'];
$name = array_diff($name, $diffArr);
return implode(',',$name);
}
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Helper\Product\View">
<plugin
name="JoshineCategoryAlterPDPMetaTags"
type="Joshine\Category\Plugin\Catalog\Helper\Product\ViewPlugin" />
</type>
</config>
\ No newline at end of file
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_category_load_after">
<observer instance="Joshine\Category\Observer\Frontend\Catalog\CategoryLoadAfter"
name="joshine_category_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment