How to group Stripe charges by key - php

I'm looking to create a Donation Website where the amounts would be grouped by team. I'm trying to get all charges via Stripe and then group them.
How could I get the total amount donated for each team ?
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
function TheMainFunction(){
\Stripe\Stripe::setApiKey("KEY");
$charges = \Stripe\Charge::all();
$teams = array();
foreach($charges->data as $charge) if(isset($charge->metadata['team']) ){
if( !in_array_r($charge->metadata['team'], $teams ) ){
array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $charge->amount) );
}
else{
foreach( $teams as $k => $v ){
if( $v['team'] == $charge->metadata['team']){
// The action should be done here
}
}
}
}
}

I found how!
function TheMainFunction(){
\Stripe\Stripe::setApiKey("KEY");
$charges = \Stripe\Charge::all();
$teams = array();
foreach($charges->data as $charge) if(isset($charge->metadata['team']) ){
if( !in_array_r($charge->metadata['team'], $teams ) ){
array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $charge->amount) );
}
else{
foreach( $teams as $k => $v ){
if( $v['team'] == $charge->metadata['team']){
$oldvalue = $v['amount'];
$newvalue = $oldvalue+$charge->amount;
unset($teams[$k]);
array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $newvalue) );
}
}
}
}
}

Related

Place ads between two text-only paragraphs

I am using the following code to place ads between text-only paragraphs. However, the problem is it inserts only 2 ads in the content even if the post is lengthy.
So, I want to display the ad in every nth paragraph (keeping the first ad). So, if n=4, the ad will display after the 4th paragraph but only if the 5th paragraph have text.
add_filter( 'the_content', 'so_25888630_ad_between_paragraphs' );
function so_25888630_ad_between_paragraphs($content){
if( in_the_loop() ){
$closing_p = '</p>';
$paragraphs = explode( $closing_p, wptexturize($content) );
$count = count( $paragraphs );
if( 4 >= $count ) {
$totals = array( $paragraphs );
}else{
$midpoint = floor($count / 2);
$first = array_slice($paragraphs, 0, $midpoint );
if( $count%2 == 1 ) {
$second = array_slice( $paragraphs, $midpoint, $midpoint, true );
}else{
$second = array_slice( $paragraphs, $midpoint, $midpoint-1, true );
}
$totals = array( $first, $second );
}
$new_paras = array();
foreach ( $totals as $key_total=>$total ) {
$p = array();
foreach ( $total as $key_paras=>$paragraph ) {
$word_count = count(explode(' ', $paragraph));
if( preg_match( '~<(?:img|ul|li)[ >]~', $paragraph ) || $word_count < 10 ) {
$p[$key_paras] = 0;
}else{
$p[$key_paras] = 1;
}
}
$m = array();
foreach ( $p as $key=>$value ) {
if( 1 === $value && array_key_exists( $key-1, $p ) && $p[$key] === $p[$key-1] && !$m){
$m[] = $key;
}elseif( !array_key_exists( $key+1, $p ) && !$m ) {
$m[] = 'no-ad';
}
}
if( $key_total == 0 ){
$ad = array( 'ad1' => '<p>PLACE YOUR ADD NO 1 HERE</p>' );
}else{
$ad = array( 'ad2' => '<p>PLACE YOUR ADD NO 2 HERE</p>' );
}
foreach ( $total as $key_para=>$para ) {
if( !in_array( 'no_ad', $m ) && $key_para === $m[0] ){
$new_paras[key($ad)] = $ad[key($ad)];
$new_paras[$key_para] = $para;
}else{
$new_paras[$key_para] = $para;
}
}
}
$content = implode( ' ', $new_paras );
}
return $content;
}

Woocommerce Upsell and cross-sell on product page

Hi I got this plugin from WordPress https://wordpress.org/plugins/c4d-woo-boost-sales/
now the plugin works fine, I would just like to change the order of the upsell and cross-sell but for some reason can not get it to work. upsell is first the cross-sell I would like to change it to cross-sell then upsell. DUE TO CHARACTER LIMIT I could not also include the original. but I can say that I only changed these two around
function c4d_woo_bs_get_up_sell_from_cart($products = array())
function c4d_woo_bs_get_cross_sell($products = array())
I have changed it like this see where it reads NEEDS TO BE FIRST AND NEEDS TO BE LAST but changing it this way did noting to the order see sections
function c4d_woo_bs_get_up_sell_from_cart($products = array())
function c4d_woo_bs_get_cross_sell($products = array())
<?php
//// INIT
add_action( 'woocommerce_init', 'c4d_woo_bs_init' );
function c4d_woo_bs_init() {
global $c4d_plugin_manager;
// show in single page
if (isset($c4d_plugin_manager['c4d-woo-bs-page-single-product']) && $c4d_plugin_manager['c4d-woo-bs-page-single-product'] == 1) {
add_action( 'woocommerce_after_single_product_summary', 'c4d_woo_bs_list_product', 99 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
// show in mini cart
if (isset($c4d_plugin_manager['c4d-woo-bs-global-mini-cart']) && $c4d_plugin_manager['c4d-woo-bs-global-mini-cart'] == 1) {
add_action( 'woocommerce_after_mini_cart', 'c4d_woo_bs_mini_cart', 0 );
}
// show in cart page
if (isset($c4d_plugin_manager['c4d-woo-bs-global-cart-page']) && $c4d_plugin_manager['c4d-woo-bs-global-cart-page'] == 1) {
add_action( 'woocommerce_after_cart', 'c4d_woo_bs_list_product' );
}
// show in thankyou page
add_action( 'woocommerce_thankyou', 'c4d_woo_bs_thankyou_page', 10);
// show in email: process || complete order
add_action( 'woocommerce_email_header', 'c4d_woo_bs_email_header', 10, 2 );
add_action( 'woocommerce_email_customer_details', 'c4d_woo_bs_email_customer_details', 10, 4 );
//// CART PAGE
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
//// UP SELL HOOK
add_filter( 'woocommerce_upsell_display_args', 'c4d_woo_bs_upsell_display_args' );
/// CROSS SELL HOOK
add_filter( 'woocommerce_cross_sells_total', 'c4d_woo_bs_cross_sells_total' );
add_filter( 'woocommerce_cross_sells_orderby', 'c4d_woo_bs_cross_sells_orderby' );
add_filter( 'woocommerce_cross_sells_columns', 'c4d_woo_bs_cross_sells_columns' );
}
/////// FUNCTIONS
function c4d_woo_bs_mini_cart($orderId) {
echo '<div class="c4d_woo_bs_mini_cart">';
c4d_woo_bs_list_product($orderId);
echo '</div>';
}
function c4d_woo_bs_replace_title_for_page_email() {
global $c4d_plugin_manager;
$c4d_plugin_manager['c4d-woo-bs-cross-title'] = $c4d_plugin_manager['c4d-woo-bs-email-cross-title'];
$c4d_plugin_manager['c4d-woo-bs-cross-hide-title'] = $c4d_plugin_manager['c4d-woo-bs-email-cross-hide-title'];
$c4d_plugin_manager['c4d-woo-bs-cross-desc'] = $c4d_plugin_manager['c4d-woo-bs-email-cross-desc'];
$c4d_plugin_manager['c4d-woo-bs-cross-hide-desc'] = $c4d_plugin_manager['c4d-woo-bs-email-cross-hide-desc'];
$c4d_plugin_manager['c4d-woo-bs-up-title'] = $c4d_plugin_manager['c4d-woo-bs-email-up-title'];
$c4d_plugin_manager['c4d-woo-bs-up-hide-title'] = $c4d_plugin_manager['c4d-woo-bs-email-up-hide-title'];
$c4d_plugin_manager['c4d-woo-bs-up-desc'] = $c4d_plugin_manager['c4d-woo-bs-email-up-desc'];
$c4d_plugin_manager['c4d-woo-bs-up-hide-desc'] = $c4d_plugin_manager['c4d-woo-bs-email-up-hide-desc'];
}
function c4d_woo_bs_thankyou_page($orderId) {
global $c4d_plugin_manager;
if (isset($c4d_plugin_manager['c4d-woo-bs-global-thankyou-page']) && $c4d_plugin_manager['c4d-woo-bs-global-thankyou-page'] == 1) {
c4d_woo_bs_replace_title_for_page_email();
c4d_woo_bs_list_product($orderId);
}
}
function c4d_woo_bs_email_header($email_heading, $email) {
if (c4d_woo_bs_is_email_pages($email)){
$file = dirname(plugin_dir_path( __FILE__) ) . '/assets/default.css';
if (file_exists($file)) {
$css = file_get_contents($file);
if ($css) {
echo '<style class="c4d-woo-bs">'.$css.'</style>';
}
}
}
}
function c4d_woo_bs_is_email_pages($email) {
$pages = array(
'WC_Email_Customer_Completed_Order',
'WC_Email_Customer_Processing_Order',
'WC_Email_Customer_Note',
);
if (in_array(get_class($email), $pages)) {
return true;
}
return false;
}
function c4d_woo_bs_email_customer_details($order, $sent_to_admin, $plain_text, $email) {
if (c4d_woo_bs_is_email_pages($email)) {
// replace title/desc for email page
c4d_woo_bs_replace_title_for_page_email();
echo '<div class="c4d_woo_bs_email_wrap">';
c4d_woo_bs_list_product($order->get_id());
echo '</div>';
}
}
function c4d_woo_bs_cross_sells_total($params) {
global $c4d_plugin_manager;
if (isset($c4d_plugin_manager['c4d-woo-bs-cross-limit'])) {
return $c4d_plugin_manager['c4d-woo-bs-cross-limit'];
}
return $params;
}
function c4d_woo_bs_cross_sells_orderby($params) {
global $c4d_plugin_manager;
if (isset($c4d_plugin_manager['c4d-woo-bs-cross-order'])) {
return $c4d_plugin_manager['c4d-woo-bs-cross-order'];
}
return $params;
}
function c4d_woo_bs_cross_sells_columns($params) {
global $c4d_plugin_manager;
if (isset($c4d_plugin_manager['c4d-woo-bs-cross-column'])) {
return $c4d_plugin_manager['c4d-woo-bs-cross-column'];
}
return $params;
}
function c4d_woo_bs_upsell_display_args($params) {
global $c4d_plugin_manager;
if (isset($c4d_plugin_manager['c4d-woo-bs-up-limit'])) {
$params['posts_per_page'] = $c4d_plugin_manager['c4d-woo-bs-up-limit'];
}
if (isset($c4d_plugin_manager['c4d-woo-bs-up-column'])) {
$params['columns'] = $c4d_plugin_manager['c4d-woo-bs-up-column'];
}
if (isset($c4d_plugin_manager['c4d-woo-bs-up-order'])) {
$params['orderby'] = $c4d_plugin_manager['c4d-woo-bs-up-order'];
}
return $params;
}
function c4d_woo_bs_list_product($orderId = false) {
global $c4d_plugin_manager;
$c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] = false;
$type = isset($c4d_plugin_manager['c4d-woo-bs-global-show-type']) ? $c4d_plugin_manager['c4d-woo-bs-global-show-type'] : 0;
if ($type == 0) {
echo '<div class="c4d_woo_bs_wrap">';
if (in_array($type, array('crosssell', 'upsell&crosssell'))) {
$products = c4d_woo_bs_cross_get_srouces($orderId);
c4d_woo_bs_get_cross_sell($products);
}
// if product is not set the up/cross sell, plugin will get the product in same category,
// so it will be duplicate so only display one time.
if ($c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] == false) {
if (in_array($type, array('upsell', 'upsell&crosssell'))) {
$products = c4d_woo_bs_up_get_srouces($orderId);
c4d_woo_bs_get_up_sell_from_cart($products);
}
}
echo '</div>';
}
}
function c4d_woo_bs_get_product_from_order($orderId, $type = 'upsell'){
$order = wc_get_order($orderId);
$items = $order->get_items();
$ids = array();
foreach ($items as $key => $item) {
if ($type == 'upsell') {
$ids = array_merge( $item->get_product()->get_upsell_ids(), $ids );
} else {
$ids = array_merge( $item->get_product()->get_cross_sell_ids(), $ids );
}
}
return $ids;
}
function c4d_woo_bs_get_product_from_category($orderId = null, $type = 'upsell'){
global $c4d_plugin_manager, $product;
$products = array();
$category_ids = array();
$category_slugs = array();
$excludes = array();
if (is_product()) {
$pid = method_exists('get_parent_id', $product) ? $product->get_parent_id() : 0;
$pid = $pid ? $pid : $product->get_id();
$data = c4d_woo_bs_get_data($pid, 'category');
if (isset($data[$type]) && $data[$type] !== '') {
$category_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $category_slugs);
}
if (count($category_slugs) < 1) {
$category_ids = array_merge($product->get_category_ids(), $category_ids);
$excludes[] = $product->get_id();
}
} else if (!WC()->cart->is_empty()) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if ( $values['quantity'] > 0 ) {
// check category of products first
$pid = method_exists('get_parent_id', $values['data']) ? $values['data']->get_parent_id() : 0;
$pid = $pid ? $pid : $values['data']->get_id();
// get up/cross sell for category/tag in product
$data = c4d_woo_bs_get_data($pid, 'category');
if (isset($data[$type]) && $data[$type] !== '') {
$category_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $category_slugs);
}
// if product is not set category/tag for up/cross sell, then check the product's categories
if (count($category_slugs) < 1) {
// get product has sampe product's category for cross/up sell
$product = wc_get_product($pid);
$category_ids = array_merge($product->get_category_ids(), $category_ids);
$excludes[] = $product->get_id();
}
}
}
} else {
$order = wc_get_order($orderId);
if ($order) {
$items = $order->get_items();
$ids = array();
foreach ($items as $key => $item) {
$product = $item->get_product();
if ($product) {
$pid = $product->get_parent_id();
$pid = $pid ? $pid : $product->get_id();
$data = c4d_woo_bs_get_data($pid, 'category');
if (isset($data[$type]) && $data[$type] !== '') {
$category_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $category_slugs);
}
if (count($category_slugs) < 1) {
$category_ids = array_merge($product->get_category_ids(), $category_ids);
$excludes[] = $product->get_id();
}
}
}
}
}
// get up/cross sell for category/tag when create category/tag
if (count($category_ids) > 0) {
$c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] = true;
foreach($category_ids as $id) {
$category_is_set = get_term_meta( $id, $type, true );
if ($category_is_set) {
$category_slugs = array_merge(array_map('trim', explode(',', $category_is_set)), $category_slugs);
$c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] = false;
}
}
}
$args = array(
'exclude' => $excludes,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $type == 'upsell' ? $c4d_plugin_manager['c4d-woo-bs-up-limit'] : $c4d_plugin_manager['c4d-woo-bs-cross-limit'],
);
if (count($category_slugs) > 0) {
$args['category'] = $category_slugs;
} else if (count($category_ids) > 0) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category_ids,
'operator' => 'IN',
)
);
}
if (count($category_slugs) > 0 || count($category_ids) > 0 ) {
$products = wc_get_products($args);
}
return $products;
}
function c4d_woo_bs_get_product_from_tags($orderId = null, $type = 'upsell'){
global $c4d_plugin_manager, $product;
$products = array();
$tag_ids = array();
$tag_slugs = array();
$excludes = array();
if (is_product()) {
$pid = method_exists('get_parent_id', $product) ? $product->get_parent_id() : 0;
$pid = $pid ? $pid : $product->get_id();
$data = c4d_woo_bs_get_data($pid, 'tags');
if (isset($data[$type]) && $data[$type] !== '') {
$tag_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $tag_slugs);
}
if (count($tag_slugs) < 1) {
$product = wc_get_product($pid);
if ($product) {
$tag_ids = array_merge($product->get_tag_ids(), $tag_ids);
$excludes[] = $product->get_id();
}
}
} else if (!WC()->cart->is_empty()) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if ( $values['quantity'] > 0 ) {
$pid = method_exists('get_parent_id', $values['data']) ? $values['data']->get_parent_id() : 0;
$pid = $pid ? $pid : $values['data']->get_id();
$data = c4d_woo_bs_get_data($pid, 'tags');
if (isset($data[$type]) && $data[$type] !== '') {
$tag_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $tag_slugs);
}
if (count($tag_slugs) < 1) {
$product = wc_get_product($pid);
if ($product) {
$tag_ids = array_merge($product->get_tag_ids(), $tag_ids);
$excludes[] = $product->get_id();
}
}
}
}
} else {
$order = wc_get_order($orderId);
if ($order) {
$items = $order->get_items();
$ids = array();
foreach ($items as $key => $item) {
$product = $item->get_product();
if ($product) {
$pid = $product->get_parent_id();
$pid = $pid ? $pid : $product->get_id();
$data = c4d_woo_bs_get_data($pid, 'tags');
if (isset($data[$type]) && $data[$type] !== '') {
$tag_slugs = array_merge(array_map('trim', explode(',', $data[$type])), $tag_slugs);
}
if (count($tag_slugs) < 1) {
$tag_ids = array_merge($product->get_tag_ids(), $tag_ids);
$excludes[] = $product->get_id();
}
}
}
}
}
// get up/cross sell for category/tag when create tag
if (count($tag_ids) > 0) {
$c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] = true;
foreach($tag_ids as $id) {
$tag_is_set = get_term_meta( $id, $type, true );
if ($tag_is_set) {
$tag_slugs = array_merge(array_map('trim', explode(',', $tag_is_set)), $tag_slugs);
$c4d_plugin_manager['c4d_woo_bs_category_tag_one_time'] = false;
}
}
}
$args = array(
'exclude' => $excludes,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $type == 'upsell' ? $c4d_plugin_manager['c4d-woo-bs-up-limit'] : $c4d_plugin_manager['c4d-woo-bs-cross-limit'],
);
if (count($tag_slugs) > 0) {
$args['tag'] = $tag_slugs;
} else if (count($tag_ids) > 0) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tag_ids,
'operator' => 'IN',
)
);
}
if (count($tag_slugs) > 0 || count($tag_ids)) {
$products = wc_get_products($args);
}
return $products;
}
function c4d_woo_bs_cross_get_srouces($orderId) {
global $c4d_plugin_manager, $product;
$ids = array();
$products = array();
// product setting , up sell, cross sell,
if (is_product()) {
$ids = array_merge( $product->get_cross_sell_ids(), $ids );
} else if (!WC()->cart->is_empty()) {
$ids = WC()->cart->get_cross_sells();
} else {
$ids = c4d_woo_bs_get_product_from_order($orderId, 'crosssell');
}
if (count($ids) > 0) {
$products = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_visible' );
}
if (count($ids) < 1) {
$products = c4d_woo_bs_get_product_from_category_tags($orderId, 'crosssell');
}
return $products;
}
function c4d_woo_bs_get_product_from_category_tags($orderId, $type = 'upsell') {
global $c4d_plugin_manager;
$products = array();
// if product does not set crosssell/upsell, then get from tags
if (isset($c4d_plugin_manager['c4d-woo-bs-global-source-tags']) && isset($c4d_plugin_manager['c4d-woo-bs-global-source-tags']) == 1) {
$ptags = c4d_woo_bs_get_product_from_tags($orderId, $type);
if (count($ptags) > 0) {
return $ptags;
}
}
// if product does not set crosssell/upsell, then get from category
if (isset($c4d_plugin_manager['c4d-woo-bs-global-source-category']) && isset($c4d_plugin_manager['c4d-woo-bs-global-source-category']) == 1) {
$pcategory = c4d_woo_bs_get_product_from_category($orderId, $type);
if (count($pcategory) > 0) {
return $pcategory;
}
}
return $products;
}
function c4d_woo_bs_up_get_srouces($orderId) {
global $c4d_plugin_manager, $product;
$ids = array();
$products = array();
$cart = WC()->cart;
if (is_product()) {
$ids = array_merge( $product->get_upsell_ids(), $ids );
} else if ( !$cart->is_empty() ) {
$in_cart = array();
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
if ( $values['quantity'] > 0 ) {
$ids = array_merge( $values['data']->get_upsell_ids(), $ids );
$in_cart[] = $values['product_id'];
}
}
$ids = array_diff( $ids, $in_cart );
} else {
$ids = c4d_woo_bs_get_product_from_order($orderId, 'upsell');
}
if (count($ids) > 0) {
$products = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_visible' );
}
if (count($ids) < 1) {
$products = c4d_woo_bs_get_product_from_category_tags($orderId, 'upsell');
}
return $products;
}
function c4d_woo_bs_get_up_sell_from_cart($products = array()) {
global $c4d_plugin_manager;
if (count($products) > 0 ){
$title = isset($c4d_plugin_manager['c4d-woo-bs-up-title']) && $c4d_plugin_manager['c4d-woo-bs-up-title'] != '' ? $c4d_plugin_manager['c4d-woo-bs-up-title'] : esc_html__('Up Sell', 'c4d-woo-bs');
if (isset($c4d_plugin_manager['c4d-woo-bs-up-hide-title']) && $c4d_plugin_manager['c4d-woo-bs-up-hide-title'] == 1) {
$title = '';
}
$desc = isset($c4d_plugin_manager['c4d-woo-bs-up-desc']) && $c4d_plugin_manager['c4d-woo-bs-up-desc'] != '' ? $c4d_plugin_manager['c4d-woo-bs-up-desc'] : '';
if (isset($c4d_plugin_manager['c4d-woo-bs-up-hide-desc']) && $c4d_plugin_manager['c4d-woo-bs-up-hide-desc'] == 1) {
$desc = '';
}
echo '<div class="c4d_woo_bs_up_sell"> NEEDS TOP BE FIRST ';
if ($title) {
echo '<h3 class="block_title">'.$title.'</h3>';
}
if ($desc) {
echo '<div class="block_desc">'.$desc.'</div>';
}
// default params
$limit = '-1'; $columns = 4; $orderby = 'rand'; $order = 'desc';
// Handle the legacy filter which controlled posts per page etc.
$args = apply_filters(
'woocommerce_upsell_display_args',
array(
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
wc_set_loop_prop( 'name', 'up-sells' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );
$orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
$limit = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
// Get visible upsells then sort them, then limit result set.
$products = wc_products_array_orderby( $products, $orderby, $order );
$products = $limit > 0 ? array_slice( $products, 0, $limit ) : $products;
wc_get_template(
'single-product/up-sells.php',
array(
'upsells' => $products,
// Not used now, but used in previous version of up-sells.php.
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
echo '</div>';
}
}
function c4d_woo_bs_get_cross_sell($products = array()) {
global $c4d_plugin_manager;
if (count($products) > 0) {
$title = isset($c4d_plugin_manager['c4d-woo-bs-cross-title']) && $c4d_plugin_manager['c4d-woo-bs-cross-title'] != '' ? $c4d_plugin_manager['c4d-woo-bs-cross-title'] : esc_html__('Cross Sell', 'c4d-woo-bs');
if (isset($c4d_plugin_manager['c4d-woo-bs-cross-hide-title']) && $c4d_plugin_manager['c4d-woo-bs-cross-hide-title'] == 1) {
$title = '';
}
$desc = isset($c4d_plugin_manager['c4d-woo-bs-cross-desc']) ? $c4d_plugin_manager['c4d-woo-bs-cross-desc'] : '';
if (isset($c4d_plugin_manager['c4d-woo-bs-cross-hide-desc']) && $c4d_plugin_manager['c4d-woo-bs-cross-hide-desc'] == 1) {
$desc = '';
}
echo '<div class="c4d_woo_bs_cross_sell"> NEEDS TOP BE LAST';
if ($title) {
echo '<h3 class="block_title">'.$title.'</h3>';
}
if ($desc) {
echo '<div class="block_desc">'.$desc.'</div>';
}
// Get visible cross sells then sort them at random.
$limit = -1 /*4*/; $columns = 4; $orderby = 'rand'; $order = 'desc';
wc_set_loop_prop( 'name', 'cross-sells' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );
// Handle orderby and limit results.
$orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
$order = apply_filters( 'woocommerce_cross_sells_order', $order );
$products = wc_products_array_orderby( $products, $orderby, $order );
$limit = apply_filters( 'woocommerce_cross_sells_total', $limit );
$products = $limit > 0 ? array_slice( $products, 0, $limit ) : $products;
wc_get_template(
'cart/cross-sells.php',
array(
'cross_sells' => $products,
// Not used now, but used in previous version of up-sells.php.
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
echo '</div>';
}
}
I have tried to getting contact with the author of the plugin but no luck, they do not respond to emails, contact forms, support requests at all.
ok as no one had an answer for me, I got one its not perfect but at least it works
/*Change order around on product page for related and cross-sell*/
.c4d_woo_bs_wrap {// this is the main div
display: flex;
flex-direction: column;
}
.c4d_woo_bs_wrap > .c4d_woo_bs_cross_sell {order: 2; } /* Will be displayed second */
.c4d_woo_bs_wrape > .cross-sells {order: 1; } /* Will be displayed first */

Only variables should be passed by reference in

I inherited maintenance of a Drupal 7 website and I have zero PHP experience and I need some help. I have just updated Drupal to 7.31 and am getting the error "Only variables should be passed by reference in..." on lines 43 and 86 of an .inc file.
Here is line 43:
$item = array_shift(array_values($data));
Here is line 86:
$item = array_shift(array_values($data));
Can anyone help me out?
I don't know if this is allowed but here the code for the .inc file
<?php
$plugin = _cdd_core_content_type_plugin('nauticus_core', 'menu_full_leaf', 'Page: menu leaf', array(
'description' => 'Add menu children',
), array(
'override_title' => 1,
'override_title_text' => ''
), 'Sail Nauticus');
function nauticus_core_menu_full_leaf_content_type_plugin_render($subtype, &$conf, $panel_args) {
$trail = menu_get_active_trail();
$menu = '';
if (is_array($trail)) {
$leaf = array_pop($trail);
if (isset($leaf['menu_name'])) {
$menu_name = $leaf['menu_name'];
if ($menu_name !== 'navigation') {
$depth = $leaf['depth'];
$p1 = $leaf['p1'];
$data = menu_tree_page_data($menu_name, $depth + 1);
foreach ($data as $i => $item) {
if ($item['link']['mlid'] !== $leaf['plid']
&& !($leaf['plid'] == 0 && ($item['link']['mlid'] == $leaf['mlid']))
) {
unset($data[$i]);
}
}
if (sizeof($data) > 1) {
$menu = menu_tree_output($data);
foreach ($menu as $i => $item) {
if (isset($item['#original_link'])) {
if ($item['#original_link']['mlid'] !== $p1) {
if (isset($menu[$i]['#below'])) {
$menu[$i]['#below'] = array();
}
}
}
}
}
elseif (sizeof($data) == 1) {
$item = array_shift(array_values($data));
if (isset($item['below']) && !empty($item['below'])) {
$menu = menu_tree_output($item['below']);
foreach ($menu as $i => $item) {
if (isset($item['#original_link'])) {
if ($item['#original_link']['mlid'] !== $p1) {
if (isset($menu[$i]['#below'])) {
$menu[$i]['#below'] = array();
}
}
}
}
}
}
}
}
}
return $menu;
}
function nauticus_core_menu_full_leaf_content_type_plugin_title($subtype, &$conf, $panel_args) {
$title = 'Navigation';
$trail = menu_get_active_trail();
if (is_array($trail)) {
$leaf = array_pop($trail);
if (isset($leaf['menu_name'])) {
$menu_name = $leaf['menu_name'];
if ($menu_name !== 'navigation') {
$depth = $leaf['depth'];
$p1 = $leaf['p1'];
$data = menu_tree_page_data($menu_name, $depth + 1);
foreach ($data as $i => $item) {
if ($item['link']['mlid'] !== $leaf['plid']
&& !($leaf['plid'] == 0 && ($item['link']['mlid'] == $leaf['mlid']))
) {
unset($data[$i]);
}
}
if (sizeof($data) >= 1) {
$item = array_shift(array_values($data));
if (isset($item['link']) && !empty($item['link'])) {
if (isset($item['link']['link_title'])) {
$title = $item['link']['link_title'];
}
}
}
}
}
}
return $title;
}
array_shift requires that the first argument be a pass by reference and array_values returns an array.
change those lines that you have $item = array_shift(array_values($data)); to
$item = array_values($data);
$item = array_shift(&$item);
the & means the variable is pass by reference. This should fix your problem.
if that does not work just remove the &.
Change
$item = array_shift(array_values($data));
to
$tmp = array_values($data);
$item = array_shift($tmp);

Check if array key exists [duplicate]

I have a multidimensional array. I need a function that checks if a specified key exists.
Let's take this array
$config['lib']['template']['engine'] = 'setted';
A function should return true when I call it with:
checkKey('lib','template','engine');
//> Checks if isset $config['lib']['template']['engine']
Note that my array isn't only 3 dimensional. It should be able to check even with only 1 dimension:
checkKey('genericSetting');
//> Returns false becase $c['genericSetting'] isn't setted
At the moment I am using an awful eval code, I would like to hear suggest :)
function checkKey($array) {
$args = func_get_args();
for ($i = 1; $i < count($args); $i++) {
if (!isset($array[$args[$i]]))
return false;
$array = &$array[$args[$i]];
}
return true;
}
Usage:
checkKey($config, 'lib', 'template', 'engine');
checkKey($config, 'genericSetting');
I created the following two functions to do solve the same problem you are having.
The first function check is able to check for one/many keys at once in an array using a dot notation. The get_value function allows you to get the value from an array or return another default value if the given key doesn't exist. There are samples at the bottom for basic usage. The code is mostly based on CakePHP's Set::check() function.
<?php
function check($array, $paths = null) {
if (!is_array($paths)) {
$paths = func_get_args();
array_shift($paths);
}
foreach ($paths as $path) {
$data = $array;
if (!is_array($path)) {
$path = explode('.', $path);
}
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key);
}
if ($i === count($path) - 1 && !(is_array($data) && array_key_exists($key, $data))) {
return false;
}
if (!is_array($data) || !array_key_exists($key, $data)) {
return false;
}
$data =& $data[$key];
}
}
return true;
}
function get_value($array, $path, $defaultValue = FALSE) {
if (!is_array($path))
$path = explode('.', $path);
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0')
$key = intval($key);
if ($i === count($path) - 1) {
if (is_array($array) && array_key_exists($key, $array))
return $array[$key];
else
break;
}
if (!is_array($array) || !array_key_exists($key, $array))
break;
$array = & $array[$key];
}
return $defaultValue;
}
// Sample usage
$data = array('aaa' => array(
'bbb' => 'bbb',
'ccc' => array(
'ddd' => 'ddd'
)
));
var_dump( check($data, 'aaa.bbb') ); // true
var_dump( check($data, 'aaa.bbb', 'aaa.ccc') ); // true
var_dump( check($data, 'zzz') ); // false
var_dump( check($data, 'aaa.bbb', 'zzz') ); // false
var_dump( get_value($data, 'aaa.bbb', 'default value') ); // "bbb"
var_dump( get_value($data, 'zzz', 'default value') ); // "default value"

Check if a "run-time" multidimensional array key exists

I have a multidimensional array. I need a function that checks if a specified key exists.
Let's take this array
$config['lib']['template']['engine'] = 'setted';
A function should return true when I call it with:
checkKey('lib','template','engine');
//> Checks if isset $config['lib']['template']['engine']
Note that my array isn't only 3 dimensional. It should be able to check even with only 1 dimension:
checkKey('genericSetting');
//> Returns false becase $c['genericSetting'] isn't setted
At the moment I am using an awful eval code, I would like to hear suggest :)
function checkKey($array) {
$args = func_get_args();
for ($i = 1; $i < count($args); $i++) {
if (!isset($array[$args[$i]]))
return false;
$array = &$array[$args[$i]];
}
return true;
}
Usage:
checkKey($config, 'lib', 'template', 'engine');
checkKey($config, 'genericSetting');
I created the following two functions to do solve the same problem you are having.
The first function check is able to check for one/many keys at once in an array using a dot notation. The get_value function allows you to get the value from an array or return another default value if the given key doesn't exist. There are samples at the bottom for basic usage. The code is mostly based on CakePHP's Set::check() function.
<?php
function check($array, $paths = null) {
if (!is_array($paths)) {
$paths = func_get_args();
array_shift($paths);
}
foreach ($paths as $path) {
$data = $array;
if (!is_array($path)) {
$path = explode('.', $path);
}
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key);
}
if ($i === count($path) - 1 && !(is_array($data) && array_key_exists($key, $data))) {
return false;
}
if (!is_array($data) || !array_key_exists($key, $data)) {
return false;
}
$data =& $data[$key];
}
}
return true;
}
function get_value($array, $path, $defaultValue = FALSE) {
if (!is_array($path))
$path = explode('.', $path);
foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0')
$key = intval($key);
if ($i === count($path) - 1) {
if (is_array($array) && array_key_exists($key, $array))
return $array[$key];
else
break;
}
if (!is_array($array) || !array_key_exists($key, $array))
break;
$array = & $array[$key];
}
return $defaultValue;
}
// Sample usage
$data = array('aaa' => array(
'bbb' => 'bbb',
'ccc' => array(
'ddd' => 'ddd'
)
));
var_dump( check($data, 'aaa.bbb') ); // true
var_dump( check($data, 'aaa.bbb', 'aaa.ccc') ); // true
var_dump( check($data, 'zzz') ); // false
var_dump( check($data, 'aaa.bbb', 'zzz') ); // false
var_dump( get_value($data, 'aaa.bbb', 'default value') ); // "bbb"
var_dump( get_value($data, 'zzz', 'default value') ); // "default value"

Categories