Woocommerce Upsell and cross-sell on product page - php
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 */
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; }
Disable Open Cart 3 check out on sundays
I have a store which is set up with Open Cart 3. I want to disable the checkout on Sundays. I have tried the following code but it doesn't seem to be working. I added the code to following file cataloge/controller/checkout/checkput.php class ControllerCheckoutCheckout extends Controller { public function index() { // Validate cart has products and has stock. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { $this->response->redirect($this->url->link('checkout/cart')); } if (date("l") == "Sunday") { $this->response->redirect($this->url->link('checkout/cart')); $this->session->data['error'] = 'Sorry we are closed on Sundays'; } Here is the full code before I added my line; <?php class ControllerCheckoutCheckout extends Controller { public function index() { // Validate cart has products and has stock. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { $this->response->redirect($this->url->link('checkout/cart')); } // Validate minimum quantity requirements. $products = $this->cart->getProducts(); foreach ($products as $product) { $product_total = 0; foreach ($products as $product_2) { if ($product_2['product_id'] == $product['product_id']) { $product_total += $product_2['quantity']; } } if ($product['minimum'] > $product_total) { $this->response->redirect($this->url->link('checkout/cart')); } } $this->load->language('checkout/checkout'); $this->document->setTitle($this->language->get('heading_title')); $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js'); $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js'); $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js'); $this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css'); // Required by klarna if ($this->config->get('payment_klarna_account') || $this->config->get('payment_klarna_invoice')) { $this->document->addScript('http://cdn.klarna.com/public/kitt/toc/v1.0/js/klarna.terms.min.js'); } $data['breadcrumbs'] = array(); $data['breadcrumbs'][] = array( 'text' => $this->language->get('text_home'), 'href' => $this->url->link('common/home') ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('text_cart'), 'href' => $this->url->link('checkout/cart') ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('checkout/checkout', '', true) ); $data['text_checkout_option'] = sprintf($this->language->get('text_checkout_option'), 1); $data['text_checkout_account'] = sprintf($this->language->get('text_checkout_account'), 2); $data['text_checkout_payment_address'] = sprintf($this->language->get('text_checkout_payment_address'), 2); $data['text_checkout_shipping_address'] = sprintf($this->language->get('text_checkout_shipping_address'), 3); $data['text_checkout_shipping_method'] = sprintf($this->language->get('text_checkout_shipping_method'), 4); if ($this->cart->hasShipping()) { $data['text_checkout_payment_method'] = sprintf($this->language->get('text_checkout_payment_method'), 5); $data['text_checkout_confirm'] = sprintf($this->language->get('text_checkout_confirm'), 6); } else { $data['text_checkout_payment_method'] = sprintf($this->language->get('text_checkout_payment_method'), 3); $data['text_checkout_confirm'] = sprintf($this->language->get('text_checkout_confirm'), 4); } if (isset($this->session->data['error'])) { $data['error_warning'] = $this->session->data['error']; unset($this->session->data['error']); } else { $data['error_warning'] = ''; } $data['logged'] = $this->customer->isLogged(); if (isset($this->session->data['account'])) { $data['account'] = $this->session->data['account']; } else { $data['account'] = ''; } $data['shipping_required'] = $this->cart->hasShipping(); $data['column_left'] = $this->load->controller('common/column_left'); $data['column_right'] = $this->load->controller('common/column_right'); $data['content_top'] = $this->load->controller('common/content_top'); $data['content_bottom'] = $this->load->controller('common/content_bottom'); $data['footer'] = $this->load->controller('common/footer'); $data['header'] = $this->load->controller('common/header'); $this->response->setOutput($this->load->view('checkout/checkout', $data)); } public function country() { $json = array(); $this->load->model('localisation/country'); $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']); if ($country_info) { $this->load->model('localisation/zone'); $json = array( 'country_id' => $country_info['country_id'], 'name' => $country_info['name'], 'iso_code_2' => $country_info['iso_code_2'], 'iso_code_3' => $country_info['iso_code_3'], 'address_format' => $country_info['address_format'], 'postcode_required' => $country_info['postcode_required'], 'zone' => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']), 'status' => $country_info['status'] ); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } public function customfield() { $json = array(); $this->load->model('account/custom_field'); // Customer Group if (isset($this->request->get['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->get['customer_group_id'], $this->config->get('config_customer_group_display'))) { $customer_group_id = $this->request->get['customer_group_id']; } else { $customer_group_id = $this->config->get('config_customer_group_id'); } $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id); foreach ($custom_fields as $custom_field) { $json[] = array( 'custom_field_id' => $custom_field['custom_field_id'], 'required' => $custom_field['required'] ); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } } Could anyone have any idea why this doesn't work or another way of disabling the checkout on Sundays?
If you put this line below befoe the redirection, the error message will show up. $this->session->data['error'] = 'Sorry we are closed on Sundays'; here is code after the change class ControllerCheckoutCheckout extends Controller { public function index() { // Validate cart has products and has stock. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { $this->response->redirect($this->url->link('checkout/cart')); } if (date("l") == "Sunday") { $this->session->data['error'] = 'Sorry we are closed on Sundays'; $this->response->redirect($this->url->link('checkout/cart')); }
REST API generation for Opencart
I tried to generate a REST API for my customers, categories, products, coupons, manufacturers and all others to use it in my Android application, but I can't get any solution. Can anyone tell me how to create APIs in opencart 3.0.2.0?
Make a custom controller in catalog > Controller > api folder Create controller file name: allproducts.php You can copy following code and paste of allproducts.php <?php class ControllerApiAllproducts extends Controller { private $error = array(); // All Products public function index(){ $products = array(); $this->load->language('catalog/product'); $this->load->model('catalog/product'); $this->load->model('tool/image'); $error[]['no_json']= "No JSON"; $product_total = $this->model_catalog_product->getTotalProducts(); $results = $this->model_catalog_product->getProducts(); foreach ($results as $result) { if (is_file(DIR_IMAGE . $result['image'])) { $image = $this->model_tool_image->resize($result['image'], 40, 40); } else { $image = $this->model_tool_image->resize('no_image.png', 40, 40); } $special = false; $product_specials = $this->model_catalog_product->getProductSpecials($result['product_id']); foreach ($product_specials as $product_special) { //if (($product_special['date_start'] == '0000-00-00' || strtotime($product_special['date_start']) < time()) && ($product_special['date_end'] == '0000-00-00' || strtotime($product_special['date_end']) > time())) { $special = $product_special['price']; //break; // } } $shop_products['shop_products'][] = array( 'product_id' => $result['product_id'], 'image' => $image, 'name' => $result['name'], 'model' => $result['model'], 'price' => $result['price'], 'special' => $special, 'quantity' => $result['quantity'], 'status' => $result['status'] ); } if (isset($this->request->get['json'])) { echo json_encode($shop_products);die; } else { $this->response->setOutput(json_encode($error)); } } // Product info Page public function productInfo(){ $this->load->language('catalog/product'); $this->load->model('catalog/product'); $this->load->model('tool/image'); $product_details = array(); $error['fail'] = 'Failed'; if (isset($this->request->get['product_id'])) { //$product_details['product_id'] = $this->request->get['product_id']; $product_details = $this->model_catalog_product->getProduct($this->request->get['product_id']); echo json_encode($product_details);die; } else { $this->response->setOutput(json_encode($error)); } } // Category Listing Page public function categories(){ $shop_categories = array(); $this->load->model('catalog/category'); $error['fail'] = 'Failed'; if (isset($this->request->get['json'])) { $shop_categories =$this->model_catalog_category->getCategories(); echo json_encode($shop_categories);die; } else { $this->response->setOutput(json_encode($error)); } } // Product Listing By Category public function categoryList(){ $this->load->model('catalog/category'); $this->load->model('catalog/product'); $this->load->model('tool/image'); $error['fail'] = 'Failed'; if (isset($this->request->get['path'])) { $url = ''; $path = ''; $parts = explode('_', (string)$this->request->get['path']); $category_id = (int)array_pop($parts); foreach ($parts as $path_id) { if (!$path) { $path = (int)$path_id; } else { $path .= '_' . (int)$path_id; } $category_info = $this->model_catalog_category->getCategory($path_id); } } else { $category_id = 0; } $category_info = $this->model_catalog_category->getCategory($category_id); if ($category_info) { $url = ''; //$data['categories'] = array(); $results = $this->model_catalog_category->getCategories($category_id); foreach ($results as $result) { $filter_data = array( 'filter_category_id' => $result['category_id'], 'filter_sub_category' => true ); } $products = array(); $filter_data = array( 'filter_category_id' => $category_id ); $product_total = $this->model_catalog_product->getTotalProducts($filter_data); $products = $this->model_catalog_product->getProducts($filter_data); echo json_encode($products); die; } else { $this->response->setOutput(json_encode($error)); } } // All Manufacturers Listing public function manufactureList() { $this->load->model('catalog/manufacturer'); $this->load->model('tool/image'); $error['fail'] = 'Failed'; $manufactureList = array(); if (isset($this->request->get['json'])) { $manufactureList = $this->model_catalog_manufacturer->getManufacturers(); echo json_encode($manufactureList);die; } else { $this->response->setOutput(json_encode($error)); } } // Manufactur info Page public function manufactureInfo() { $this->load->model('catalog/manufacturer'); $this->load->model('catalog/product'); $this->load->model('tool/image'); $error['fail'] = 'Failed'; if (isset($this->request->get['manufacturer_id'])) { $manufactureInfo = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id); echo json_encode($product_details);die; } else { $this->response->setOutput(json_encode($error)); } } // Category Listing Page public function specialProduct(){ $specialProduct = array(); $this->load->model('catalog/product'); $this->load->model('tool/image'); $error['fail'] = 'Failed'; if (isset($this->request->get['json'])) { $specialProduct = $this->model_catalog_product->getProductSpecials(); echo json_encode($specialProduct);die; } else { $this->response->setOutput(json_encode($error)); } } } I made following APIs and its path (see Postman apps): ALL Products : http://examples.com/index.php?route=api/allproducts&json Product By ID : http://examples.com/index.php?route=api/allproducts/productInfo&json&product_id=30 ALL Categories : http://examples.com/index.php?route=api/allproducts/categories&json Category Wise Product : http://examples.com/index.php?route=api/allproducts/categoryList&json&path=25_28 ALL Manufacturers : http://examples.com/index.php?route=api/allproducts/manufactureList&json Manufactur By ID : http://examples.com/index.php?route=api/allproducts/manufactureInfo&manufacturer_id=11 Special Products : http://examples.com/index.php?route=api/allproducts/specialProduct&json
How to group Stripe charges by key
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) ); } } } } }
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);