Syntax error' (T_ENCAPSED_AND_WHITESPACE) on Woocommerce Hook - woocommerce

I meet this error message in this function and i don't see any problem with characters.
syntax error, unexpected ''cfwc_save_cus' (T_ENCAPSED_AND_WHITESPACE)
function cfwc_save_custom_field( $post_id ) {
$product = wc_get_product( $post_id );
$title = isset( $_POST['custom_description'] ) ? $_POST['custom_description'] : '';
$product->update_meta_data( 'custom_description', sanitize_text_field( $title ) );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );

Related

How do i Replace function from wordpress plugins?

I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}

Change & Link of Add To Cart Button if product already in cart

I am trying to change the link & text of the Add to Cart button on the archive page if the products is already in cart.
I only allow quantity one per product to be added.
But the default way of Woocommerce is to direct to the single page with the notice.
Instead I would like the user to either
See the text "View Cart" and direct the user to the cart
or
See the text "Added" and have no link whatsoever.
Thank you
To change the text, you need the codes below.
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "View Cart";
}
return $text;
}
To redirect to cart, this will do it.
add_action( 'wp_loaded', 'wc_add_to_cart_action', 19 );
function wc_add_to_cart_action(){
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return;
}
$product_id = absint( $_REQUEST['add-to-cart'] );
$adding_to_cart = wc_get_product( $product_id );
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && $adding_to_cart->is_sold_individually() ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
or use this for the second part.
add_action( 'woocommerce_simple_add_to_cart', 'wc_simple_add_to_cart' );
function wc_simple_add_to_cart(){
global $product;
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
echo sprintf( '<form class="cart">%s</form>', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ) );
}
}
update
for shop page you need this code:
add_filter('woocommerce_product_add_to_cart_url', 'wc_product_add_to_cart_url', 10, 2 );
function wc_product_add_to_cart_url( $url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && is_shop() ) {
$url = wc_get_cart_url();
}
return $url;
}

Edit Title Tag Wordpress tag.php

I'am trying to build an Input field for setting a custom HTML title-tag on tag.php in Wordpress. I archived it on page.php with custom fields. But it have no idea how to get it working on tag.php.
In My Function i have now:
add_theme_support( 'title-tag' );
add_filter( 'document_title_parts', 'orweb_custom_title');
function orweb_custom_title( $title ) {
if ( ! is_singular() ) return $title;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true ));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title['title'] = $custom_title;
}
return $title;
}
function orweb_custom_title_old($title){
if ( ! is_singular() ) return;
$custom_title = trim(get_post_meta( get_the_id(), 'title', true));
if( ! empty( $custom_title ) ){
$custom_title = esc_html( $custom_title );
$title = $custom_title;
}
return $title;
}
add_filter('wp_title', 'orweb_custom_title_old', 10, 2);
Found here: https://orbitingweb.com/blog/custom-seo-friendly-title-tags-wordpress/.

Wordpress - How to auto insert some words from title to tags

I want to auto-insert words in title of a post and begin by # in tags. and use this function. but when i publish a post, all words inserted.
function convert_to_tag( $post_id ) {
$post = get_post( $post_id );
$tax = isset( $types[$post->post_type] ) ? $types[$post->post_type] : 'post_tag';
if( isset( $post->post_title ) ) :
$title_words = explode( ' ', $post->post_title );
foreach( $title_words as $word ) :
if (0 == strpos($word, '#')) :
wp_insert_term(
$word,
$tax,
array()
);
endif;
endforeach;
endif;
}
add_action( 'save_post', 'convert_to_tag' );

Woocommerce cart custom field error on update

I'm trying to add a custom field on each product on cart. I got the code from
WooCommerce: Add input field to every item in cart
but the update is not working. I tried to change the global $woocommerce to WC() but still it gives me 500 (Internal Server Error) on chrome console everytime I click update. heres the code:
cart.php:
<td class="product-url">
<?php
$html = sprintf( '<div class="url"><input type="text" name="cart[%s][url]" value="%s" size="4" title="Url" class="input-text url text" /></div>', $cart_item_key, esc_attr( $values['url'] ) );
echo $html;
?>
function.php (This is not a snippet so do not run):
// get from session your URL variable and add it to item
add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
function cart_item_from_session( $data, $values, $key ) {
$data['url'] = isset( $values['url'] ) ? $values['url'] : '';
return $data;
}
// this one does the same as woocommerce_update_cart_action() in plugins\woocommerce\woocommerce-functions.php
// but with your URL variable
// this might not be the best way but it works
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['url'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['url'] = $cart_totals[ $cart_item_key ]['url'];
}
}
}
}
}
// this is in Order summary. It show Url variable under product name. Same place where Variations are shown.
add_filter( 'woocommerce_get_item_data', 'item_data', 10, 2 );
function item_data( $data, $cart_item ) {
if ( isset( $cart_item['url'] ) ) {
$data['url'] = array('name' => 'Url', 'value' => $cart_item['url']);
}
return $data;
}
// this adds Url as meta in Order for item
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
woocommerce_add_order_item_meta( $item_id, 'Url', $values['url'] );
}

Resources