simple html dom - cannot redeclair str_get_html() - simple-html-dom

Im trying to make a class to help with SEO and also compare google API with actual results
class:
<?php
class true_seo {
public $string, $amount;
private $arr;
public function __construct(){}
public function set_g_key( $key ) {
$this->g_key = $key;
}
public function set_phrase( $string ){
if( is_string ( $string ) ) {
$string = array( $string );
}
if( is_array ( $string ) ) {
$this->phrases = $string;
}else{
Throw new exception("incorect input for phrase, string or array");
}
}
public function get_sites_use_spider( $amount ) {
require "simple_html_dom.php";
$main_result = array();
foreach( $this->phrases as $phrase ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
$data = true_seo::google_search_api( $APIparams, 'http://www.google.co.uk/search', false );
new simple_html_dom();
$html = str_get_html( $data );
$result = array();
foreach( $html->find('li.g h3 a') as $g ) {
$data = $g->parent()->nextSibling();
$other = $data->find('span a');
$x = 0;
foreach( $other as $d ) {
( $x == 0 ? $cache = $d->href : $simular = $d->href );
$x++;
}
$excess_span = $data->find('span',0)->outertext;
if( isset( $data->find('div',0)->tag ) ) {
$excess_div = $data->find('div',0)->outertext;
$title = str_replace( array( $excess_span, $excess_div, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','','' ), $data->outertext );
}else{
$title = str_replace( array( $excess_span, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','' ), $data->outertext );
}
$result[] = array( 'link' => $g->href, 'title' => strip_tags( $title ), 'cache' => $cache, 'simular' => 'http://www.google.co.uk' . $simular );
}
$main_result[$phrase] = $result;
$html->clear();
}
$this->non_api_data = $main_result;
}
public function get_sites_use_api( $amount ) {
$arr = array();
foreach( $this->phrases as $phrase ) {
if( $amount > 4 ) {
$times = $amount / 4;
}else{
$times = 1;
}
$arg = array();
for($x = 0; $x < $times; $x++ ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => ($x * 4), "maxResults" => 4, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
if( $data = true_seo::google_search_api( $APIparams, 'http://ajax.googleapis.com/ajax/services/search/web' ) ) {
$arg = array_merge($arg, $data->responseData->results);
}else{
Throw new exception("Request error: no results returned from Google.");
}
}
$arg = array_reverse( $arg );
$remove = $amount % 4;
if( $amount < 4 ) {
$remove = 4 - $amount;
}
for( $x=0; $x < $remove; $x++ ) {
unset( $arg[$x] );
}
$arg = array_reverse( $arg );
foreach( $arg as $g ) {
$result = array( 'link' => $g->url, 'title' => strip_tags( $g->content ), 'cache' => $g->cacheUrl, 'simular' => 'na' );
$arr[$phrase][] = $result;
}
}
$this->api_data = $arr;
}
public function google_search_api($args, $url, $api = true){
if ( !array_key_exists('v', $args) ) {
$args['v'] = '1.0';
}
$url .= '?'.http_build_query($args, '', '&');
if( $result = #file_get_contents($url) ) {
if( $api == true ) {
return json_decode($result);
}else{
return $result;
}
}else{
Throw new exception("No data returned from url: $url");
}
}
public function set_get_actual( $string ) {
$this->actual->name = $string;
$this->actual->data = file_get_contents( $string );
}
public function get_actual_description(){
require_once "simple_html_dom.php";
new simple_html_dom();
$html = str_get_html( $this->actual->data );
return $html->find('head meta[name=description]',0)->content;
$html->clear();
}
}
?>
called by :
<?php
try{
require "./classes/class_true_seo.php";
$seo = new true_seo();
$seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSl_WmKnmsXGmN0kkjN2wkrfEOY-hT2sL-_x5v4NtT3DgElKNsR7FDJDQ');
$seo->set_phrase(array("web design mansfield"));
$seo->get_sites_use_api(10);
ob_start();
foreach( $seo->api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
$seo->set_get_actual( $results['link'] );
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
#echo "<p>" . $seo->get_actual_title() . "</p>";
echo "<p>" . $seo->get_actual_description() . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('p') . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('h2') . "</p>";
}
}
$api_return = ob_get_clean();
ob_start();
$seo->get_sites_use_spider(10);
foreach( $seo->non_api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
}
}
$non_api_return = ob_get_clean();
}catch(Exception $err){
$error = $err->getMessage();
}
?>
My problem being that I keep getting the error:
Fatal error: Cannot redeclare file_get_html() (previously declared in C:\wamp\www\seo\classes\simple_html_dom.php:37) in C:\wamp\www\seo\classes\simple_html_dom.php on line 41
which is due to the last function in the class get_actual_description().
Can anyone see where im cocking up?
regards,
Phil

put require_once "simple_html_dom.php" outside of function

Related

Woocommerce update order value after updating cart discount

I want to add maximum discount limit on % discount coupons. I've added this code to do so in cart, but payment gateway is still getting the % discount without the maximum limit set by this code. Will I have to update any other value to fix this?
Payment gateway plugging is getting the total using get_total method.
add_action( 'woocommerce_coupon_options_usage_limit', 'woocommerce_coupon_options_usage_limit', 10, 2 );
function woocommerce_coupon_options_usage_limit( $coupon_id, $coupon )
{
echo '';
// max discount per coupons
$max_discount = get_post_meta( $coupon_id, '_max_discount', true );
woocommerce_wp_text_input( array(
'id' => 'max_discount',
'label' => __( 'Usage max discount', 'woocommerce' ),
'placeholder' => esc_attr( $max_discount, 'woocommerce' ),
'description' => __( 'The maximum discount this coupon can give.', 'woocommerce' ),
'type' => 'number',
'desc_tip' => true,
'class' => 'short',
'custom_attributes' => array(
'step' => 1,
'min' => 0,
),
'value' => $max_discount ? $max_discount : '',
) );
echo '';
}
add_action( 'woocommerce_coupon_options_save', 'woocommerce_coupon_options_save', 10, 2 );
function woocommerce_coupon_options_save( $coupon_id, $coupon ) {
update_post_meta( $coupon_id, '_max_discount', wc_format_decimal( $_POST['max_discount'] ) );
}
// filter to change discount if over max coupon amount
function filter_woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $instance ) {
$cartCoupons = WC()->cart->get_applied_coupons();
foreach ($cartCoupons as $key => $appliedCoupon) {
$coupon = new WC_Coupon($appliedCoupon);
$couponType = get_post_meta( $coupon->get_id(), 'discount_type', true );
if ($couponType == 'percent') {
$maxCouponAmount = get_post_meta( $coupon->get_id(), '_max_discount', true );
$excludedProducts = explode(",", get_post_meta( $coupon->get_id(), 'exclude_product_ids', true ));
$cartLines = count(WC()->cart->get_cart());
$cartLineItems = WC()->cart->get_cart();
foreach ($cartLineItems as $cartItem){
$cartProductID[] = $cartItem['product_id'];
if (!empty($excludedProducts)) {
$cartLinesWithoutExcluded = array_intersect($cartProductID,$excludedProducts);
} else {
$cartLinesWithoutExcluded = $cartProductID;
}
$cartLinesWithoutExcluded = count($cartLinesWithoutExcluded);
$totalCartItems = $cartLines - $cartLinesWithoutExcluded;
$discount = $maxCouponAmount / $totalCartItems;
}
} else {
$discount = 0.00;
}
return $discount;
}
}
// apply the coupon whether it is max discount or a product price adjustment
function apply_max_amount_or_product_price_adjustment(){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
if ( !is_admin() && !wp_is_json_request() ) {
global $wp, $woocommerce, $post;
$cartCoupons = WC()->cart->get_applied_coupons();
foreach ($cartCoupons as $key => $appliedCoupon) {
$coupon = new WC_Coupon($appliedCoupon);
$maxCouponAmount = get_post_meta( $coupon->get_id(), '_max_discount', true );
$excludedProducts = explode(",", get_post_meta( $coupon->get_id(), 'exclude_product_ids', true ));
$couponType = get_post_meta( $coupon->get_id(), 'discount_type', true );
// $fixedProductPrice = get_post_meta( $coupon->get_id(), '_adjust_price', true );
$couponAmount = WC()->cart->get_coupon_discount_amount( $appliedCoupon );
if (!empty($maxCouponAmount) && $couponType == 'percent' && ($couponAmount >= $maxCouponAmount)) {
$couponAmount = add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
}
if ($couponType == 'fixed_cart') {
$cart = WC()->cart->get_cart();
$couponProducts = explode(',',get_post_meta( $coupon->get_id(), 'product_ids', true ));
$fixedPricePerProduct = get_post_meta( $coupon->get_id(), '_price_per_product_amt', true );
foreach( $cart as $cart_item ) {
if (in_array($cart_item['data']->get_parent_id(), $couponProducts)) {
$cart_item['data']->set_price( $fixedPricePerProduct );
}
}
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'apply_max_amount_or_product_price_adjustment', 10, 1);
add_action('woocommerce_coupon_options_usage_limit', 'woocommerce_coupon_options_usage_limit', 10, 2);
function woocommerce_coupon_options_usage_limit($coupon_id, $coupon)
{
echo '';
// max discount per coupons
$max_discount = get_post_meta($coupon_id, '_max_discount', true);
woocommerce_wp_text_input(array(
'id' => 'max_discount',
'label' => __('Usage max discount', 'woocommerce'),
'placeholder' => esc_attr($max_discount, 'woocommerce'),
'description' => __('The maximum discount this coupon can give.', 'woocommerce'),
'type' => 'number',
'desc_tip' => true,
'class' => 'short',
'custom_attributes' => array(
'step' => 1,
'min' => 0,
),
'value' => $max_discount ? $max_discount : '',
));
echo '';
}
add_action('woocommerce_coupon_options_save', 'woocommerce_coupon_options_save', 10, 2);
function woocommerce_coupon_options_save($coupon_id, $coupon)
{
update_post_meta($coupon_id, '_max_discount', wc_format_decimal($_POST['max_discount']));
}
// filter to change discount if over max coupon amount
function filter_woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $instance)
{
$cartCoupons = WC()->cart->get_applied_coupons();
foreach ($cartCoupons as $key => $appliedCoupon) {
$coupon = new WC_Coupon($appliedCoupon);
$couponType = get_post_meta($coupon->get_id(), 'discount_type', true);
if ($couponType == 'percent') {
$maxCouponAmount = get_post_meta($coupon->get_id(), '_max_discount', true);
$excludedProducts = explode(",", get_post_meta($coupon->get_id(), 'exclude_product_ids', true));
$cartLines = count(WC()->cart->get_cart());
$cartLineItems = WC()->cart->get_cart();
foreach ($cartLineItems as $cartItem) {
$cartProductID[] = $cartItem['product_id'];
if (!empty($excludedProducts)) {
$cartLinesWithoutExcluded = array_intersect($cartProductID, $excludedProducts);
} else {
$cartLinesWithoutExcluded = $cartProductID;
}
$cartLinesWithoutExcluded = count($cartLinesWithoutExcluded);
$totalCartItems = $cartLines - $cartLinesWithoutExcluded;
$new_discount = $maxCouponAmount / $totalCartItems;
if ($new_discount < $discount) {
$discount = $new_discount;
}
}
} else {
$discount = 0.00;
}
return $discount;
}
}
// apply the coupon whether it is max discount or a product price adjustment
function apply_max_amount_or_product_price_adjustment()
{
if (is_admin() && !defined('DOING_AJAX'))
return;
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
if (!is_admin() && !wp_is_json_request()) {
global $wp, $woocommerce, $post;
$cartCoupons = WC()->cart->get_applied_coupons();
foreach ($cartCoupons as $key => $appliedCoupon) {
$coupon = new WC_Coupon($appliedCoupon);
$maxCouponAmount = get_post_meta($coupon->get_id(), '_max_discount', true);
$excludedProducts = explode(",", get_post_meta($coupon->get_id(), 'exclude_product_ids', true));
$couponType = get_post_meta($coupon->get_id(), 'discount_type', true);
// $fixedProductPrice = get_post_meta( $coupon->get_id(), '_adjust_price', true );
$couponAmount = WC()->cart->get_coupon_discount_amount($appliedCoupon);
if (!empty($maxCouponAmount) && $couponType == 'percent' && ($couponAmount >= $maxCouponAmount)) {
$couponAmount = add_filter('woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5);
}
if ($couponType == 'fixed_cart') {
$cart = WC()->cart->get_cart();
$couponProducts = explode(',', get_post_meta($coupon->get_id(), 'product_ids', true));
$fixedPricePerProduct = get_post_meta($coupon->get_id(), '_price_per_product_amt', true);
foreach ($cart as $cart_item) {
if (in_array($cart_item['data']->get_parent_id(), $couponProducts)) {
$cart_item['data']->set_price($fixedPricePerProduct);
}
}
}
}
}
}
add_action('woocommerce_before_calculate_totals', 'apply_max_amount_or_product_price_adjustment', 10, 1);
add_filter('woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5);

How insert title attribute in a template Wordpress

my wordpress template does not display the title attribute of the menu (but if I change the template it is okay), how could i solve it? the website in www.imsdesign.eu
I am assuming that you added title attributes to you menu by doing below steps:
From your WordPress dashboard, go to your Menus page (Appearance > Menus)
Select the Menu that you want to edit from the list of your Menus.
Click on the configuration arrow on the right side of the Menu Item title, that when clicked opens the configuration box.
Click on the “Screen Options” on top of the page to reveal additional advanced menu properties.
Activate the option for Title Attribute by selecting the Title Attribute box to expose the settings box under the Menu item instantly.
Add your Title Attribute in the text box just below the Navigation label option.
If it is still not working that's mean that you WP theme does not has title attributes. To change it please find the correct WP template (probably header.php) and add title attributes to nav elements by using method the_title_attribute()
<?php
class FastwpAltiusMenu {
private static $nav_template = '';
private static $nav_overlay = '';
function __construct() {
global $fastwp_altius_data, $fastwp_altius_menu_type;
$templates = array();
$templates[1] = array( 'nav' => 'fastwp-menu-default', 'overlay' => '' );
$templates[2] = array( 'nav' => 'fastwp-menu-overlay', 'overlay' => 'fastwp-menu-overlay2' );
if( is_page() ) {
$fastwp_altius_meta_type = get_post_meta( get_the_ID(), 'p_nav_style', true );
if( $fastwp_altius_meta_type == 1 || $fastwp_altius_meta_type == 2 ) {
$fastwp_altius_data['nav_style'] = $fastwp_altius_meta_type;
}
}
$menu = !empty( $fastwp_altius_data['nav_style'] ) && in_array( (int) $fastwp_altius_data['nav_style'], array_keys( $templates ) ) ? (int) $fastwp_altius_data['nav_style'] : 1;
$fastwp_altius_menu_type= $menu;
self::$nav_template = $templates[$menu]['nav'];
self::$nav_overlay = $templates[$menu]['overlay'];
}
public static function nav( $echo = true ) {
if( (boolean) $echo ) {
echo get_template_part( 'fastwp/menu/' . self::$nav_template );
}
ob_start();
get_template_part( 'fastwp/menu/' . self::$nav_template );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
public static function overlay( $echo = true ) {
$nav_overlay = self::$nav_overlay;
if( !empty( $nav_overlay ) ) {
if( (boolean) $echo ) {
echo get_template_part( 'fastwp/menu/' . $nav_overlay );
}
ob_start();
get_template_part( 'fastwp/menu/' . $nav_overlay );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
}
class FastwpAltiusWalker {
var $one_page = false;
function __construct( $menu_id, $menu = 0 ) {
$this->override_menu = $menu;
$elements = $this->nice_elements( $menu_id );
$this->main_elements = isset( $elements['main'] ) ? $elements['main'] : array();
$this->subnav_elements = isset( $elements['subnav'] ) ? $elements['subnav'] : array();
}
function set_onePage() {
$this->one_page = true;
}
function add_one_page_section( $item ) {
if( $this->one_page ) {
global $fastwp_altius_one_page_sections;
if( $item->object == 'page' ) {
if( empty( $fastwp_altius_one_page_sections ) ) $fastwp_altius_one_page_sections = array();
$section_name = preg_replace( '/[^a-z0-9_]/i', '', str_replace( ' ', '_', $item->title ) );
if( in_array( $section_name, array_keys( $fastwp_altius_one_page_sections ) ) ) {
$section_name = $section_name . '_' . $item->ID;
}
$fastwp_altius_one_page_sections[$section_name] = $item->object_id;
return '#section-' . $section_name;
}
}
return $item->url;
}
function get_menu() {
$output = '';
if( !empty( $this->main_elements ) ) {
/* Add Visual Composer meta */
global $fastwp_altius_one_page_css_vc;
$fastwp_altius_one_page_css_vc = array();
foreach( $this->main_elements as $item ) {
$item = $item[0];
$typeparent = get_post_meta( $item->ID, '_menu_item_menutype', true );
/* Add Visual Composer meta */
if( isset( $item->object_id ) ) {
$fastwp_altius_one_page_css_vc[$item->object_id] = get_post_meta( $item->object_id, '_wpb_shortcodes_custom_css', true );
}
$a_classes = $li_classes = $a_atts = $li_atts = array();
if( get_the_ID() == $item->object_id ) {
$li_classes[] = 'current';
}
if( isset( $item->hasChildren ) && $item->hasChildren ) {
$a_classes[] = 'sf-with-ul';
}
if( isset( $this->subnav_elements[$item->ID] ) ) {
$li_classes[] = 'parentMenuItem';
}
if( !empty( $item->classes ) ) {
$a_classes[] = implode( ' ', $item->classes );
}
$childrens = '';
if( $typeparent == 'mega2' ) {
$li_classes[] = 'dropDown megaMenu';
$items_array = $this->levels_recursive_array( $item->ID );
unset( $items_array['subnav'] );
$items_per_col = ceil( count( $items_array ) / 2 );
$col1_items = $col2_items = '';
foreach( $items_array as $k => $mitem ) {
$typechild = get_post_meta( $mitem->ID, '_menu_item_menutype2', true );
if( $k >= $items_per_col ) {
$col2_items .= $this->link_type( $mitem, $typechild );
} else {
$col1_items .= $this->link_type( $mitem, $typechild );
}
}
$childrens = '<div class="megaMenuContent cols2 clearfix">';
$childrens .= '<ul>' . $col1_items . '</ul>';
$childrens .= '<ul>' . $col2_items . '</ul>';
$childrens .= '</div>';
} else if( $typeparent == 'mega3' ) {
$li_classes[] = 'dropDown megaMenu';
$items_array = $this->levels_recursive_array( $item->ID );
unset( $items_array['subnav'] );
$items_per_col = ceil( count( $items_array ) / 3 );
$col1_items = $col2_items = $col3_items = '';
foreach( $items_array as $k => $mitem ) {
$typechild = get_post_meta( $mitem->ID, '_menu_item_menutype2', true );
if( $k < $items_per_col ) {
$col1_items .= $this->link_type( $mitem, $typechild );
} else if( $k/2 >= $items_per_col ) {
$col3_items .= $this->link_type( $mitem, $typechild );
} else {
$col2_items .= $this->link_type( $mitem, $typechild );
}
}
$childrens = '<div class="megaMenuContent cols3 clearfix">';
$childrens .= '<ul>' . $col1_items . '</ul>';
$childrens .= '<ul>' . $col2_items . '</ul>';
$childrens .= '<ul>' . $col3_items . '</ul>';
$childrens .= '</div>';
} else {
$childrens = $this->levels_recursive( $item->ID );
}
$output .= sprintf( "\n" . '<li%s%s><a href="%s"%s%s>%s</a>%s</li>',
( !empty( $li_classes ) ? ' class="' . implode( $li_classes, ' ' ) . '"' : '' ),
( !empty( $li_atts ) ? ' ' . implode( ' ', $li_atts ) : '' ),
$this->add_one_page_section( $item ),
( !empty( $a_classes ) ? ' class="' . implode( $a_classes, ' ' ) . '"' : ' ' ),
( !empty( $a_atts ) ? ' ' . implode( ' ', $a_atts ) : '' ),
$item->title,
$childrens );
}
}
return $output;
}
function link_type( $item = '', $type = '' ) {
$li_classes = array();
if( get_the_ID() == $item->object_id ) {
$li_classes[] = 'current';
}
if( $type == 'title' ) {
return '<li class="megaMenuTitle' . ( !empty( $li_classes ) ? ' ' . implode( ' ', $li_classes ) : '' ) . '">' . $item->title . '</li>';
} else {
return '<li' . ( !empty( $li_classes ) ? ' class="' . implode( ' ', $li_classes ) . '"' : '' ) . '>' . $item->title . '</li>';
}
}
function levels_recursive( $parent = 0, $level = 1 ) {
if( empty( $parent ) ) return ;
$output = '';
if( isset( $this->subnav_elements[$parent] ) ) {
$output .= '<ul class="fastwp-menu-level-' . $level . '">';
foreach( $this->subnav_elements[$parent] as $item ) {
$output .= '<li' . ( $this->item_has_children( $item->ID ) ? ' class="dropDown"' : '' ) . '>' . $item->title . '';
$output .= $this->levels_recursive( $item->ID, ($level+1) );
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
function item_has_children( $item = '' ) {
if( in_array( $item, array_keys( $this->subnav_elements ) ) ) {
return true;
}
return false;
}
function levels_recursive_array( $parent = 0 ) {
if( empty( $parent ) ) return array();
$output = array();
if( isset( $this->subnav_elements[$parent] ) ) {
foreach( $this->subnav_elements[$parent] as $item ) {
$output[] = $item;
$output['subnav'] = $this->levels_recursive( $item->ID );
}
}
return $output;
}
function nice_elements( $menu_id ) {
$elements = array();
foreach( $this->get_elements( $menu_id ) as $element ) {
if( !empty( $element->menu_item_parent ) ) {
$elements['subnav'][$element->menu_item_parent][] = $element;
} else {
$elements['main'][$element->ID][] = $element;
}
}
return $elements;
}
function get_elements( $menu_id = 'primary' ) {
$locations = get_nav_menu_locations();
if( !empty( $this->override_menu ) ) {
$menu = $this->override_menu;
} else {
$menu = $locations[$menu_id];
}
$elems = wp_get_nav_menu_items( get_term( $menu, 'nav_menu' ), array( 'depth' => 1 ) );
$return_elems = array();
foreach( $elems as $elem ) {
$return_elems[] = $elem;
}
return $return_elems;
}
}

how to add multi select category in visual composer

How can I add a multi-selector for the Visual composer?
vc_map( array(
"name" => esc_html__("Ajax Posts", '7mag'),
"base" => "YT_ajaxpost",
"icon" => "YT_vc_ico_ajaxpost",
"class" => "YT_vc_sc_ajaxpost",
"category" => esc_html__("7mag", '7mag'),
"params" => array(
array(
"type" => "dropdown",
"heading" => esc_html__("Style", '7mag'),
"param_name" => "multiple",
"value" => $categories_array,
),
) );
For example:
link
You need to register new shortcode attribute.
// Create multi dropdown param type
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );
function dropdown_multi_settings_field( $param, $value ) {
$param_line = '';
$param_line .= '<select multiple name="'. esc_attr( $param['param_name'] ).'" class="wpb_vc_param_value wpb-input wpb-select '. esc_attr( $param['param_name'] ).' '. esc_attr($param['type']).'">';
foreach ( $param['value'] as $text_val => $val ) {
if ( is_numeric($text_val) && (is_string($val) || is_numeric($val)) ) {
$text_val = $val;
}
$text_val = __($text_val, "js_composer");
$selected = '';
if(!is_array($value)) {
$param_value_arr = explode(',',$value);
} else {
$param_value_arr = $value;
}
if ($value!=='' && in_array($val, $param_value_arr)) {
$selected = ' selected="selected"';
}
$param_line .= '<option class="'.$val.'" value="'.$val.'"'.$selected.'>'.$text_val.'</option>';
}
$param_line .= '</select>';
return $param_line;
}
Then use it:
array(
"type" => "dropdown_multi",
"heading" => esc_html__("Style", '7mag'),
"param_name" => "multiple",
"value" => $categories_array,
),
I've updated the above solution. due to $value!=='' check the values were not being selected after save.
function dropdown_multi_settings_field( $param, $value ) {
if ( ! is_array( $value ) ) {
$param_value_arr = explode( ',', $value );
} else {
$param_value_arr = $value;
}
$param_line = '';
$param_line .= '<select multiple name="' . esc_attr( $param['param_name'] ) . '" class="wpb_vc_param_value wpb-input wpb-select ' . esc_attr( $param['param_name'] ) . ' ' . esc_attr( $param['type'] ) . '">';
foreach ( $param['value'] as $text_val => $val ) {
if ( is_numeric( $text_val ) && ( is_string( $val ) || is_numeric( $val ) ) ) {
$text_val = $val;
}
$selected = '';
if ( ! empty( $param_value_arr ) && in_array( $val, $param_value_arr ) ) {
$selected = ' selected="selected"';
}
$param_line .= '<option class="' . $val . '" value="' . $val . '"' . $selected . '>' . $text_val . '</option>';
}
$param_line .= '</select>';
return $param_line;
}
vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );

WooCommerce Smart Coupon generated from which Item?

I am writing a business extension (pdf printing) for the smart coupons.
Now I need a relation from the generated coupon (of the order) and the order item.
For example, which order item has generated the coupon?
Is there a way to get the order item_id from the coupon?
Is use this code to get the coupons:
$coupons = get_post_meta($order_id, 'sc_coupon_receiver_details', true));
Thank you very much
I have found a solution, but i had to put the code inside "woocommerce-smart-coupons.php" file.
Line 379: Extend the Filter Variables
add_filter( 'generate_smart_coupon_action', array( $this, 'generate_smart_coupon_action' ), 1, 10 );
Line 4783: Extend the Variable with the $item_id
if( $this->is_coupon_amount_pick_from_product_price( array( $coupon_title ) ) ) {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $sc_called_credit_details[$item_id] . ':' . $item_id;
} else {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $coupon->amount . ':' . $item_id;
}
Line 4816: Get the $item_id from the details and refer it to the "generate_smart_coupon" method
foreach ( $email_to_credit[$email_id] as $coupon_credit => $qty ) {
$coupon_details = explode( ':', $coupon_credit );
$coupon_title = get_the_title( $coupon_details[0] );
$coupon = new WC_Coupon( $coupon_title );
$credit_amount = $coupon_details[1];
$item_id = $coupon_details[2];
$message_index = array_search( $email_id, $email[$coupon->id], true );
if ( $message_index !== false && isset( $receivers_messages[$coupon->id][$message_index] ) && !empty( $receivers_messages[$coupon->id][$message_index] ) ) {
$message_from_sender = $receivers_messages[$coupon->id][$message_index];
} else {
$message_from_sender = '';
}
for ( $i = 0; $i < $qty; $i++ ) {
if ( $coupon->type != 'smart_coupon' ) continue; // only process smart_coupon here, rest coupon will be processed by function update_coupon
$this->generate_smart_coupon( $email_id, $credit_amount, $order_id, $coupon, 'smart_coupon', $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
$smart_coupon_codes = array();
}
}
Line 5194: Change the Method and extend the variables
public function generate_smart_coupon( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
return apply_filters( 'generate_smart_coupon_action', $email, $amount, $order_id, $coupon, $discount_type, $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
}
Line 5212: Extend the filter method to
public function generate_smart_coupon_action( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
Line 5307: Store the $item_id in the post_meta
update_post_meta( $smart_coupon_id, 'apply_before_tax', $apply_before_tax );
update_post_meta( $smart_coupon_id, 'free_shipping', $free_shipping );
update_post_meta( $smart_coupon_id, 'product_categories', $product_categories );
update_post_meta( $smart_coupon_id, 'exclude_product_categories', $exclude_product_categories );
update_post_meta( $smart_coupon_id, 'generated_from_order_id', $order_id );
update_post_meta( $smart_coupon_id, 'generated_from_item_id', $item_id );

Adding weighted tags in wordpress

Is there a way I can add weighted tags to wordpress? I need to customize the tag cloud widget to show the most used tags with different colors
I've stumbled upon this as well. Here's a modified function from the original tag cloud widget:
<?php
function my_colorful_tag_cloud( $args = array() ) {
$defaults = array(
'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
);
$args = wp_parse_args( $args, $defaults );
$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
if ( empty( $tags ) || is_wp_error( $tags ) )
return;
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), $tag->taxonomy );
if ( is_wp_error( $link ) )
return false;
$tags[ $key ]->link = $link;
$tags[ $key ]->id = $tag->term_id;
}
$defaults = array(
'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
'topic_count_text_callback' => 'default_topic_count_text',
'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
);
if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
$body = 'return sprintf (
_n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
number_format_i18n( $count ));';
$args['topic_count_text_callback'] = create_function('$count', $body);
}
$args = wp_parse_args( $args, $defaults );
extract( $args );
if ( empty( $tags ) )
return;
$tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
$tags = $tags_sorted;
unset($tags_sorted);
} else {
if ( 'RAND' == $order ) {
shuffle($tags);
} else {
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
if ( 'name' == $orderby )
uasort( $tags, '_wp_object_name_sort_cb' );
else
uasort( $tags, '_wp_object_count_sort_cb' );
if ( 'DESC' == $order )
$tags = array_reverse( $tags, true );
}
}
if ( $number > 0 )
$tags = array_slice($tags, 0, $number);
$counts = array();
$real_counts = array(); // For the alt tag
foreach ( (array) $tags as $key => $tag ) {
$real_counts[ $key ] = $tag->count;
$counts[ $key ] = $topic_count_scale_callback($tag->count);
}
$min_count = min( $counts );
$spread = max( $counts ) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread < 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
$a = array();
$colors = 6;
foreach ( $tags as $key => $tag ) {
$count = $counts[ $key ];
$real_count = $real_counts[ $key ];
$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
$tag_name = $tags[ $key ]->name;
$class = 'color-' . ( round( ( $smallest + ( ( $count - $min_count ) * $font_step ) ) - ( $smallest - 1 ) ) );
$a[] = "<a href='$tag_link' class='tag-link-$tag_id $class' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
. "$unit;'>$tag_name</a>";
}
$return = join( $separator, $a );
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
}
This code (by default)will generate a tag cloud with 13 possible classes for each tag(since the defaults are min. size=10 and max.size = 22). In order to change the color for each tag weight, simply add those rules to your CSS:
.colorful_tag_cloud a.color-x { color: {color} }
where "x" is a number from 1 to 13
Hope this helps.

Resources