magento 2 regenerate product url php code
please follow the step as i show below
1) backup you database
2) create one controller name Producturl.php
3) copy my code into that controller
<?php
namespace Company\Module\Controller\Producturl;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Store\Model\Store;
class Producturl extends \Magento\Framework\App\Action\Action
{
public function execute()
{
//// for regenerate product url /////////////////
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->collection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');
$this->productUrlRewriteGenerator = $objectManager->create('\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator');
$this->urlPersist = $objectManager->create('\Magento\UrlRewrite\Model\UrlPersistInterface');
$this->collection->addAttributeToSelect(['url_path', 'url_key']);
$list = $this->collection->load();
$store_id=1; // your store id
foreach($list as $product) {
if($store_id === Store::DEFAULT_STORE_ID)
$product->setStoreId($store_id);
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => $store_id
]);
try {
$this->urlPersist->replace(
$this->productUrlRewriteGenerator->generate($product)
);
} catch(\Exception $e) {
$out->writeln('<error>Duplicated url for '. $product->getId() .'</error>');
}
}
}
}
?>
make url short php
This code is for make you url short through google API
<?php
// Declare the class
class GoogleUrlApi {
public $key=your key
// Constructor
function GoogleURLAPI($apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$this->key;
}
// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response['longUrl']) ? $response['longUrl'] : false;
}
// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
// Create instance with key
$key = 'AIzaSyDUtkDIQ6uK7GCE68Lji_eQE9lPlo3Lk5Q';
$googer = new GoogleURLAPI();
// Test: Shorten a URL
$shortDWName = $googer->shorten("http://americanentrepreneurship.com/nj/entrepreneurs-featured/university-entrepreneurial-activities-draw-alumni-involvement");
echo $shortDWName; // returns http://goo.gl/i002
echo "<br>";
// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name
?>
<?php
// Declare the class
class GoogleUrlApi {
public $key=your key
// Constructor
function GoogleURLAPI($apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$this->key;
}
// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response['longUrl']) ? $response['longUrl'] : false;
}
// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
// Create instance with key
$key = 'AIzaSyDUtkDIQ6uK7GCE68Lji_eQE9lPlo3Lk5Q';
$googer = new GoogleURLAPI();
// Test: Shorten a URL
$shortDWName = $googer->shorten("http://americanentrepreneurship.com/nj/entrepreneurs-featured/university-entrepreneurial-activities-draw-alumni-involvement");
echo $shortDWName; // returns http://goo.gl/i002
echo "<br>";
// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name
?>
Create virtual host in local pc for you website in ubuntu
Create virtual host in local pc for you website in ubuntu
1) open you terminal with sudo su
2) cd /etc/apache2/sites-available/ paste this into your terminal .
3) sudo cp 000-default.conf example1.com.conf
4) go to folder /etc/apache2/sites-available/ and open file example1.com.conf into any editor you want
5) than paste below code
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/example1/
ServerName example1.dev
ServerAlias www.example1.dev
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
6) save the file asked for password enter your root password .
7) open your terminal and run sudo a2ensite example1.com.conf
8) sudo systemctl restart apache2
or
sudo service apache2 restart
9) open file /etc/hosts
enter this 2 line
192.168.1.30 example1.dev
192.168.1.30 www.example1.dev
#Note: 192.168.1.30 is my pc ip so you write your ip
1) open you terminal with sudo su
2) cd /etc/apache2/sites-available/ paste this into your terminal .
3) sudo cp 000-default.conf example1.com.conf
4) go to folder /etc/apache2/sites-available/ and open file example1.com.conf into any editor you want
5) than paste below code
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/example1/
ServerName example1.dev
ServerAlias www.example1.dev
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
6) save the file asked for password enter your root password .
7) open your terminal and run sudo a2ensite example1.com.conf
8) sudo systemctl restart apache2
or
sudo service apache2 restart
9) open file /etc/hosts
enter this 2 line
192.168.1.30 example1.dev
192.168.1.30 www.example1.dev
#Note: 192.168.1.30 is my pc ip so you write your ip
Add new product image in PrestaShop by core development
// add product image...
if(count($_FILES['files']['tmp_name']) > 1){
for($i=0;$i < count($_FILES['files']['tmp_name']);$i++ ){
$image = new Image();
$url = $_FILES['files']['tmp_name'][$i];
$shops = Shop::getShops(true, null, true);
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) +1 ;
if($i==0){
$image->cover = true; // or false;
} else {
$image->cover = false; // or false;
}
if (($image->validateFields(false, true)) === true &&($image->validateFieldsLang(false, true)) === true && $image->add()) {
$image->associateTo($shops);
if (!$this->copyImg($id_product, $image->id, $url)){
$image->delete();
}
//$combination = new Combination((int)$idProductAttribute);
//$combination->setImages($image->id);
}
}
} else {
$image = new Image();
$url = $_FILES['files']['tmp_name'][0];
$shops = Shop::getShops(true, null, true);
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&($image->validateFieldsLang(false, true)) === true && $image->add()) {
$image->associateTo($shops);
if(!$this->copyImg($id_product, $image->id, $url)){
$image->delete();
}
//$combination = new Combination((int)$idProductAttribute);
//$combination->setImages($image->id);
}
}
// add product image...
if(count($_FILES['files']['tmp_name']) > 1){
for($i=0;$i < count($_FILES['files']['tmp_name']);$i++ ){
$image = new Image();
$url = $_FILES['files']['tmp_name'][$i];
$shops = Shop::getShops(true, null, true);
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) +1 ;
if($i==0){
$image->cover = true; // or false;
} else {
$image->cover = false; // or false;
}
if (($image->validateFields(false, true)) === true &&($image->validateFieldsLang(false, true)) === true && $image->add()) {
$image->associateTo($shops);
if (!$this->copyImg($id_product, $image->id, $url)){
$image->delete();
}
//$combination = new Combination((int)$idProductAttribute);
//$combination->setImages($image->id);
}
}
} else {
$image = new Image();
$url = $_FILES['files']['tmp_name'][0];
$shops = Shop::getShops(true, null, true);
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&($image->validateFieldsLang(false, true)) === true && $image->add()) {
$image->associateTo($shops);
if(!$this->copyImg($id_product, $image->id, $url)){
$image->delete();
}
//$combination = new Combination((int)$idProductAttribute);
//$combination->setImages($image->id);
}
}
// add product image...
Add new product combination in PrestaShop by core development
$attributes=array();
$attribute is a all attribute id wich you selected at the time of product add.
$p is product id
if(count(Tools::getValue('attribute'))){
$attributes=Tools::getValue('attribute');
$i=0;
$combinationAttributes = [];
foreach ($attributes as $key => $value) {
if($value==0) continue;
$combinationAttributes[] = $value;
}
if(!empty($combinationAttributes) && !$p->productAttributeExists($combinationAttributes)){
$price =0;
$weight = 0;
$ecotax = 7;
$unit_price_impact = 0;
$quantity = 1;
$reference = "";
$supplier_reference = "";
$ean13 = "";
$default = true;
$idProductAttribute = $p->addProductAttribute(
(float)$price,
(float)$weight,
$unit_price_impact,
(float)$ecotax,
(int)$quantity,
"",
strval($reference),
strval($supplier_reference),
strval($ean13),
$default,
NULL,
NULL,
1,
"");
$p->addAttributeCombinaison($idProductAttribute, $combinationAttributes);
}
}
$attribute is a all attribute id wich you selected at the time of product add.
$p is product id
if(count(Tools::getValue('attribute'))){
$attributes=Tools::getValue('attribute');
$i=0;
$combinationAttributes = [];
foreach ($attributes as $key => $value) {
if($value==0) continue;
$combinationAttributes[] = $value;
}
if(!empty($combinationAttributes) && !$p->productAttributeExists($combinationAttributes)){
$price =0;
$weight = 0;
$ecotax = 7;
$unit_price_impact = 0;
$quantity = 1;
$reference = "";
$supplier_reference = "";
$ean13 = "";
$default = true;
$idProductAttribute = $p->addProductAttribute(
(float)$price,
(float)$weight,
$unit_price_impact,
(float)$ecotax,
(int)$quantity,
"",
strval($reference),
strval($supplier_reference),
strval($ean13),
$default,
NULL,
NULL,
1,
"");
$p->addAttributeCombinaison($idProductAttribute, $combinationAttributes);
}
}
Add new product in PrestaShop by core development
hello the below code is to add new product in PrestaShop
$p = new Product;
$p->name = Tools::getValue('name');
$p->id_category_default = Tools::getValue('category');
$p->indexed = 1;
$p->active = Tools::getValue('active');
$p->redirect_type = '404';
$p->visibility = 'both';
$p->id_supplier = 0;
$p->reference = strtolower(Tools::getValue('name'));
// $p->link_rewrite = array(
// 1 => strtolower(Tools::getValue('name')),
// 2 => strtolower(Tools::getValue('name')),
// ); // link rewrite must be array with every uses languages
//$product->minimal_quantity = 1;
//$product->show_price = 1;
$p->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => Tools::link_rewrite(Tools::getValue('name')));
$p->quantity = 1;
$p->price = Tools::getValue('price');
$p->description = Tools::getValue('Description');
$p->description_short = Tools::getValue('summary');
$p->id_sponser = $this->context->customer->id;
$p->date_add = date('Y-m-d H:i:s');
$p->date_upd = date('Y-m-d H:i:s');
$p->add();
$p->addToCategories(2,Tools::getValue('category'));
$p = new Product;
$p->name = Tools::getValue('name');
$p->id_category_default = Tools::getValue('category');
$p->indexed = 1;
$p->active = Tools::getValue('active');
$p->redirect_type = '404';
$p->visibility = 'both';
$p->id_supplier = 0;
$p->reference = strtolower(Tools::getValue('name'));
// $p->link_rewrite = array(
// 1 => strtolower(Tools::getValue('name')),
// 2 => strtolower(Tools::getValue('name')),
// ); // link rewrite must be array with every uses languages
//$product->minimal_quantity = 1;
//$product->show_price = 1;
$p->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => Tools::link_rewrite(Tools::getValue('name')));
$p->quantity = 1;
$p->price = Tools::getValue('price');
$p->description = Tools::getValue('Description');
$p->description_short = Tools::getValue('summary');
$p->id_sponser = $this->context->customer->id;
$p->date_add = date('Y-m-d H:i:s');
$p->date_upd = date('Y-m-d H:i:s');
$p->add();
$p->addToCategories(2,Tools::getValue('category'));
WooCommerce Sample plugin is adding full price when using custom option
Is your WooCommerce Sample e is adding full price of your product rathan than custome sample price than this is the solution for it
just copy and paste this code into the respective file
/wp-content/plugins/woocommerce-sample/woocommerce-sample.php
1) add this code in init() function
//custome RI code start
add_action( 'woocommerce_before_calculate_totals','cp_add_custom_price', 10, 2);
function cp_add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
if($value['sample']){
$product_id = $value['product_id'];
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}else{
$price = $value['data']->price;
}
$value['data']->set_price( $price );
}
}
}
//custome RI code start
2) Add this code inside the foreach loop of mini-cart file
note: $product_id variable is before this code ,
$_product variable is after this code
/wp-content/plugins/woocommerce/templates/cart/mini-cart.php
//custome RI code start
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if($cart_item['sample']){
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}
$cart_item['data']->set_price( $price );
//$product_price=apply_filters( 'woocommerce_cart_item_price',$price, $cart_item, $cart_item_key );
}
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
//custome RI code start
just copy and paste this code into the respective file
/wp-content/plugins/woocommerce-sample/woocommerce-sample.php
1) add this code in init() function
//custome RI code start
add_action( 'woocommerce_before_calculate_totals','cp_add_custom_price', 10, 2);
function cp_add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
if($value['sample']){
$product_id = $value['product_id'];
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}else{
$price = $value['data']->price;
}
$value['data']->set_price( $price );
}
}
}
//custome RI code start
2) Add this code inside the foreach loop of mini-cart file
note: $product_id variable is before this code ,
$_product variable is after this code
/wp-content/plugins/woocommerce/templates/cart/mini-cart.php
//custome RI code start
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if($cart_item['sample']){
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}
$cart_item['data']->set_price( $price );
//$product_price=apply_filters( 'woocommerce_cart_item_price',$price, $cart_item, $cart_item_key );
}
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
//custome RI code start
Subscribe to:
Posts
(
Atom
)