WordPress do_shortcode in Woocommerce - php

I am using do_shortcode hook that is preventing "Add to cart" to show non-paid members.
I have used the following code and the "Add to cart" doesn't seem to work.
else{
echo do_shortcode( '[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' . '<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>' . '[/ihc-hide-content]" );
');
It's being displayed as :
Output screenshot

Try this code
else{
echo do_shortcode(
'[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' .
'<button
type="submit"
name="add-to-cart"
value="
'.<?php
esc_attr( $product->get_id() )
?>.'"
class="single_add_to_cart_button button alt">
'.<?php
esc_html( $product->single_add_to_cart_text() )
?>.'
</button>' .
'[/ihc-hide-content]" );
');
UPDATE
else{
echo do_shortcode(
'[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' .
'<button
type="submit"
name="add-to-cart"
value="
'.
esc_attr( $product->get_id() )
.'"
class="single_add_to_cart_button button alt">
'.
esc_html( $product->single_add_to_cart_text() )
.'
</button>' .
'[/ihc-hide-content]" );
');

Related

Wordpress woocommerce add to cart button not working

This is the code in the file loop/add-to-cart.php
I tried to edit the code, when I click on the add to cart button, nothing is added anyway
<?php
if (!defined('ABSPATH')) {
exit;
}
global $product;
if ( $product->is_in_stock() ) {
echo apply_filters(
'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf(
'<div class="add-to-cart-container product-buttons-wrapper mt-auto"><a href="%s" data-quantity="%s" class="%s product_type_%s single_add_to_cart_button button button-primary d-none %s" %s> %s</a>',
esc_url($product->add_to_cart_url()),
esc_attr(isset($args['quantity']) ? $args['quantity'] : 1),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
esc_attr($product->get_type()),
$product->get_type() == 'simple' ? 'ajax_add_to_cart' : '',
isset($args['attributes']) ? wc_implode_html_attributes($args['attributes']) : '',
esc_html($product->add_to_cart_text())
),
$product,
$args
);?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr($product->get_id()); ?>" data-quantity="1" data-product_id="<?php echo esc_attr($product->get_id()); ?>" class="single_add_to_cart_button alt button button-primary"><?php echo esc_html($product->single_add_to_cart_text()); ?></button>
<button class="button button-secondary" data-bs-toggle="modal" data-bs-target="#modal-request">Заказать</button></div>
<?php } else { ?>
<div class="product-buttons-wrapper">
<button class="button button-secondary" data-bs-toggle="modal" data-bs-target="#modal-request">Заказать</button>
</div>
<?php }
Tried online solutions, nothing worked.

How to add do_action in echo?

Maybe a crazy question, but I need to put below code in echo and I am getting an error. Basically I want to hide add to cart section from not logged in users and show them different content/button
This whole code
<div class="woocommerce-variation-add-to-cart variations_button">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
in here
<?php
if ( is_user_logged_in() ) {
echo 'THAT CODE SHOULD GO HERE';
} else {
echo 'Welcome, visitor!';
}
?>
You can do :
<?php
if ( is_user_logged_in() ) {
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
} else {
echo 'Welcome, visitor!';
}
?>
Please, you should be able to do this yourself. But here it is:
<?php
if(is_user_logged_in()){
echo "<div class='woocommerce-variation-add-to-cart variations_button'>";
do_action( 'woocommerce_before_add_to_cart_button' );
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
echo "<button type='submit' class='single_add_to_cart_button button alt'>".echo esc_html( $product->single_add_to_cart_text() );."</button>";
do_action( 'woocommerce_after_add_to_cart_button' );
echo "<input type='hidden' name='add-to-cart' value='".echo absint( $product->get_id() );."' />
<input type='hidden' name='product_id' value='".echo absint( $product->get_id() );."' />
<input type='hidden' name='variation_id' class='variation_id' value='0' />
</div>
}";
} else {
echo 'Welcome, visitor!';
}
?>
Although the other answer by Jonathan is better.

Variable SKU not showing on Single Page

I have some problems displaying the SKU for variations, I modified the Single Product Page to display the Variations as a List, this is the Code I used (functions of the the Theme, tried with a Standard Theme, no change)
function woocommerce_variable_add_to_cart() {
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>"method="post" enctype='multipart/form-data'>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<?php
if(!empty($value['attributes'])){
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
<?php
}
}
?>
<table>
<tbody>
<tr>
<td>
<b><?php echo implode('/', $value['attributes']);?></b>
</td>
<td>
<?php echo $value['price_html'];?>
</td>
<td>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" /></div>
<?php
global $product;
?>
<div class>
<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variation' ) ) ) : ?>
<span class="sku_wrapper"><?php esc_html_e( 'Artikelnummer: ', 'woocommerce' ); ?> <span class="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' ); ?></span></span>
<?php endif;?>
</div>
</td>
</tr>
</tbody>
</table>
</form>
<?php
}
}
The only thing not working is getting the SKU of the Variation, instead it gets the SKU of the Product itself or nothing at all (this is the case with the code below, any hint/help would be appreciated. I also tried implent this Variable Product Sku not Working
but it didn't work either
Thanks in Advance (I hade trouble with getting all the coe to display in the code window so I have to use the snippet function, appologies for that)
Your code was a bit hard to read but try using get_post_meta with the variation post id.
$variation_sku = get_post_meta( $value['variation_id'] , '_sku', TRUE );
See if that works.

Custom add to cart button/link in variable single product pages

In WooCommerce 3.0+, I have created some tabs using js and in each tab contains products from different categories. I have managed to modify add-to-cart link for simple products where ones the addtocart button is clicked it goes to the next tab without refreshing and the product is being added successfully to the cart.
if ( has_term( 'jeans-discount', 'product_cat', $post ) ) {
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" data-target="2" href="javascript:void(0); %s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="custom %s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}
However I am not able to modify the addtocart button for variable products in variation-add-to-cart.php template file:
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
For simple product the addtocart link shows as href="javascript:void(0); /wordpress/woo-slider/?add-to-cart=73".
Is there a way i can do this for variable products addtocart link as well?
For WooCommerce version 3.0+ you will override variation-add-to-cart.php template this way:
<?php
/**
* Single variation cart button
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_after_add_to_cart_quantity' );
// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->get_id() ) ) {
?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } else { // Other product categories
?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
I can't test it for real (as I don't have a system of tabs enabled), but this is successful working on my test server without error problems.
For WooCommerce 2.6.x you will override variation-add-to-cart.php template this way:
<?php
/**
* Single variation cart button
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->id ) ) {
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } else { // Other product categories
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>

WooCommerce checkout layout

I would like to change the layout of my WooCommerce checkout page, but I can't figure out how to do it since it seems to consist of multiple PHP pages.
What I'm trying to achieve is moving the summary part and shipping info to the top, and have the input fields for the shipping address shown afterwards.
Should I make these changes using CSS, or can I simply change the order of the hooks in the template?
Thanks!
In the "woocommerce/templates/checkout" folder there is a file called "form-checkout.php". Copy the contents of that file to "yourtheme/woocommerce/checkout/form-checkout.php" On line ~54 there is the following code:
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
Move that to just below
<form name="checkout" method="post" class="checkout" action="<?php echo esc_url( $get_checkout_url ); ?>">
and add:
<?php
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
echo apply_filters( 'woocommerce_order_button_html', '<input type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />' );
?>
to just below the
<?php endif; ?>
and save file the. That will bring the summary and shipping to above the input fields, but you will still have "Place Order" button at the top of the page. Copy the contents of the "review-order.php" to "yourtheme/woocommerce/checkout/review-order.php" and remove the following (from line ~169):
<?php
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
echo apply_filters( 'woocommerce_order_button_html', '<input type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" />' );
?>
Removing the above will remove the "Place order" button at the top of the page.
You can edit the "form-check.php" file in "woocommerce/templates/checkout/form-checkout.php", but it is not recommended as when you update woocommerce you will lose those changes. Copying the file to "yourtheme/woocommerce/checkout/form-checkout.php" will override the file and you won't lose those changes if you update woocommerce.

Categories