I have a question using codeigniter, grocerycrud and image_moo. The code is shown below. On a production server using xampp it works fine, but when uploading that same code to a server to publish it under linux gives error. He uploads the image correctly but when he tries to process the call to image_moo he returns a javascript error. I've tried to see by all means where the error is but no solution so far. Someone who gave you something similar could tell me how to solve it?
Thank you
public function index()
{
$crud = new grocery_CRUD();
$this->load->config ( 'grocery_crud' );
$this->config->set_item ( 'grocery_crud_file_upload_allow_file_types', 'gif|jpeg|jpg|png' );
$crud->set_theme ( 'datatables' );
$crud->set_table ( 'portafolio' );
$crud->set_subject ( 'imagen de portafolio' );
$crud->columns ( 'imagen', 'descripcion_es', 'descripcion_en', 'activa' );
$crud->required_fields ( 'activa', 'descripcion_es', 'descripcion_en', 'imagen' );
$crud->set_field_upload ( 'imagen', 'uploads/portfolio' );
$crud->display_as ( 'descripcion_es', 'Descripción ES' );
$crud->display_as ( 'descripcion_en', 'Descripción EN' );
//voy a reducir el tamano de la imagen siempre
$crud->callback_after_upload ( array ( $this, 'reducir_350' ) );
$output = $crud->render();
$data [ 'grid'] = $output->output;
$data [ 'css_files' ] = $output->css_files;
$data [ 'js_files' ] = $output->js_files;
$data ['titulo_pagina'] = 'Imágenes de portafolio';
$data ['menu_principal'] = $this->load->view ( 'back/view_menu', '', true );
$this->load->view ( 'back/view_base', $data );
}
function reducir_350 ( $uploader_response, $field_info, $files_to_upload )
{
$this->load->library ( 'image_moo' );
//Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = DIR_SITIO . $field_info->upload_path . '/' . $uploader_response[0]->name;
$file_savedto = DIR_SITIO . $field_info->upload_path . '/p-' . $uploader_response[0]->name;
$this->image_moo->load ( $file_uploaded )->resize_crop ( 350,250 )->save ( $file_savedto, true );
return true;
}
Related
For checkout I use one script to show and hide billing fields depending on the shipping way.
Earlier it worked fine, but that time - I think because of using select2 in the city field instead of the text input - the address text field is making issues.
When user writes their address not so fast, the form is starting to update too quick, can delete the new written after that short pause characters or not delete, randomly, and when I want to delete something - the select 2 can appear on the top of the page and not closing.
add_filter( 'woocommerce_update_order_review_fragments', 'awoohc_add_update_form_billing', 99 );
function awoohc_add_update_form_billing( $fragments ) {
$checkout = WC()->checkout();
ob_start();
?>
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) {
$field['country'] = $checkout->get_value( $field['country_field'] );
}
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
<?php
$art_add_update_form_billing = ob_get_clean();
$fragments['.woocommerce-billing-fields'] = $art_add_update_form_billing;
return $fragments;
}
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( 'local_pickup:1' === $chosen_methods[0] ) {
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_state'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'b_override_billing_checkout_fields', 20, 1 );
function b_override_billing_checkout_fields( $fields ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( 'boxberry_self:4' === $chosen_methods[0] || 'boxberry_self_after:6' === $chosen_methods[0] ) {
unset( $fields['billing']['billing_address_1'] );
}
return $fields;
}
// Just hide woocommerce billing country
add_action( 'woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5 );
function hide_checkout_billing_country() {
echo '<style>#billing_country_field{display:none;}</style>';
}
add_filter('woocommerce_billing_fields', 'customize_checkout_fields', 100 );
function customize_checkout_fields( $fields ) {
if ( is_checkout() ) {
// HERE set the required key fields below
$chosen_fields = array( 'postcode', 'country', 'company','last_name', 'address_2');
foreach( $chosen_fields as $key ) {
if( isset($fields['billing_'.$key]) && $key !== 'country') {
unset($fields['billing_'.$key]); // Remove all define fields except country
}
}
}
return $fields;
}
/*
* Updating the form
*/
add_action( 'wp_footer', 'awoohc_add_script_update_shipping_method' );
function awoohc_add_script_update_shipping_method() {
if ( is_checkout() ) {
?>
<script>
jQuery(document).ready(function ($) {
$(document.body).on('updated_checkout updated_shipping_method', function (event, xhr, data) {
$('input[name^="shipping_method"]').on('change', function () {
$('.woocommerce-billing-fields__field-wrapper').block({
message: null,
overlayCSS: {
background: '#fff',
'z-index': 1000000,
opacity: 0.3
}
});
$('select#billing_city').select2();
});
var first_name = $('#billing_first_name').val(),
phone = $('#billing_phone').val(),
email = $('#billing_email').val(),
city = $('#billing_city').val(),
address_1 = $('#billing_address_1').val(),
$(".woocommerce-billing-fields__field-wrapper").html(xhr.fragments[".woocommerce-billing-fields"]);
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_first_name"]').val(first_name);
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_phone"]').val(phone);
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_email"]').val(email);
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_city"]').val(city);
$('select#billing_city').select2();
$(".woocommerce-billing-fields__field-wrapper").find('input[name="billing_address_1"]').val(address_1);
$('.woocommerce-billing-fields__field-wrapper').unblock();
});
});
</script>
<?php
}
}
Yes, I needed to use select2 twice, in other ways it didn't work as needed. But when even I use it once, it didn't help me with that issue. I can't disable select2, the client needs it, and needs to fix changing the address.
How I replaced the city input to select
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_city_fields' );
function override_checkout_city_fields( $fields ) {
// Define here in the array your desired cities (Here an example of cities)
$option_cities = array(
'city1' => 'city1',
'city2' => 'city2',
);
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = $option_cities;
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = $option_cities;
return $fields;
}
Or maybe it's a delivery service plugin. Maybe you see what I don't see.
I know that exist many plugins, but I need to find what's wrong manually.
I tried to /comment/ different parts of the script, so I think it's something with select, or with delicery service plugin if not it.
I have a fully working AJAX load more button on my Wordpress posts. On click, it loads the next page of posts and appends them directly to the end of the first set.
I now need to create category filters. So when a user clicks a category name, it updates the results below via AJAX to only show posts in that category - BUT I need the load more ajax to continue to work correctly. I've tried hashing together two different AJAX calls, and also tried combining both AJAX into one, but I can't seem to get them talking...
Here's the code:
functions.php
function we_title_filters() {
if(is_home()): ?>
<div class="title-filters">
<h3 class="widget-title" style="display:inline-block;">Latest News</h3>
<div class="filters">
<?php
$include = array(3651, 7, 2828, 2829, 2172);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'include' => $include
) );
foreach ( $categories as $category ) {
printf( '%2$s',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
<div class="dropdown" style="float:right;">
<span class="dropbtn">More</span>
<ul class="more-list">
<?php
$include = array(3651, 7, 2828, 2829, 2172);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'include' => $include
) );
foreach ( $categories as $category ) {
printf( '<li>%2$s</li>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
</ul>
</div>
</div>
</div>
<?php endif;
}
This outputs the filters in a list. On click of one of them, it needs to AJAX change the results, but also keep the load more button working, within that category
/* AJAX load more stuff */
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Enqueue necessary JS file and localize.
*
*/
add_action( 'wp_enqueue_scripts', 'sn_infinite_scroll_enqueue' );
function sn_infinite_scroll_enqueue() {
if ( ! is_singular() ) {
global $wp_query;
$args = array(
'nonce' => wp_create_nonce( 'be-load-more-nonce' ),
'url' => admin_url( 'admin-ajax.php' ),
'query' => $wp_query->query,
'maxpage' => $wp_query->max_num_pages,
);
wp_enqueue_script( 'sn-load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array( 'jquery' ), '1.0', true );
wp_localize_script( 'sn-load-more', 'beloadmore', $args );
}
}
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Parse information
*
*/
add_action( 'wp_ajax_sn_infinite_scroll_ajax', 'sn_infinite_scroll_ajax' );
add_action( 'wp_ajax_nopriv_sn_infinite_scroll_ajax', 'sn_infinite_scroll_ajax' );
function sn_infinite_scroll_ajax() {
if ( ! is_singular() ) {
check_ajax_referer( 'be-load-more-nonce', 'nonce' );
$excluded_ids = get_field('top_stories', option);
$args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
$args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
$args['paged'] = esc_attr( $_POST['page'] );
$args['post_status'] = 'publish';
$args['post__not_in'] = $excluded_ids;
$pageType = esc_attr( $_POST['pageType'] );
if ( $pageType == "is-home" ) {
$initial = 30;
$ppp = 30;
$columns = 3;
}
else {
$initial = 30;
$ppp = 30;
$columns = 3;
}
$args['posts_per_page'] = $ppp;
$args['offset'] = $initial + ( $ppp * ( $_POST['page'] ) );
ob_start();
$loop = new WP_Query( $args );
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
sn_post_summary( $loop->current_post, $columns );
endwhile;
endif;
wp_reset_postdata();
$page = esc_attr( $_POST['page'] );
}
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
}
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Output articles
*
*/
function sn_post_summary( $count, $columns ) {
// Be able to convert the number of columns to the class name in Genesis
$fractions = array( '', 'half', 'third', 'fourth', 'fifth', 'sixth' );
// Make a note of which column we're in
$column_number = ( $count % $columns ) + 1;
// Add one-* class to make it correct width
$countClasses = sprintf( 'one-' . $fractions[$columns - 1] . ' ', $columns );
// Add a class to the first column, so we're sure of starting a new row with no padding-left
if ( 1 == $column_number )
$countClasses .= 'first ';
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
add_action( 'genesis_entry_header', 'genesis_do_post_image', 8 );
echo '<article class="' . $countClasses . implode( ' ', get_post_class() ) . '">'; // add column class
do_action( 'genesis_entry_header' );
echo '</article>';
}
This is a modified version of the infinite scroll loader found here: https://gist.github.com/graylaurenm/86daa4f23aa8749c0933f72133ac7106
I removed the infinite scroll options so it only loads on click of the button.
I am having a wordpress site which is using the Divi theme. When I update the number of items in the cart, the mini cart on the header is not getting updated accordignly. However, the number of items in the mini cart are getting updated on re-loading the page.
Divi is using the below function for updating the cart,
if ( ! function_exists( 'et_show_cart_total' ) ) {
function et_show_cart_total( $args = array() ) {
if ( ! class_exists( 'woocommerce' ) || ! WC()->cart ) {
return;
}
$defaults = array(
'no_text' => false,
);
$args = wp_parse_args( $args, $defaults );
$items_number = WC()->cart->get_cart_contents_count();
$url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : WC()->cart->get_cart_url();
printf(
'<a href="%1$s" class="et-cart-info">
<span>%2$s</span>
</a>',
esc_url( $url ),
( ! $args['no_text']
? esc_html( sprintf(
_nx( '%1$s Item', '%1$s Items', $items_number, 'WooCommerce items number', 'Divi' ),
number_format_i18n( $items_number )
) )
: ''
)
);
}
}
How can I update the mini cart on updating the number of items in the cart on ajax call?
Please can anyone help?
Please try the following code in your functions.php file
add_filter( 'woocommerce_add_to_cart_fragments', 'your_custom_functions', 10, 1 );
function your_custom_functions( $fragments ) {
$fragments['.your_cart_class'] = '' . WC()->cart->get_cart_contents_count() . ' Items';
return $fragments;
}
I've got a problem when adding a grouped product to cart.
I need to set a custom option for all products that are added to cart while adding a grouped product to cart.
What I have tried last (with a little bit of success):
<checkout_cart_product_add_after>
<observers>
<customoptions>
<type>singleton</type>
<class>Company_CustomOptions_Model_Observer</class>
<method>addCustomOptionGroupSku</method>
</customoptions>
</observers>
</checkout_cart_product_add_after>
and
public function addCustomOptionGroupSku(Varien_Event_Observer $observer) {
$product = $observer->getProduct ();
if ($product->isGrouped ()) {
$quoteItem = $observer->getQuoteItem ();
$additionalOptions = array (
'options' => array (
'label' => 'GROUPSKU',
'value' => $product->getSku ()
)
);
$quoteItem->addOption ( new Varien_Object ( array (
'product' => $quoteItem->getProduct (),
'code' => 'additional_options',
'value' => serialize ( $additionalOptions )
) ) );
}
}
I have created one grouped product, containing two products.
But that code only adds the custom option "GROUPSKU" to one of the items in the cart. The other one is untouched.
How do I get all the QuoteItems that are about to be added to the cart?
PS: I have also added this question to the Magento part of StackExchange: https://magento.stackexchange.com/questions/51883/add-custom-options-while-adding-grouped-product-to-cart
I found a solution which does not need an observer.
I had to do a rewrite to Mage_Sales_Model_Quote. More specific the method addProductAdvanced()
Bad news: You can not simply use parent::addProductAdvanced() and then do your own stuff. You have to copy the original code and fill it up with your code.
Here is what I did (look out for the comment starting with /********):
public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null) {
if ($request === null) {
$request = 1;
}
if (is_numeric ( $request )) {
$request = new Varien_Object ( array (
'qty' => $request
) );
}
if (! ($request instanceof Varien_Object)) {
Mage::throwException ( Mage::helper ( 'sales' )->__ ( 'Invalid request for adding product to quote.' ) );
}
$cartCandidates = $product->getTypeInstance ( true )->prepareForCartAdvanced ( $request, $product, $processMode );
/**
* Error message
*/
if (is_string ( $cartCandidates )) {
return $cartCandidates;
}
/**
* If prepare process return one object
*/
if (! is_array ( $cartCandidates )) {
$cartCandidates = array (
$cartCandidates
);
}
$parentItem = null;
$errors = array ();
$items = array ();
foreach ( $cartCandidates as $candidate ) {
// Child items can be sticked together only within their parent
$stickWithinParent = $candidate->getParentProductId () ? $parentItem : null;
$candidate->setStickWithinParent ( $stickWithinParent );
$item = $this->_addCatalogProduct ( $candidate, $candidate->getCartQty () );
/******** own modification for custom option GROUPSKU*/
if($product->isGrouped()){
$additionalOptions = array (
'options' => array (
'label' => 'GROUPSKU',
'value' => $product->getSku ()
)
);
$item->addOption ( new Varien_Object ( array (
'product' => $item->getProduct (),
'code' => 'additional_options',
'value' => serialize ( $additionalOptions )
) ) );
}
/******** own modification end*/
if ($request->getResetCount () && ! $stickWithinParent && $item->getId () === $request->getId ()) {
$item->setData ( 'qty', 0 );
}
$items [] = $item;
/**
* As parent item we should always use the item of first added product
*/
if (! $parentItem) {
$parentItem = $item;
}
if ($parentItem && $candidate->getParentProductId ()) {
$item->setParentItem ( $parentItem );
}
/**
* We specify qty after we know about parent (for stock)
*/
$item->addQty ( $candidate->getCartQty () );
// collect errors instead of throwing first one
if ($item->getHasError ()) {
$message = $item->getMessage ();
if (! in_array ( $message, $errors )) { // filter duplicate messages
$errors [] = $message;
}
}
}
if (! empty ( $errors )) {
Mage::throwException ( implode ( "\n", $errors ) );
}
Mage::dispatchEvent ( 'sales_quote_product_add_after', array (
'items' => $items
) );
return $item;
}
This way all the products that are added to the cart using a grouped product now have the custom option.
This question already has answers here:
How do I create a Magento session outside of Magento?
(4 answers)
Closed 7 years ago.
I nave a issue with customer login event. I have application (flex app) outside magento and there is a form for customer login. I use this code to login customers:
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session');
try {
$session->login ( $_REQUEST['username'], $_REQUEST['password'] );
$return .= '<userid>'.$session->getCustomer()->getId().'</userid>';
} catch (Exception $e) {
$return .= '<error>'.$e->getMessage().'</error>';
}
Everything works well and the customer is logged in. The issue is that the event is dispatched but Mage::Visitor and Mage::Reports does't catch the event and if I return from flex to product view page it gets MySQL errors in the "report_viewed_product_index" table. Can anyone help with this.
Thank you!
Solved my issue reading How do I create a Magento session outside of Magento?
Here is what I've done
require_once '../../app/Mage.php';
umask(0);
Mage::app($_REQUEST['store_id'])->init();
$core_session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session');
$return = '<user>';
$write = Mage::getSingleton ( 'core/resource' )->getConnection ( 'core_write' );
$url = Mage::getUrl ( '*/*/*', array ('_current' => true ) );
Mage::getSingleton ( 'core/session' )->setLastUrl ( $url );
$visitor_id = $_SESSION ['core'] ['visitor_data'] ['visitor_id'];
if (! empty ( $visitor_id )) {
Mage::getSingleton ( 'log/visitor' )->setId ( $visitor_id );
} else {
Mage::getSingleton ( 'customer/session' )->setWishlistItemCount ( 0 );
Mage::getSingleton ( 'catalog/session' )->setCatalogCompareItemsCount ( 0 );
$write->query ( "INSERT INTO log_url_info (url, referer) VALUES (?, ?)", array ($url, Mage::helper ( 'core/http' )->getHttpReferer ( true ) ) );
$url_id = $write->lastInsertId ();
$log_visitor = Mage::getSingleton ( 'log/visitor' )->initServerData ()->setFirstVisitAt ( now () )->setIsNewVisitor ( true )->setLastVisitAt ( now () )->setLastUrlId ( $url_id )->save ();
$write->query ( "INSERT INTO log_url (url_id, visitor_id, visit_time) VALUES (?, ?, ?)", array ($url_id, $log_visitor->getId (), now () ) );
$core_session->setVisitorData ( $log_visitor->getData () );
$visitor_id = $log_visitor->getId ();
}
try {
$session->login ( $_REQUEST['username_cl_mag'], $_REQUEST['password_cl_mag'] );
//$session->setCustomerAsLoggedIn($session->getCustomer());
//$customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
$customerId = $session->getCustomerId();
$eventModel = Mage::getModel('reports/event');
$eventModel->updateCustomerType($visitor_id, $customerId);
Mage::getModel('reports/product_index_compared')
->updateCustomerFromVisitor()
->calculate();
Mage::getModel('reports/product_index_viewed')
->updateCustomerFromVisitor()
->calculate();
$return .= '<userid>'.$session->getCustomerId().'</userid>';
} catch (Exception $e) {
$return .= '<error>'.$e->getMessage().'</error>';
}
print $return .= "</user>";