PrestaShop: Product Youtube Video

Trabla: PrestaShop - Product Youtube Video


PrestaShop eCommerce shop - add  Product Youtube Video to shop template - codingtrabla tutorial 1

Admin Product Edit Form:

PrestaShop eCommerce shop - add  Product Youtube Video to shop template - codingtrabla tutorial 2

Environment: PrestaShop v1.6.0.9

Solving Overview:
1. Add column "youtube_url" into "ps_product" table
2. Extend ProductCore class into Product using PrestaShop override principle - add support of new field.
3. Change Admin template "informations.tpl" - add support of "youtube_url"
4. Change theme tempalete "product.tpl" - add youtube iframe
5. Clear shop cache



Solving Details:
1. Add column "youtube_url" into "ps_product" table

ALTER TABLE `ps_product` ADD COLUMN `youtube_url` varchar(500) DEFAULT NULL;

2. Extend ProductCore class into Product using PrestaShop override principle - add support of new field:
- create file Product.php
- copy file into folder .../prestashop/override/classes/
- edit file .../prestashop/override/classes/Product.php add following code

<?php

/**
 * Description of Product
 *
 * @author samuraikit
 */
class Product extends ProductCore {
   
    public $youtube_url = '';
    
    public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
// Classic fields
'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'supplier_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'location' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64),
'width' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'height' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'depth' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'weight' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'quantity_discount' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),
                    
                        //Youtube Url
                        'youtube_url' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl', 'size' => 500),
                    
'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
'cache_is_pack' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'cache_has_attachments' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'is_virtual' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),

/* Shop fields */
'id_category_default' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'id_tax_rules_group' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'on_sale' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'online_only' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'ecotax' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'minimal_quantity' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'required' => true),
'wholesale_price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'unity' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'),
'unit_price_ratio' => array('type' => self::TYPE_FLOAT, 'shop' => true),
'additional_shipping_cost' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'),
'customizable' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'text_fields' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'uploadable_files' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedInt'),
'active' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'redirect_type' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isString'),
'id_product_redirected' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId'),
'available_for_order' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'available_date' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
'condition' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isGenericName', 'values' => array('new', 'used', 'refurbished'), 'default' => 'new'),
'show_price' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'indexed' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'visibility' => array('type' => self::TYPE_STRING, 'shop' => true, 'validate' => 'isProductVisibility', 'values' => array('both', 'catalog', 'search', 'none'), 'default' => 'both'),
'cache_default_attribute' => array('type' => self::TYPE_INT, 'shop' => true),
'advanced_stock_management' => array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
'date_upd' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),

/* Lang fields */
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
'link_rewrite' => array(
'type' => self::TYPE_STRING, 
'lang' => true, 
'validate' => 'isLinkRewrite', 
'required' => true, 
'size' => 128,
'ws_modifier' => array(
'http_method' => WebserviceRequest::HTTP_POST,
'modifier' => 'modifierWsLinkRewrite'
)
),
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'description_short' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'available_now' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'available_later' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'IsGenericName', 'size' => 255),
),
'associations' => array(
'manufacturer' => array('type' => self::HAS_ONE),
'supplier' => array('type' => self::HAS_ONE),
'default_category' => array('type' => self::HAS_ONE, 'field' => 'id_category_default', 'object' => 'Category'),
'tax_rules_group' => array('type' => self::HAS_ONE),
'categories' => array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),
'stock_availables' => array('type' => self::HAS_MANY, 'field' => 'id_stock_available', 'object' => 'StockAvailable', 'association' => 'stock_availables'),
),
);
}

3. Change Admin template "informations.tpl" - add support of "youtube_url"

File is located in admin folder
.../prestashop/admin3652/themes/default/template/controllers/products/informations.tpl
where admin3652 is admin folder, should be changed to yours


<div class="form-group">
<label class="control-label col-lg-3" for="upc">
<span class="label-tooltip" data-toggle="tooltip"
title="{l s='This type of product code is widely used in the United States, Canada, the United Kingdom, Australia, New Zealand and in other countries.'}">
{$bullet_common_field} {l s='UPC barcode'}
</span>
</label>
<div class="col-lg-3">
<input maxlength="12" type="text" id="upc" name="upc" value="{$product->upc|escape:'html':'UTF-8'}" />
</div>
</div>
             
        <!-- SamuraiKit START -->        
        <div class="form-group">
<label class="control-label col-lg-3" for="youtube_url">
<span class="label-tooltip" data-toggle="tooltip"
title="{l s='YouTube Url'}">
{$bullet_common_field} {l s='YouTube Url'}
</span>
</label>
<div class="col-lg-3">
<input maxlength="500" type="text" id="youtube_url" name="youtube_url" value="{$product->youtube_url|escape:'html':'UTF-8'}" />
</div>
</div>
        <!-- SamuraiKit End -->    

<hr/>

{* status informations *}
<div class="form-group">
<div class="col-lg-1"><span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="active" type="radio" onclick=""}</span></div>
<label class="control-label col-lg-2">
{l s='Enabled'}
</label>
<div class="col-lg-9">



4. Change theme tempalete "product.tpl" - add youtube iframe
File is located in admin folder
.../prestashop/themes/default-bootstrap/product.tpl

<span>{l s='Up to'}</span>
{if $quantity_discount.price >= 0 || $quantity_discount.reduction_type == 'amount'}
{$discountPrice=$productPrice-$quantity_discount.real_value|floatval}
{else}
{$discountPrice=$productPrice-($productPrice*$quantity_discount.reduction)|floatval}
{/if}
{$discountPrice=$discountPrice*$quantity_discount.quantity}
{$qtyProductPrice = $productPrice*$quantity_discount.quantity}
{convertPrice price=$qtyProductPrice-$discountPrice}
</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
</section>
                                                
                       
{/if}
             
             
             <!-- SamuraiKit START -->
                {if isset($product->youtube_url) }
                    <iframe width="640" height="360" src="{$product->youtube_url}" frameborder="0" allowfullscreen></iframe>
                {/if}
                <!-- SamuraiKit END -->
             
             
             
{if isset($features) && $features}
<!-- Data sheet -->
<section class="page-product-box">
<h3 class="page-product-heading">{l s='Data sheet'}</h3>
<table class="table-data-sheet">
{foreach from=$features item=feature}
<tr class="{cycle values="odd,even"}">
{if isset($feature.value)}
<td>{$feature.name|escape:'html':'UTF-8'}</td>
<td>{$feature.value|escape:'html':'UTF-8'}</td>
{/if}
</tr>
{/foreach}
</table>
</section>
<!--end Data sheet -->
{/if}
{if $product->description}



5. Clear shop cache

Note: use YouTube embed url

PrestaShop eCommerce shop - add  Product Youtube Video to shop template - codingtrabla tutorial 3





















Check out source files on GitHub:
https://github.com/samuraikit/prestashop-product-youtube

No comments:

Post a Comment